ZoneDateTime object getting defaulted to UTC [duplicate] - java

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().

Related

How to transform in Java a Twitter Timestamp into a Date [duplicate]

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).

How to guess unknown dateTime format? [duplicate]

This question already has answers here:
How to get date datatype from sql database to java?
(2 answers)
Closed 7 years ago.
I have a DateTime which I received as JSON via a REST Servce of the Couchbase's Sync Gateway:
"2015-05-20T13:32:25.9999478-07:00"
I do not have the access to the Sync Gateway's configs.
I did no find any documentation about the default format of Sync Gateway's dateTime format.
I do not understand what the .9999478-07:00" means.
Is there a way to guess that somehow?
That is the ISO standard notation for date time
YYYY-MM-DD,
then a 'T' for time, HH:MI:SS.S* (fractional seconds),
+/- time zone (there are half our zones!)
Time and also time zone optional.
Look in the wikipedia or javadoc.
When no time zone the date time representation can be sorted alphabetical to be naturally ordered.

Parse DateTime type in C# sent in JSON [duplicate]

This question already has answers here:
Parsing a JSON date info into a C# DateTime
(4 answers)
Closed 9 years ago.
How do I parse Json date in java {"UserCreationTime":"/Date(1348477516620+0530)/"} this is json response i got from .net wcf service, it is basically DateType type in C#.
Thanks in advance.
The first number, 1348477516620 is the number of milliseconds since 1/1/1970 UTC.
The second number +0530 is the UTC offset of the system that created this value, at this specific point in time. But that number is not reflected in the first value in any way.
In other words, if all you care about is a specific instance in time, throw away the second part and just use the first part.
Date date = new Date(1348477516620);
And yes, it's an ugly format and nobody likes it. It's being slowly phased out in favor of ISO8601.

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?

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

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

Categories

Resources