How to guess unknown dateTime format? [duplicate] - java

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.

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

Can I have more control on zone offset formatting at DateTimeFormatter [duplicate]

This question already has answers here:
SimpleDateFormat with TimeZone
(6 answers)
Closed 4 years ago.
Currently we are using
DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSX")
To format the time range to an external vendor for a range of data, for India, that formatter will give time like this:
2018-04-26T00:00:00.000+0530
However, my vendor say they cannot accept this format and it have to look like
2018-04-26T00:00:00.000+05:30
However, look like in DateTimeFormatter, whatever I choose Z/z/X/x, I don't get that format of offset. Just wonder is that a way to customize the offset to be HH:mm?
Or, I need to get the offset in second and work that our myself?
It is three x. Just tried with JavaRepl:
java.time.format.DateTimeFormatter
.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSxxx")
.withZone(java.time.ZoneId.systemDefault())
.format(java.time.Instant.now())
Results in
java.lang.String res10 = "2018-04-27T11:06:50.648+00:00"
After some trial and error, I saw that this is also documented in the API documentation of DateTimeFormatter but it is not easy to find (buried in a lot of other text):
Three letters outputs the hour and minute, with a colon, such as '+01:30'
DateTimeFormatter API Documentation

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

Time Zone cannot be changed in Calendar util [duplicate]

This question already has answers here:
Working with various Calendar TimeZone in Java (without using Joda Time)
(3 answers)
Closed 9 years ago.
This sentence is supposed to get time information at Chicago time zone:
Calendar.getInstance(TimeZone.getTimeZone("America/Chicago")).getTime();
My problem is no matter what string I put in getTimeZone(), result would be changed.
Could anyone explain this situation?
The key thing to understand is that a java.util.Date represents UTC only - it has no time zone information. Time zones are presentation layer only - they are used to figure out how to display the time represented by the java.util.Date.
So if you use SimpleDateFormat or the Calendar.get(...) methods, the time zone will be taken into account.

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.

Categories

Resources