2

I'm using this library I just discovered which is supposedly less heavier than Joda time for android and I said what the heck, let's use it. But now I'm struggling to find any good examples on the web about how to use it, besides these two methods I have:

// ZonedDateTime contains timezone information at the end
// For example, 2011-12-03T10:15:30+01:00[Europe/Paris]
public static ZonedDateTime getDate(String dateString) {
    return ZonedDateTime.parse(dateString).withZoneSameInstant(ZoneId.of("UTC"));
}

public static String formatDate(String format, String dateString) {
    return DateTimeFormatter.ofPattern(format).format(getDate(dateString));
}

So how can I get the difference between two dates with this library?

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

1 Answers1

1

There are several options depending on what you require from the difference you obtain.

It’s easiest to find the difference measured in some time unit. Use ChronoUnit.between. For example:

    ZonedDateTime zdt1 = getDate("2011-12-03T10:15:30+01:00[Europe/Paris]");
    ZonedDateTime zdt2 = getDate("2017-11-23T23:43:45-05:00[America/New_York]");

    long diffYears = ChronoUnit.YEARS.between(zdt1, zdt2);
    System.out.println("Difference is " + diffYears + " years");

    long diffMilliseconds = ChronoUnit.MILLIS.between(zdt1, zdt2);
    System.out.println("Difference is " + diffMilliseconds + " ms");

This prints:

Difference is 5 years
Difference is 188594895000 ms

I am using your getDate method, so the format required is that of ZonedDateTime (modified from ISO 8601), for example 2011-12-03T10:15:30+01:00[Europe/Paris]. Seconds and fraction of second are optional, as is time zone ID in square brackets.

BTW you don’t need to convert to UTC before finding the difference. You will get the same result even if you leave out that conversion.

You may also get the difference in years, months and days. The Period class can give you this, but it cannot handle time of day, so convert to LocalDate first:

    Period diff = Period.between(zdt1.toLocalDate(), zdt2.toLocalDate());
    System.out.println("Difference is " + diff);

Difference is P5Y11M21D

The output means a period of 5 years 11 months 21 days. The syntax may feel a little strange at first, but is straightforward. It is defined by the ISO 8601 standard. In this case the time zone matters since it is never the same date in all time zones.

To get the difference in hours, minutes and seconds use the Duration class (I am introducing a new time since using Duration for nearly 6 years would be too atypical (though possible)).

    ZonedDateTime zdt3 = getDate("2017-11-24T18:45:00+01:00[Europe/Copenhagen]");
    Duration diff = Duration.between(zdt2, zdt3);
    System.out.println("Difference is " + diff);

Difference is PT13H1M15S

A period of 13 hours 1 minute 15 seconds. The T that you already know from 2011-12-03T10:15:30+01:00[Europe/Paris] here too separates the date part from the time part so you know that in this case 1M means 1 minute, not 1 month.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • Wow thank you so much!! Very appreciated it.And if you don't mind telling me the correct date format to pass my dates to the getDate() method is something like this; yyyy-MM-dd'T'HH:mm:ss ? –  Aug 17 '18 at 02:49
  • 1
    I found it: "yyyy-MM-dd'T'HH:mm:ssXXX" –  Aug 18 '18 at 05:49
  • And btw my friend, how DO i convert to UTC after i get the difference? –  Aug 18 '18 at 05:50
  • Sorry if I was unclear; I meant if the difference is what you need, you don’t need to convert to UTC at all (that is, you may optionally leave out `.withZoneSameInstant(ZoneId.of("UTC"))` from your method). – Ole V.V. Aug 18 '18 at 07:00
  • i was trying your code its showing call requires api level 26.but i am using min level 17 – Syed Danish Haider Jan 07 '19 at 09:11
  • @SyedDanishHaider The asker is using [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP). You can too! It’s the Android adaption of the backport of java.time, and from what I’ve understood it’s what Android developers generally use on API levels under 26. See more [in this question: How to use ThreeTenABP in Android Project](https://stackoverflow.com/questions/38922754/how-to-use-threetenabp-in-android-project). – Ole V.V. Jan 07 '19 at 10:15