Java 8 class to represent time intervals [duplicate] - java

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.

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.

getActualMaximum function returns the non-existent value in kotlin [duplicate]

This question already has answers here:
Why is January month 0 in Java Calendar?
(18 answers)
Why Java Calendar set(int year, int month, int date) not returning correct date? [duplicate]
(3 answers)
Closed 3 years ago.
I have the following code to get the last day of the month but what is strange is that returns 31 for the last day. On that month of the year there should be only 30 days. Therefore, when I tried to parse the string data, I get Caused by: java.text.ParseException: Unparseable date: "2020/6/31 00:00:00" Some tips or example will be lovely. I would love to hear from you!
val cal: Calendar = Calendar.getInstance().also {
it.clear()
it.set(Calendar.YEAR, 2020)
it.set(Calendar.MONTH, 6)
}
val lastDaysOfMonth = cal.getActualMaximum(Calendar.DAY_OF_MONTH)
The java.util.Calendar class is notoriously difficult to use, so much so that Joda-Time was independently developed and then officially adapted/adopted into the java.time package (with some small modifications from the original developer). Here, the problem you have with Calendar is that JANUARY is a constant 0. So, when you use a 6 for Calendar.MONTH that is JULY. There are 31 days in July 2020. Don't use the Calendar class if you can avoid it. The java.time API is far more consistent. If you can't avoid using Calendar, use the provided constants - for example,
it.set(Calendar.MONTH, Calendar.JUNE)
But, even if you're using a platform without java.time, you can almost certainly use the ThreeTen-Backport (or the Android version) like,
val lastDaysOfMonth = java.time.LocalDate.of(2020, 6, 1)
.with(java.time.temporal.TemporalAdjusters.lastDayOfMonth())
.getDayOfMonth();
That would be a lot shorter with import statements (naturally).

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

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)

convert date to c# ticks in java [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
C# DateTime.Ticks equivalent in Java
hello guys
can anybody tell me how to convert date to .Net ticks in java.
any help will be appreciative.
thanks
ticks = 621355968000000000L+javaMillis*10000;
You may also want to check the icu4j library from the ICU project especially the UniversalTimeScale class which is similar to .Net ticks.
DateTime.Ticks Property The value of this property is the number of 100-nanosecond intervals that have elapsed since 12:00 A.M., January 1, 0001.
Date.getTime() Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this Date object.
Accounting for the 1970 years offset and multiplying by ten seems to be the solution.

Categories

Resources