Invalid ZoneDateTime parsing in Java / Scala [duplicate] - java

This question already has an answer here:
ZonedDateTime change behavior jdk 8/11
(1 answer)
Closed 2 years ago.
I am trying to parse dates from strings to ZonedDateTimes and I've come across a bizzare problem.
2020-11-01T01:00-05:00[America/New_York]
This is an hour right after time EDT ends this year. When I pass it to ZonedDateTime.parse I get
ZonedDateTime.parse("2020-11-01T01:00-05:00[America/New_York]")
// 2020-11-01T01:00-04:00[America/New_York]
but if I do
ZonedDateTime.parse("2020-11-01T01:00-04:00[America/New_York]").plusHours(1)
I get
2020-11-01T01:00-05:00[America/New_York]
So it's not like Java cannot represent this ambiguous value or something..
Can anyone explain to me that behavior and possible solution?
Note: I am using Java 8

As Amir Schnell said in the comments, this seems to be a bug in the JDK, as they cannot reproduce this in Java 11.
For now, I have found this work around:
Parse the string into a local date time, zone ID, and zone offset, and create a ZonedDateTime using those three things:
TemporalAccessor ta = DateTimeFormatter.ISO_ZONED_DATE_TIME.parse("2020-11-01T01:00-05:00[America/New_York]");
System.out.println(
ZonedDateTime.ofLocal(
LocalDateTime.from(ta),
ZoneId.from(ta),
ZoneOffset.from(ta)
)
);

Related

Offset not calculated correctly at OffsetDateTime to Instant conversion in Java 8 [duplicate]

This question already has answers here:
ZonedDateTime America/Phoenix zone to GMT having issue [duplicate]
(3 answers)
Java Date Time conversion to given timezone
(3 answers)
Closed 7 months ago.
I would need the community's help because I could not find the answer in the Java documentation. I don't understand how the offset is taken into the math calculations when I try to convert an OffsetDateTime (ex: 2022-07-09T11:30:34) object to an Instant object. For example:
If we would run on OpenJDK 1.8 the command in a main function: OffsetDateTime.parse("2022-07-09T12:30:34+01:00", DateTimeFormatter.ISO_OFFSET_DATE_TIME).toInstant() the outcome would be an Instance of date-and-time 2022-07-09T11:30:34 when I would had expected an Instant of 2022-07-09T13:30:34. The difference is the hour. Why do I get it like this?
And the opposite using -01:00 will do the revet.
I apologize for not formatting my text correctly or if I missed something. I would appreciate it if my post would not be marked us not worthy. And sorry if the answer was already answered in a different thread, which I could not find.
Thank you in advance.

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

Best way to increment the date in a String URL? [duplicate]

This question already has answers here:
How to add days to a date in Java
(3 answers)
Closed 7 years ago.
So I have a URL that contains date=2016-01-25 somewhere in between. My goal is, when a user enters the URL and n days (in a GUI application's text fields), it should go into a loop of n times and increment the date from the URL. That also means it goes to the next month if it is 30/31. Anyone have an optimized approach to this?
Assuming you are using Java 8, you can use the java.time.LocalDate class to parse, add days, and convert back to a string. Here's an example:
String date = "2016-01-25";
LocalDate localDate = LocalDate.parse(date);
localDate = localDate.plusDays(15);
System.out.println(localDate); // Prints "2016-02-09"
To add to the Java 8 solutions, if you're using an earlier version of Java you can use JodaTime's LocalDate class to accomplish the same thing. Syntax will be the same as in Java 8.
You would need to parse the date string out of the URL, using something like regex and then create a Date object increment your date and rebuild the URL.
Java's Date primitives such as LocalDate have support for "plusDays(int n)"

How to get difference in two times in java? [duplicate]

This question already has answers here:
Calculating the difference between two Java date instances
(45 answers)
Closed 7 years ago.
I have set a time using the hh:mm:ss format in the variable a and set whatever the current time is in the variable in x, same format. And in a if statement if the current time is less then the set time i want it to outprint how many hh:mm:ss it will take to get to the set a value. Thanks
a.setHours(7);
a.setMinutes(45);
a.setSeconds(00);
currenttime.format(x);
if (x.compareTo(a)<0); //x is current time (hh:mm:ss)
{
//how do you outprint difference between x and a
System.out.print("You have event in: " x);
}
if (x.compareTo(a)>0 && x.compareTo(a1)<0)
{
}
Maybe you should have a look at java.time.Duration as detailed in this post here how-to-find-difference-between-two-joda-time-datetimes-in-minutes
Or you could also use joda-time instead and go with the details presented in this here number-of-days-between-two-dates-in-joda-time
Using the Days class with the withTimeAtStartOfDay method should work
... or in this post here how-to-calculate-difference-between-two-dates-in-years-etc-with-joda-time
... that is unless you're already using Java 8, since
Joda-Time is the de facto standard date and time library for Java. From Java SE 8 onwards, users are asked to migrate to java.time (JSR-310)

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