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);
Related
This question already has answers here:
Cannot parse String in ISO 8601 format, lacking colon in offset, to Java 8 Date
(3 answers)
Closed 2 years ago.
Jira is giving me this date format via Rest API:
2021-01-21T11:08:45.000+0100
How can i parse this to a LocalDateTime in Java?
I tried
ZonedDateTime.parse("2021-01-21T11:08:45.000+0100", DateTimeFormatter.ISO_OFFSET_DATE_TIME);
Or this:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
ZonedDateTime.parse("2021-01-21T11:08:45.000+0100"), formatter);
The result is DateTimeParseException
Since the zone offset in your value is in the format +0100, it cannot be parsed with any of the predefined formatters like DateTimeFormatter.ISO_OFFSET_DATE_TIME, as it expects it to be in the format +01:00
You can parse 2021-01-21T11:08:45.000+0100 with the pattern "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
ZonedDateTime.parse("2021-01-21T11:08:45.000+0100", DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ"))
The reference for DateTimeFormatter is here.
This question already has answers here:
How to Format ISO-8601 in Java
(3 answers)
Converting ISO 8601-compliant String to java.util.Date
(31 answers)
Closed 4 years ago.
I know there are alot of these, but I can'e seem to find the magic string for this date format:
String textDate = "2018-04-25T18:23:57.556Z";
My code is:
String pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSZ";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
simpleDateFormat.parse(textDate)
What's weird is there is a "Z" in the date string itself, so I am not sure how the timezone works on this one.
If I change the date format to:
"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
It works, but I am not sure how to get the time zone then...
Z = UTC
The literal "Z" is actually part of the ISO 8601 datetime standard for UTC times. When "Z" (Zulu) is tacked on the end of a time, it indicates that that time is UTC, so really the literal Z is part of the time.
The java.time classes use the ISO 8601 standard formats by default when parsing/generating strings. The Instant class represents a moment in UTC, a perfect fit for your input string.
Instant instant = Instant.parse( "2018-04-25T18:23:57.556Z" ) ;
This question already has answers here:
Java string to date conversion
(17 answers)
Closed 5 years ago.
I've a problem with date. I've a date as string in format: "2017-05-10 16:30"
I'd like to convert it to date looking the same as I wrote before.
Please help me, thanks in advance!
You can use LocalDateTime.parse to create a LocalDateTime object, but the second part of your question didn't really make sense. The date object itself doesn't have a format, so it can't "look like" anything. You decide what format to adopt when you convert it back to a string.
A simple search in google or stackoverflow might lead to answer but here you go.
Pre Java 8
Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm").parse("2017-05-10 16:30");
Java 8
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
LocalDateTime ldt = LocalDateTime.parse("2017-05-10 16:30", dtf);
This question already has answers here:
How to convert LocalDate to SQL Date Java?
(3 answers)
Closed 7 years ago.
This post was edited and submitted for review last year and failed to reopen the post:
Original close reason(s) were not resolved
What's the correct way to convert between java.sql.Date and LocalDate (in both directions) in Java 8 (or higher)?
The Java 8 version (and later) of java.sql.Date has built in support for LocalDate, including toLocalDate and valueOf(LocalDate).
To convert from LocalDate to java.sql.Date you can use
java.sql.Date.valueOf( localDate );
And to convert from java.sql.Date to LocalDate:
sqlDate.toLocalDate();
Time zones:
The LocalDate type stores no time zone information, while java.sql.Date does. Therefore, when using the above conversions, the results depend on the system's default timezone (as pointed out in the comments).
If you don't want to rely on the default timezone, you can use the following conversion:
Date now = new Date();
LocalDate current = now.toInstant()
.atZone(ZoneId.systemDefault()) // Specify the correct timezone
.toLocalDate();
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?