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?
Related
This question already has an answer here:
Convert ISO 8601 timestamp string to epoch seconds in Java [duplicate]
(1 answer)
Closed 14 days ago.
I have date and time in string format in one of my models.
"startTime": "2022-10-19T14:31:22+00:00"
How can I convert it to long format in Java?
I tried using
'Long.valueOf(startTime)' and 'Long.parseLong(startTime)' are two functions.
but in both I am getting an exception.
"at the java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)"
You can parse your datetime format using builtin methods - also to convert it to a long value (assuming the number of seconds since the epoch).
Instant.parse("2022-10-19T14:31:22+00:00").getEpochSecond()
See https://docs.oracle.com/en/java/javase/19/docs/api/java.base/java/time/Instant.html
See code run at Ideone.com.
This question already has answers here:
Parsing ISO 8601 date format like 2015-06-27T13:16:37.363Z in Java [duplicate]
(2 answers)
Closed 4 years ago.
I need to transform a Twitter timestampe into a Java Date object,
here is an example of a value of a Timestampe: "2015-01-06T21:07:00Z"
Can you please give me sample of java code (standard Java) doing the job?
Thank you
I recommend you take advantage of the new Date/Time API introduced in Java 8, specifically Instant as follows:
Instant.parse("2015-01-06T21:07:00Z");
You can then perform a multitude of operations, but keep in mind that the object is immutable, so any changes to the instance (that aren't chained) must be stored in a separate variable.
Actually it is ISO 8601 format for UTC time zone.
It conforms with XML DateTime format as well.
So, to get actual java.util.Calendar or java.util.Date out of it you simply can use available in JDK
Calendar twitterCalendar = javax.xml.bind.DatatypeConverter.parseDateTime("2015-01-06T21:07:00Z");
Date twitterDate = javax.xml.bind.DatatypeConverter.parseDateTime("2015-01-06T21:07:00Z").getTime();
Just be aware: java.util.Date has no Time Zone information in it. Your string is in UTC, so if you try to print value of twitterDate you will see Date/Time in TimeZone of your computer/server. Still actual value of twitterDate stays the same
millisecond value that is an offset from the Epoch, January 1, 1970 00:00:00.000 GMT (Gregorian).
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);
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.
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