How parse this 2009-05-12T13:40:00Z to java date [duplicate] - java

This question already has answers here:
What's the best way to parse an XML dateTime in Java?
(9 answers)
Closed 6 years ago.
what's the best way to parse this string 2009-05-12T13:40:00Z to a valid java date?
I'm not sure for what "T" and "Z" stands for - i guess Z means zulu?
Thanks,
Joo

The format string you have is a UTC date/time in the ISO 8601 format. The format is simply the date, a T separator and the UTC time (or zulu time).
java.text.SimpleDateFormatter should perform all the parsing you need.

Note that the patter has some special handling for internal static values like 'T':
See the answer here Illegal pattern character 'T' when parsing a date string to java.util.Date

Related

ZoneDateTime object getting defaulted to UTC [duplicate]

This question already has answers here:
Java 8 Date and Time: parse ISO 8601 string without colon in offset [duplicate]
(4 answers)
Closed 4 years ago.
Hi I have a date object in my controller request mapping. The object is of ZoneDateTime. The problem is on parsing the data to ZoneDateTime it is converting it to UTC by default. I need to retain the timezone information. Is there a way to handle that.
ex: 2018-06-10T12:00:00+0500
value in my controller:
2018-06-10T07:00:00[UTC]
I am planning to use an object mapper to fix it while marshaling of data in controller. But i am not sure if i am heading to right direction.
Thanks.
The string you're parsing doesn't have time zone information. The appropriate type to use is OffsetDateTime:
OffsetDateTime dt = OffsetDateTime.parse("2018-06-10T12:00:00+0500",
DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssX"))
You can convert that to a ZonedDateTime by calling dt.toZonedDateTime().

Which date time format is this? [duplicate]

This question already has answers here:
SimpleDateFormat parsing date with 'Z' literal [duplicate]
(12 answers)
Closed 6 years ago.
I wonder which format is the following datetime value:
"2016-05-18T12:05:33Z"
This date time format is used on Zendesk's tickets in the fields of created_at and updated_at.
I know that its "yyyy-MM-ddTHH:mm:ss........", but what does the "Z" stand for?
What I want to do is parse and convert into a java.time class for storing dates and times, but I do not know which is the best one.
That is ISO 8601 format and the Z is the timezone indicator; it means UTC.
The best java.time class to use is ZonedDateTime. Example:
ZonedDateTime dateTime = ZonedDateTime.parse("2016-05-18T12:05:33Z",
DateTimeFormatter.ISO_DATE_TIME);

Parsing weird date format [duplicate]

This question already has answers here:
Converting ISO 8601-compliant String to java.util.Date
(31 answers)
Closed 7 years ago.
I have this weird date format that I have to parse.
2015-12-18T03:36:06.000+0000
I am currently mapping a regex to date formats so I can parse different dates. However, this format got me confused. Any help appreciated.
To parse a String into a Date in Java, you use a DateFormat object, and specify the format the date is in. There is no need to use a Regex, the Java library has a way to do this for you.
final DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
final Date d = df.parse("2015-12-18T03:36:06.000+0000"); // From your code above
System.out.println(d);
See the JavaDoc for SimpleDateFormat for more explanation as to what the symbols mean. This is actually a common format for dates called ISO 8601, I just took the pattern right from the documentation.
Watch out! These DateFormat objects are not threadsafe.

What is regular expression for date of format "dd-mm-yyyy"? [duplicate]

This question already has answers here:
Regex to validate date formats dd/mm/YYYY, dd-mm-YYYY, dd.mm.YYYY, dd mmm YYYY, dd-mmm-YYYY, dd/mmm/YYYY, dd.mmm.YYYY with Leap Year Support
(27 answers)
Closed 8 years ago.
regular expression that i want is to be used with Java. i have tried using previously designed regular expression but they are not serving the need
i have tried this version of regular expression:
^(((((0[1-9])|(1\d)|(2[0-8]))-((0[1-9])|(1[0-2])))|((31-((0[13578])|(1[02])))|((29|30)-((0[1,3-9])|(1[0-2])))))-((20[0-9][0-9]))|(29-02-20(([02468][048])|([13579][26]))))$
don't use regex for date parsing, use the date parser, E.g SimpleDateFormat.parse()
Check string format by:
^[0-9]{2}-[0-9]{2}-[0-9]{4}$
Then validate your date using:
new SimpleDateFormat("dd-MM-yyyy").parse(yourDateString);
At a quick glance, the following should work:
(0[1-9]|[1-2][0-9]|3[01])\-(0[1-9]|1[0-2])\-(\d{4})
At a second glance however you will find that this doesn't stop things like trying to match 31-02-2008, i.e.: that is the 31st of February, which is an invalid date.
As others suggested it is preferable to use something like SimpleDateFormat.parse() if possible, as it will cover the day/month variations, and is more likely to cover things like leap years and such.

Android/Java Time difference ISO 8601 with Now [duplicate]

This question already has answers here:
Converting ISO 8601-compliant String to java.util.Date
(31 answers)
Closed 6 years ago.
I have an ISO 8601 time string and want to calculate the time difference to now in minutes without using joda time. How is this done?
Thanks
Parse it using SimpleDateFormat to get a Date, get the milliseconds-since-unix-epoch of that using Date.getTime(), then compare with System.currentTimeMillis().
If your ISO-8601 strings contain a time zone offset as something like "-08:00" (which they certainly can), you'll need to remove the colon first, and use the Z format specifier in SimpleDateFormat. (In Java 7 you could use X, but that's not available in Android's version of SimpleDateFormat as far as I'm aware.)
What about javax.xml.datatype.Duration as returned by javax.xml.datatype.DatatypeFactory?

Categories

Resources