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

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)

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.

Invalid ZoneDateTime parsing in Java / Scala [duplicate]

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

Time as a string parser in java [duplicate]

This question already has answers here:
Parsing time strings like "1h 30min"
(8 answers)
Closed 6 years ago.
Is there any libraries that is able to take a string such as 5d 1h 2m 15s and add it to a java date / java Calendar?
ie a system property will be set as 5d 1h 2m 15s
We will read in the system property and add this amount of time to the current date.
Otherwise I will have to implement this as a long in milliseconds.
you'll need a bit of parsing to extract the component element but Joda-Time's duration should help you out
http://joda-time.sourceforge.net/apidocs/org/joda/time/Duration.html
If you are using Java8 joda-time's concept were integrated so no need to external dependencies
https://docs.oracle.com/javase/8/docs/api/java/time/Duration.html
once you have a duration adding it to the current date time should be trivial
Java8 and jodaTime
Instant ajustedTime = Instant.now().plus(yourDurationInstance);
you can convert to and from Java's date pretty easily, others have already answered here

Java 8 class to represent time intervals [duplicate]

This question already has answers here:
Is there a class in java.time comparable to the Joda-Time Interval?
(4 answers)
Closed 6 years ago.
Java 8 introduced a new Time & Date API, with classes like Period or Duration.
Now I'm looking for a class to represent date intervals, e.g. "from 4th August 2016 to 8th August 2016" and answer the question: do these intervals overlap. Period doesn't seem to satisfy this, since it works in an affine way, not knowing where the interval has started, only how long it takes.
Is there any Java 8 standard library class to suit my needs? Or do I have to write my own?
You could resolve the date down into a long and use an IntervalTree. Here's one I made earlier.

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

Categories

Resources