Java разница между localdatetime

Java 8 – Разница между двумя локальными датами или локальным временем

В Java 8 мы можем использовать Период , Длительность или Хронометр для вычисления разницы между двумя Локальными датами или локальным временем .

  1. Период для расчета разницы между двумя Локальными датами .
  2. Продолжительность для вычисления разницы между двумя Локальное время .
  3. Хроноунит для всего.

1. Период

package com.mkyong.java8; import java.time.LocalDate; import java.time.Period; public class JavaLocalDate < public static void main(String[] args) < LocalDate from = LocalDate.of(2020, 5, 4); LocalDate to = LocalDate.of(2020, 10, 10); Period period = Period.between(from, to); System.out.print(period.getYears() + " years,"); System.out.print(period.getMonths() + " months,"); System.out.print(period.getDays() + " days"); >>

2. Продолжительность

package com.mkyong.java8; import java.time.Duration; import java.time.LocalDateTime; public class JavaLocalDateTime < public static void main(String[] args) < LocalDateTime from = LocalDateTime.of(2020, 10, 4, 10, 20, 55); LocalDateTime to = LocalDateTime.of(2020, 10, 10, 10, 21, 1); Duration duration = Duration.between(from, to); // days between from and to System.out.println(duration.toDays() + " days"); // hours between from and to System.out.println(duration.toHours() + " hours"); // minutes between from and to System.out.println(duration.toMinutes() + " minutes"); // seconds between from and to System.out.println(duration.toSeconds() + " seconds"); System.out.println(duration.getSeconds() + " seconds"); >>
6 days 144 hours 8640 minutes 518406 seconds 518406 seconds

3. Хроноблок

package com.mkyong.java8; import java.time.LocalDateTime; import java.time.temporal.ChronoUnit; public class JavaChronoUnit < public static void main(String[] args) < LocalDateTime from = LocalDateTime.of(2020, 10, 4, 10, 20, 55); LocalDateTime to = LocalDateTime.of(2020, 11, 10, 10, 21, 1); long years = ChronoUnit.YEARS.between(from, to); long months = ChronoUnit.MONTHS.between(from, to); long weeks = ChronoUnit.WEEKS.between(from, to); long days = ChronoUnit.DAYS.between(from, to); long hours = ChronoUnit.HOURS.between(from, to); long minutes = ChronoUnit.MINUTES.between(from, to); long seconds = ChronoUnit.SECONDS.between(from, to); long milliseconds = ChronoUnit.MILLIS.between(from, to); long nano = ChronoUnit.NANOS.between(from, to); System.out.println(years + " years"); System.out.println(months + " months"); System.out.println(weeks + " weeks"); System.out.println(days + " days"); System.out.println(hours + " hours"); System.out.println(minutes + " minutes"); System.out.println(seconds + " seconds"); System.out.println(milliseconds + " milliseconds"); System.out.println(nano + " nano"); >>
0 years 1 months 5 weeks 37 days 888 hours 53280 minutes 3196806 seconds 3196806000 milliseconds 3196806000000000 nano

Рекомендации

Источник

Читайте также:  Python self request files

How to calculate days between two dates in Java

In my previous article, I wrote about adding days to an instance of a date in Java. In this article, you will learn how to calculate the duration between two dates using Java 8 new date and time API and the legacy Date API.

Java 8 introduced a whole new date and time API (classes in the java.time.* package) to fix the shortcomings of the legacy Date and Calendar API. The new API provides several utility methods for date and time manipulations. You can use utility classes like Period , Duration , and ChronoUnit to calculate the difference between two instances of the new date and time API class.

The LocalDate class presents the date without time in ISO-8601 format ( yyyy-MM-dd ). Unlike the legacy Date class, it doesn’t store any timezone information. The following example demonstrates how you can use the Duration class to calculate the number of days between two LocalDate instances:

// create date instances LocalDate localDate1 = LocalDate.parse("2019-12-31"); LocalDate localDate2 = LocalDate.parse("2020-01-08"); // calculate difference long days = Duration.between(localDate1.atStartOfDay(), localDate2.atStartOfDay()).toDays(); // print days System.out.println("Days between " + localDate1 + " and " + localDate2 + ": " + days); 
Days between 2019-12-31 and 2020-01-08: 8 

The new date and time API also provides ChronoUnit enum class to represent individual date-time units such as days, months, years, hours, seconds, etc. Each unit provides an implementation for a method called between() to calculate the amount of time between two temporal objects for that specific unit. Let us look at the below example that calculates the days and months between two LocalDate instances using ChronoUnit :

