Is it possible to parse the following date and time in java? - parsing

How will i be able to parse 2013-12-20T00:45:00.000+05:30 to get date and time separately. This is in IST.
I was able to get the date using
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
sdf.format(sdf1.parse(expirationDate)
but i dont know how to get the time.

String expirationDate = "2013-12-20T00:45:00.000+05:30";
SimpleDateFormat string2DateFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
SimpleDateFormat date2StringFormatter = new SimpleDateFormat("HH:mm:ss");
System.out.println(date2StringFormatter.format(string2DateFormatter.parse(expirationDate)));

Timestamp timestamp = new Timestamp(System.currentTimeMillis());
Date date = new Date(timestamp.getTime());
// S is the millisecond
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM/dd/yyyy' 'HH:MM:ss:S");
System.out.println(simpleDateFormat.format(timestamp));
System.out.println(simpleDateFormat.format(date));
for time
Date date = new Date(); // given date
Calendar calendar = GregorianCalendar.getInstance(); // creates a new calendar instance
calendar.setTime(date); // assigns calendar to given date
calendar.get(Calendar.HOUR_OF_DAY); // gets hour in 24h format
calendar.get(Calendar.HOUR); // gets hour in 12h format
calendar.get(Calendar.MONTH); // gets month number, NOTE this is zero base

Related

change the datetime format in flutter, to leave it only with the date

I would like to know how I can change the datetime format in flutter, to leave it only with the date.
I also want to know if there is another widget that only contains the date.
Thanks
You can use DateFormat from intl package.
With it you can custom to any format that you need.
Add to pubspec.yaml
dependencies:
intl: ^0.15.7
On Dart code:
import 'package:intl/intl.dart';
final dateFormatter = DateFormat('yyyy-MM-dd');
final dateString = dateFormatter.format(DateTime.now());
Then you can put the string into any widget. Example:
Text(dateString)
About date format: https://www.cl.cam.ac.uk/~mgk25/iso-time.html
Make a new DateTime and get the day from that:
var date = DateTime.now();
var result = "${date.year}/${date.month}/${date.day}";
print(result); // prints 2019/3/6
You can get the day month and year from DateTime object and convert it to string.
DateTime today = DateTime.now();
print(today); // prints 2019-12-15 00:00:00.000
DatetTime date = DateTime(today.year, today.month, today.day).toString().substring(0,10);
print(date); // prints 2019-12-15
hope this helps

How to change back year that change into 2000 after using date format

hi I want to get current hour and minute from Date(), so I need to format it into string and want to bring back into date again. But after I try to convert to date the year change into 2000, how can I got back to current year.
//date formatter
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "HH:mm"
dateFormatter.timeZone = TimeZone.current
// Get current time and format it to compare
var currentTime = Date() //Get current time
let currentTimeStr = dateFormatter.string(from: currentTime) //get current time only hour and minute
currentTime = dateFormatter.date(from: currentTimeStr)! //this is where the problem because the year change into 1 January 2000
From what I read in the comments, I think you want both the current time in a Date object and also a string with only hours and minutes in "HH:MM" format.
The problem comes from trying to use a formatter that doesn't have a year specified. You are overwriting the currentTime from a string that doesn't have a year (or day, or month) defined, so it defaults to Jan 1st 2000 (the hours and minutes should be correct).
You're also saying you need to format it into a String, and then go back to a Date object. You don't, you already have all the data you need in the Date object, so keep it around and use it when you need to. If this means creating a bunch of DateFormatters all over your project, you can always extend Date to have a function or variable that returns the string with the format you want.
extension Date {
var hoursAndMinutesString: String {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "HH:mm"
dateFormatter.timeZone = TimeZone.current
return dateFormatter.string(from: self)
}
}
Then just call the function from the Date object when you need to, like this:
currentTime.hoursAndMinutesString

Firebase & Swift - Date saved with UTC -7?

I'm trying to understand how exactly dates are saved in the CloudFirestore of Firebase...
When I put the current date in the database dateToSAve = Date() , this date is stored with UTC-7:
But in someone in France for example, want to save data on Firebase, it will be the current date -9 hours to conform with the UTC -7
And sometimes, because of that, it can be a different day...
Do you know how to handle this problem? I would like to save in the database, the current date of the user.
You should not store date object in database, instead you should save timestamp in UTC 0. And after retrieving that timestamp you can convert it back into date with the time portion in your current time zone.
For getting timestamp in UTC 0:
let timestamp = Date().timeIntervalSince1970
And for converting timestamp back to date with the time portion in your current timezone:
// gives date with time portion in UTC 0
let date = Date(timeIntervalSince1970: timestamp)
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MMM-dd-yyyy" // change to your required format
dateFormatter.timeZone = TimeZone.current
// date with time portion in your specified timezone
print(dateFormatter.string(from: date))

How to format Date(Time)Picker value to a date only string?

My default date setting in Fiori launchpad is "dd.MM.yyyy, HH:mm". Whenever I get the date from the date picker placed in view, I am getting the date in the above format.
Now I want to send this date to backend through ODataModel which generally accepts date in XML date format (e.g. "2014-12-30"). I tried the below code, but it did not work.
var fromDate = this.byId("fromDate").getValue(); // "30.12.2014, 10:36"
var oDateFormat = DateFormat.getDateTimeInstance({ pattern : "yyyy-MM-dd" }); // DateFormat required from "sap/ui/core/format/DateFormat"
var subFromDate = oDateFormat.format(new Date(fromDate)); // "0NaN-NaN-NaN".
When I check in debugger mode, the value in subFromDate is "0NaN-NaN-NaN". Please provide your valuable suggestions.
You can use getDateValue() method instead of getValue.
var fromDate = this.byId("fromDate").getDateValue(); // returns a JS date object
var oDateFormat = DateFormat.getDateTimeInstance({ pattern : "yyyy-MM-dd" }); // DateFormat required from "sap/ui/core/format/DateFormat"
var subFromDate = oDateFormat.format(fromDate); // "2014-12-30"
The fromDate isn't correct. When I using new Date("30.12.2014, 10:36"), the console show message " Invalid Date".
I look for more information about "Date" from MDN(link).
new Date(dateString)
String value representing a date. The string should be in a format recognized by the Date.parse() method. The dateString could be '30 12 2014 10:36'.
So you need to replace the '.' and ',' to ' ' in fromDate first.
You can use display format and value format properties of date time picker.
new sap.m.DateTimePicker("ED",{
valueFormat: "yyyy-MM-ddTHH:mm:ss",
displayFormat: "dd-MM-yyyy HH:mm:ss"
});
you can easily get the values using
sap.ui.getCore().getControl("ED").getValue();

How to find expiry date from current and given date

I am working on the application which have 15days free subscription and then it give the expiry message to the user.
I got the expiry date (yyyy-MM-dd format) in login and have to check with current date .
I have tried with the two date object 1st object have current date and 2nd have expiry date.
But i am only get true when both date are same with DateTimeUtilities.isSameDate(date1, date2); function but it only return true if both date are same.
Please anyone help me to solve my problem.
You can find your answer with the following
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");//Your format type
Date todayDate = new Date(HttpDateParser.parse(formatter.formatLocal(System.currentTimeMillis())));//It converts system date in your given format & store in todayDate object
Date expiryDate = new Date(HttpDateParser.parse(expiryDateString));//It Store your expiry date in your expiryDate format
Calendar cal1 = Calendar.getInstance();
Calendar cal2 = Calendar.getInstance();
cal1.setTime(todayDate);
cal2.setTime(expiryDate);
if(cal1.before(cal2))
{
// Current date is less than your Expiry date
}
else
{
// Current date is equals and greater your Expiry date
}
You can do the following
boolean expired = (currentDate.getTime()>expiryDate.getTime());
if(expired)
Status.show("Expired");

Resources