Calculating number of months between two OffsetDateTimes [duplicate] - java

This question already has answers here:
Java 8: Calculate difference between two ZonedDateTime
(3 answers)
Closed 4 years ago.
I am using java 8 and I'm trying to calculate the amount of months between two OffsetDateTime objects. What is the best way to do this?

Without more details, the standard way would be:
long months = ChronoUnit.MONTHS.between(odt1, odt2);

the most comprehensible way (IMO) is to use ChronoUnit
OffsetDateTime odt1 = OffsetDateTime.now();
OffsetDateTime odt2 = odt1.plusMonths(10);
System.out.println(ChronoUnit.MONTHS.between(odt1, odt2));

Related

Java getting number of days between two dates [duplicate]

This question already has answers here:
Java, Calculate the number of days between two dates [duplicate]
(10 answers)
Closed 1 year ago.
I am trying to calculate the number of days between two dates.
Even though I found many similar questions, I just cannot come up with a solution.
Date lastpickup = (Date) section_userdata.get("lastpickup");
Date today = new Date();
Instant instant_lastpickup = lastpickup.toInstant().truncatedTo(ChronoUnit.DAYS);
Instant instant_today = today.toInstant().truncatedTo(ChronoUnit.DAYS);
This is my code at the moment.
A date is read from a config and should be compared to the actual date.
With the code I have I am able to determine whether the date is the same or not, but I want to know which amount of days (ideally as an Integer) is between those two.
I want to look at the calendar days, not 24h rhythm.
Well, it was way easier than I thought.
Here's my solution
long daysCount = ChronoUnit.DAYS.between(instant_lastpickup, instant_today);

How to find the number of days between LocalDate java? [duplicate]

This question already has answers here:
Calculate days between two Dates in Java 8
(14 answers)
Closed 6 years ago.
I've got two LocalDates:
LocalDate date1;
LocalDate date2;
//...
How to find the number of days between those dates?
LocalDate.until is what you're looking for. (LocalDate implements Temporal, which is what Days accepts, and ChronoUnit is a library of TemporalUnit instances.)
long days = date1.until(date2, ChronoUnit.DAYS);
I would do something like
long daysBetween = DAYS.between(date1, date2);

get today's date and time as string [duplicate]

This question already has answers here:
How to get current moment in ISO 8601 format with date, hour, and minute?
(23 answers)
Calendar date to yyyy-MM-dd format in java
(11 answers)
Closed 6 years ago.
I am new to Java and trying to use Calendar object to get date and time of now as a string. I am particularly stuck at object and object conversions.
Here is the format I need (as a string):
2016-03-30T14:21:00Z
If I could just get the date and time format right, I could play around with the string but I am struggling with deprecated methods.
Thank you for replies
Your best bet is to start using Java 8's new Time API (or JodaTime if you can't use Java 8)
LocalDateTime now = LocalDateTime.now();
String isoFormat = DateTimeFormatter.ISO_INSTANT.format(now.toInstant(ZoneOffset.UTC));
System.out.println(isoFormat);
outputs 2016-03-30T17:51:38.639Z (when I tested it)
Solved my question using this link:
http://beginnersbook.com/2013/05/current-date-time-in-java/
Thanks for replies, I will also look into Java 8' time API

Difference between 2 dates Java [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Difference in days between two dates in Java?
I need to find difference between 2 dates. First one is in string format,
s1=2012-10-01T15:33:34.652905Z
I need to convert s1 into date d1 and then find the difference in integer between today's date d2 and d1.
How do I do this?
To convert your string to a date, you can use SimpleDateFormat.parse("yyyy-MM-dd'T'HH:mm:ss.SSSZ").
Given two Date objects, you can get whatever differences (days, seconds...) you need with JODA Time as advised in SO entry : Difference in days between two dates in Java?

How do I create a new Joda DateTime truncated to the last hour? [duplicate]

This question already has answers here:
JodaTime equivalent of DateUtils.truncate()
(4 answers)
Closed 7 years ago.
I am pulling timestamps from a file that I want to create a new DateTime for, but I want to create the DateTime at the floor of the hour (or any Joda Period will do).
How Can I do this?
Wohoo, found it. Simple like everything in Joda once I traced down the calls.
DateTime dt = new DateTime().hourOfDay().roundFloorCopy();

Categories