3
===========================================================
Date_Time_from          | Date_Time_to
===========================================================
2018-06-16 02:00:00     |2018-06-18 02:00:00
2018-06-16 03:00:00     |2018-06-18 03:00:00
2018-06-16 04:00:00     |2018-06-18 04:00:00
2018-06-16 05:00:00     |2018-06-18 05:00:00
2018-06-16 06:00:00     |2018-06-18 06:00:00
==========================================================

I am using LocalDateTime but instead of getting the difference of 2 DateTime, it only calculated the time. It did not count the day.

Here's the code:

LocalDateTime fromdate = LocalDateTime.of(orderYear,orderMonth,orderDay,orderHour,orderMinute,orderSeconds);
LocalDateTime todate = LocalDateTime.of(deliverYear, deliverMonth, deliverDay,deliverHour,deliverMinute,deliverSeconds);
int a = fromdate.get(ChronoField.MINUTE_OF_DAY)-todate.get(ChronoField.MINUTE_OF_DAY);

The output of above code is zero since, in 2 hours in date_time_from equivalent to 120 mins subtracted to 2 hours in date_time_to.

Is there another way to get the total minutes and include to compute the date?

N. Labrahmi
  • 657
  • 4
  • 23
AyukNayr
  • 386
  • 2
  • 21
  • Have you tried joda time? – Raj Jun 16 '18 at 01:32
  • 1
    Use `Duration`!!!!! Possible duplicate [How to find difference between two Joda-Time DateTimes in minutes](https://stackoverflow.com/questions/12851934/how-to-find-difference-between-two-joda-time-datetimes-in-minutes/12852021#12852021) – MadProgrammer Jun 16 '18 at 01:35
  • 1
    @emaillenin JodaTime is in "maintenance" mode because of the inclusion of the new date/time API in Java 8+ – MadProgrammer Jun 16 '18 at 01:37

3 Answers3

3
LocalDateTime fromdate = LocalDateTime.of(orderYear,orderMonth,orderDay,orderHour,orderMinute,orderSeconds);
LocalDateTime todate = LocalDateTime.of(deliverYear, deliverMonth, deliverDay,deliverHour,deliverMinute,deliverSeconds);
Duration difference = Duration.between(fromdate, todate);


Then you can format it using something similar to...

long hours = difference.toHours();
long mins = difference.minusHours(hours).toMinutes();

// Or if you're lucky enough to be using Java 9+
//String formatted = String.format("%dhrs %02dmins", duration.toHours(), duration.toMinutesPart());
String formatted = String.format("%dhrs %02dmins", hours, mins);


And just to prove the point...

import java.time.Duration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

public class Test {

    public static void main(String[] args) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH);
        List<LocalDateTime> from = new ArrayList<>(5);
        List<LocalDateTime> to = new ArrayList<>(5);

        from.add(LocalDateTime.parse("2018-06-16 02:00:00", formatter));
        from.add(LocalDateTime.parse("2018-06-16 03:00:00", formatter));
        from.add(LocalDateTime.parse("2018-06-16 04:00:00", formatter));
        from.add(LocalDateTime.parse("2018-06-16 05:00:00", formatter));
        from.add(LocalDateTime.parse("2018-06-16 06:00:00", formatter));

        to.add(LocalDateTime.parse("2018-06-18 02:00:00", formatter));
        to.add(LocalDateTime.parse("2018-06-18 03:00:00", formatter));
        to.add(LocalDateTime.parse("2018-06-18 04:00:00", formatter));
        to.add(LocalDateTime.parse("2018-06-18 05:00:00", formatter));
        to.add(LocalDateTime.parse("2018-06-18 06:00:00", formatter));

        for (int index = 0; index < from.size(); index++) {
            LocalDateTime fromDate = from.get(index);
            LocalDateTime toDate = to.get(index);
            String difference = formatDurationBetween(fromDate, toDate);
            System.out.println(fromDate.format(formatter) + " - " + toDate.format(formatter) + " = " + difference);
        }
    }

    public static String formatDurationBetween(LocalDateTime from, LocalDateTime to) {
        Duration difference = Duration.between(from, to);

        long days = difference.toDays();
        difference = difference.minusDays(days);
        long hours = difference.toHours();
        long mins = difference.minusHours(hours).toMinutes();

        return String.format("%dd %dh %02dm", days, hours, mins);
    }

}


Outputs...

2018-06-16 02:00:00 - 2018-06-18 02:00:00 = 2d 0h 00m
2018-06-16 03:00:00 - 2018-06-18 03:00:00 = 2d 0h 00m
2018-06-16 04:00:00 - 2018-06-18 04:00:00 = 2d 0h 00m
2018-06-16 05:00:00 - 2018-06-18 05:00:00 = 2d 0h 00m
2018-06-16 06:00:00 - 2018-06-18 06:00:00 = 2d 0h 00m


You can of course make your own formatting algorithm based on your needs

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • 1
    Okay, so, based on the above, what do you think you might need to change (hint `Duration` has `toMinutes`, but what in the above example is "modifying" the `Duration`)? – MadProgrammer Jun 16 '18 at 02:02
3

Is that what you asking for,

LocalDateTime fromDate = LocalDateTime.of(2018, 6, 16, 2, 0, 0);
LocalDateTime toDate = LocalDateTime.of(2018, 6, 18, 2, 0, 0);
long minutes = Duration.between(fromDate, toDate).toMinutes();

Update

As per the below comment leading zeros were removed since they are error prone.

Ravindra Ranwala
  • 20,744
  • 6
  • 45
  • 63
  • 1
    This answer seems to be to the point since minutes were asked for. As an aside, while `00` works for specifying 0 minutes and 0 seconds, you’ll get into trouble once you try `08` for 8 minutes or `09` for 9 seconds, since Java perceives numbers with a prefixed 0 as [octal numbers](https://en.wikipedia.org/wiki/Octal). So it’s better to avoid that (unless you have a reason for intending octal numbers, of course). – Ole V.V. Jun 16 '18 at 05:39
  • @OleV.V. yeah, because god forbid we make people think for themselves or do due-dulligance to solve their problems – MadProgrammer Jun 16 '18 at 08:36
2

I need the output to be in mins only. Thanks

There are two great answers already so this is just to add that while the Duration class is the most general and flexible for representing the difference between two date-times, there is a simpler option, ChronoUnit.MINUTES:

    LocalDateTime fromdate = LocalDateTime.of(2018, Month.JUNE, 16, 2, 0, 0);
    LocalDateTime todate = LocalDateTime.of(2018, Month.JUNE, 18, 2, 0, 0);
    long diffInMinutes = ChronoUnit.MINUTES.between(fromdate, todate);
    System.out.println(diffInMinutes);

Output is

2880

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161