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.
Related
This question already has answers here:
Convert java.util.Calendar ISO 8601 format to java.sql.Timestamp
(2 answers)
java to mysql. I need convert from string parametre to timestamp
(2 answers)
Converting LocalDateTime to Timestamp format
(2 answers)
Closed 2 years ago.
I'm trying to convert 2020-11-03T14:03:45.173649-05:00 into timestamp using java.
I tried using Timestamp.valueOf("2020-11-03T14:03:45.173649-05:00"); here but getting error saying java.lang.IllegalArgumentException:Timestamp format must be yyyy-mm-dd hh:mm:ss[.fffffffff]. Is there any way to convert Date time having timezone offset to timestamp?
Since this is an ISO format, the easiest would be to get an OffsetDateTime first and then convert to Timestamp:
var input = "2020-11-03T14:03:45.173649-05:00";
var odt = OffsetDateTime.parse(input);
var ts = Timestamp.from(odt.toInstant());
Alternatively, you can parse the string using a SimpleDateFormat.
This question already has answers here:
How to parse dates in multiple formats using SimpleDateFormat
(13 answers)
Java LocalDateTime.parse with millisecond precision but optional microsecond precision
(1 answer)
How to check validity of Date String?
(4 answers)
Closed 2 years ago.
There can be two different date time formats as shown below. The second variant has milliseconds time.
2020-09-07T16:15:42Z
2020-09-09T11:41:58.5152Z
Currently i am using this way to parse the date. Is there a way to specify a single format to account for both of these two cases?
def dateutc = (new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSS'Z'"))
dateutc.setTimeZone(TimeZone.getTimeZone("UTC"))
time = dateutc.parse(point.time.toString()).getTime()
Thanks for the help!
Please note i am using java 7.
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:
Java string to date conversion
(17 answers)
Closed 7 years ago.
While writing a code for a java project I used date format "dd/mm/yyyy". But another colleague writing code for the same project used "dd/MM/yyyy" format and mismatch occurred. I also observed that database date format works fine with "dd/MM/yyyy", not with "dd/mm/yyyy".
So I need to know the difference between these two formats.
The difference can be found here https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
basically small m is minute bit M is month
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?