// create date instances LocalDate pastDate = LocalDate.of(2019, Month.AUGUST, 15); LocalDate now = LocalDate.now(); // calculate difference long months = ChronoUnit.MONTHS.between(pastDate, now); long days = ChronoUnit.DAYS.between(pastDate, now); // print days & months System.out.println("Months between " + pastDate + " and " + now + ": " + months); System.out.println("Days between " + pastDate + " and " + now + ": " + days); 
Months between 2019-08-15 and 2020-01-10: 4 Days between 2019-08-15 and 2020-01-10: 148 

A LocalTime represents time without date in ISO-8601 format ( HH:mm:ss.SSS ). To calculate the difference between two instances of LocalTime , you need to use the Duration class that represents the duration between two times. Here is an example:

// create time instances LocalTime firstTime = LocalTime.of(11, 30); LocalTime secondTime = LocalTime.of(22, 35); // calculate the difference between times Duration duration = Duration.between(firstTime, secondTime); // print the difference System.out.println("Hours between " + firstTime + " and " + secondTime + ": " + duration.toHours()); System.out.println("Minutes between " + firstTime + " and " + secondTime + ": " + duration.toMinutes()); 
Hours between 11:30 and 22:35: 11 Minutes between 11:30 and 22:35: 665 

Alternatively, you can also use ChronoUnit to calculate the difference between two LocalDate in terms of specific time units as shown below:

// create time instances LocalTime firstTime = LocalTime.of(10, 15, 45); LocalTime secondTime = LocalTime.of(14, 50, 15); // calculate the difference between times long hours = ChronoUnit.HOURS.between(firstTime, secondTime); long minutes = ChronoUnit.MINUTES.between(firstTime, secondTime); long seconds = ChronoUnit.SECONDS.between(firstTime, secondTime); // print the difference System.out.println("Hours between " + firstTime + " and " + secondTime + ": " + hours); System.out.println("Minutes between " + firstTime + " and " + secondTime + ": " + minutes); System.out.println("Seconds between " + firstTime + " and " + secondTime + ": " + seconds); 
Hours between 10:15:45 and 14:50:15: 4 Minutes between 10:15:45 and 14:50:15: 274 Seconds between 10:15:45 and 14:50:15: 16470 

The LocalDateTime class represents both date and time without timezone in ISO-8601 format ( yyyy-MM-ddTHH:mm:ss.SSS ). The following example demonstrates how you can calculate the difference between two LocalDateTime instances of terms of days using Duration :

// create date time instances LocalDateTime dateTime1 = LocalDateTime.parse("2018-08-02T15:14"); LocalDateTime dateTime2 = LocalDateTime.parse("2019-02-14T12:45"); // calculate difference Duration duration = Duration.between(dateTime1, dateTime2); // print the difference System.out.println("Days between " + dateTime1 + " and " + dateTime2 + ": " + duration.toDays()); System.out.println("Hours between " + dateTime1 + " and " + dateTime2 + ": " + duration.toHours()); 
Days between 2018-08-02T15:14 and 2019-02-14T12:45: 195 Hours between 2018-08-02T15:14 and 2019-02-14T12:45: 4701 

Let us look at another example that uses ChronoUnit to find out the difference between two LocalDateTime objects:

// create date time instances LocalDateTime pastDateTime = LocalDateTime.of(2019, Month.NOVEMBER, 12, 10, 40); LocalDateTime now = LocalDateTime.now(); // calculate difference long months = ChronoUnit.MONTHS.between(pastDateTime, now); long days = ChronoUnit.DAYS.between(pastDateTime, now); // print days & months System.out.println("Months between " + pastDateTime + " and " + now + ": " + months); System.out.println("Days between " + pastDateTime + " and " + now + ": " + days); 
Months between 2019-11-12T10:40 and 2020-01-10T18:36:48.356: 1 Days between 2019-11-12T10:40 and 2020-01-10T18:36:48.356: 59 

A ZonedDateTime represents a date and time with a timezone in ISO-8601 format (e.g 2019-05-15T10:15:30+01:00[Europe/Paris] ). It deals with timezone-specific dates and times in Java 8 and higher. Here is an example that shows how you can measure the difference between two ZonedDateTime instances:

// create date time instances ZonedDateTime dateTime1 = ZonedDateTime.parse("2019-05-15T10:15:30+01:00[Europe/Paris]"); ZonedDateTime dateTime2 = ZonedDateTime.parse("2020-01-05T12:00:33+05:00[Asia/Karachi]"); // calculate difference Duration duration = Duration.between(dateTime1, dateTime2); // print the difference System.out.println("Days between " + dateTime1 + " and " + dateTime2 + ": " + duration.toDays()); System.out.println("Hours between " + dateTime1 + " and " + dateTime2 + ": " + duration.toHours()); 

As you can see above, we have created two ZonedDateTime instances in different time zones. The great thing about the new date and time API is that it will automatically calculate the timezone differences, as shown in the below output:

Days between 2019-05-15T10:15:30+02:00[Europe/Paris] and 2020-01-05T12:00:33+05:00[Asia/Karachi]: 234 Hours between 2019-05-15T10:15:30+02:00[Europe/Paris] and 2020-01-05T12:00:33+05:00[Asia/Karachi]: 5638 

The same date and time difference calculations can be performed through the ChronoUnit enum class as shown below:

// create date time instances ZonedDateTime pastDateTime = ZonedDateTime.of(2018,2, 18, 10, 30, 0, 0, ZoneId.of("Europe/Paris")); ZonedDateTime now = ZonedDateTime.now(ZoneId.systemDefault()); // calculate difference long months = ChronoUnit.MONTHS.between(pastDateTime, now); long days = ChronoUnit.DAYS.between(pastDateTime, now); // print days & months System.out.println("Months between " + pastDateTime + " and " + now + ": " + months); System.out.println("Days between " + pastDateTime + " and " + now + ": " + days); 
Months between 2018-02-18T10:30+01:00[Europe/Paris] and 2020-01-10T18:48:38.368+05:00[Asia/Karachi]: 22 Days between 2018-02-18T10:30+01:00[Europe/Paris] and 2020-01-10T18:48:38.368+05:00[Asia/Karachi]: 691 

Finally, the last new date and time API class we’ll discuss is OffsetDateTime . It represents a date and time with an offset from UTC/Greenwich in the ISO-8601 format (e.g., 2018-12-30T23:15:30+03:30 ). The following example shows how you can measure the difference between two OffsetDateTime instances:

// create date time instances OffsetDateTime dateTime1 = OffsetDateTime.parse("2018-12-30T23:15:30+03:30"); OffsetDateTime dateTime2 = OffsetDateTime.parse("2019-07-17T05:15:30-05:00"); // calculate difference Duration duration = Duration.between(dateTime1, dateTime2); // print the difference System.out.println("Days between " + dateTime1 + " and " + dateTime2 + ": " + duration.toDays()); System.out.println("Hours between " + dateTime1 + " and " + dateTime2 + ": " + duration.toHours()); 
Days between 2018-12-30T23:15:30+03:30 and 2019-07-17T05:15:30-05:00: 198 Hours between 2018-12-30T23:15:30+03:30 and 2019-07-17T05:15:30-05:00: 4766 

Another way to find out the difference between two OffsetDateTime is using the ChronoUnit enum class:

// create date time instances OffsetDateTime pastDateTime = OffsetDateTime.of(2019, 8, 25, 15, 40, 0, 0, ZoneOffset.of("+03:00")); OffsetDateTime now = OffsetDateTime.now(ZoneOffset.systemDefault()); // calculate difference long months = ChronoUnit.MONTHS.between(pastDateTime, now); long days = ChronoUnit.DAYS.between(pastDateTime, now); // print days & months System.out.println("Months between " + pastDateTime + " and " + now + ": " + months); System.out.println("Days between " + pastDateTime + " and " + now + ": " + days); 
Months between 2019-08-25T15:40+03:00 and 2020-01-10T18:56:30.484+05:00: 4 Days between 2019-08-25T15:40+03:00 and 2020-01-10T18:56:30.484+05:00: 138 

Before Java 8, java.util.Date and java.util.Calendar classes were used for handling dates and times. These old classes have many flaws that were fixed with the release of the new date and time API in Java 8. Since these classes are still actively used in old projects, it is worth knowing how to calculate the difference between two Date objects. The following example demonstrates how you can parse strings to create two instances of Date and then find the difference between them in milliseconds. Later, we convert the milliseconds into days and print the result on the console:

// date pattern SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); // parse string to date Date dateBefore = formatter.parse("2019-12-05"); Date dateAfter = formatter.parse("2020-01-05"); // calculate difference in milliseconds long ms = Math.abs(dateAfter.getTime() - dateBefore.getTime()); // convert milliseconds to days long days = TimeUnit.DAYS.convert(ms, TimeUnit.MILLISECONDS); // print days System.out.println("Days Between Dates: " + days); 

In this article, we looked at different ways to calculate the difference between two dates in Java. We discussed Java 8 new date and time API classes that provide utility methods to calculate the number of days between the given date and time instances. The new date and time API introduced in Java 8 provides a wide range of classes that have simplified working with dates and times. Read next: How to get current date and time in Java ✌️ Like this article? Follow me on Twitter and LinkedIn. You can also subscribe to RSS Feed.

You might also like.

Источник

Оцените статью