1

I have a input date string in format "2021-07-15T05:00:00.527+05:30" I want date difference from current time (current time is in UTC).

DateTime inputDateTime = new DateTime(input, DateTimeZone.UTC);
DateTime now = new DateTime(DateTimeZone.UTC);
Days.daysBetween(now, inputDateTime).getDays();

If i convert the input date to UTC, This is yielding the wrong result. Is there any way to get the offset from input date and add it to current UTC date and then compare the Date?

EDIT:

Sorry for phrasing the question wrong. I am trying to find out is it Today or is it PreviousDay or isT it Tomorrow for the given input date which as offset with respect to utc timezone.

In server when time is 2021-07-14T23:00:00.527Z, for above input date, it should be taken as Today (i.e. 0). If in server time is 2021-07-14T13:00:00.527Z, for same input dat, it should be Tomorrow (i.e. 1).

Edit2:

I did try out converting both to localDate, Days.daysBetween(now.toLocalDate(), inputDateTime.toLocalDate()).getDays() but when UTC time is in previous date 2021-07-14T23:00:00.527Z, and the given date 2021-07-15T05:00:00.527+05:30, yields 1 but i am expecting it to be 0 `

  • Which result had you expected and which wrong result is it giving instead? – Ole V.V. Jul 15 '21 at 09:55
  • I cannot reproduce. When running your code just now I get `0`, which seems to me to be correct. – Ole V.V. Jul 15 '21 at 09:58
  • Sorry, looks like phrased the question incorrectly. I am trying to check isToday or isTomorrow for given input string. i think i should not use Days.daysBetween for this. – user2668552 Jul 15 '21 at 10:40
  • You will want to convert the `DateTime` to `LocalDate`. Find the `LocalDate` of today in UTC and compare. And add 1 day to today to get tomorrow and compare again. – Ole V.V. Jul 15 '21 at 10:55
  • Thanks @OleV.V. Looks like i am making a mistake when converting given date to local date. Any suggestions on Edit2 will really help. – user2668552 Jul 15 '21 at 11:06

2 Answers2

4

I am trying to check isToday or isTomorrow for given input string.

I suggest:

    String input = "2021-07-19T05:00:00.527+05:30";
    
    DateTime inputDateTime = new DateTime(input, DateTimeZone.UTC);
    LocalDate inputDate = inputDateTime.toLocalDate();
    LocalDate today = LocalDate.now(DateTimeZone.UTC);
    boolean isToday = inputDate.equals(today);
    LocalDate tomorrow = today.plusDays(1);
    boolean isTomorrow = inputDate.equals(tomorrow);
    
    System.out.format("Is today? + %b; is tomorrow? %b.%n", isToday, isTomorrow);

Output when run just now — approximately 18:32 UTC on 18th July:

Is today? + true; is tomorrow? false.

I took the input string 2021-07-19T05:00:00.527+05:30 as example. It equals 2021-07-18T23:30:00.527Z (UTC). So the date I am comparing to today and tomorrow is 2021-07-18.

java.time

Bonus quote from the Joda-Time homepage:

Note that from Java SE 8 onwards, users are asked to migrate to java.time (JSR-310) - a core part of the JDK which replaces this project.

(Joda-Time - Home; boldface is original)

The corresponding java.time code is similar. Only the conversion from string to date and time is textually different. It makes explicit that an offset conversion is taking place, which I consider an advantage:

    OffsetDateTime inputDateTime = OffsetDateTime.parse(input)
            .withOffsetSameInstant(ZoneOffset.UTC);
    LocalDate inputDate = inputDateTime.toLocalDate();
    LocalDate today = LocalDate.now(ZoneOffset.UTC);
    boolean isToday = inputDate.equals(today);
    LocalDate tomorrow = today.plusDays(1);
    boolean isTomorrow = inputDate.equals(tomorrow);
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • Very clear answer! I suggest you also post the `java.time` solution along with the ways to use it in the old versions. – Arvind Kumar Avinash Jul 18 '21 at 19:10
  • Hi @OleV.V. Thaks. But one thing If this was ran at 18:29 or before on 18th July, I am expecting is Today to be false and is Tomorrow to be true for given input date. Because for the input date, it's not yet today untill 18:30 is crossed right? Or is my thinking wrong here. – user2668552 Jul 19 '21 at 19:58
  • That’s weird, @user2668552. Did you mean 18:29 in UTC or your own time zone? Obviously `LocalDate.now(DateTimeZone.UTC)` ought to give the same result before and after 18:30 UTC. A [mre], please? – Ole V.V. Jul 22 '21 at 17:09
  • 1
    I am running this in server which is in UTC. So when I convert the input date 2021-07-15T05:00:00.527+05:30 to UTC and get LocalDate it will be 2021-07-14. When the UTC time is before 18:30 on 2021-07-14, for user in input date timezone it is still tomorrow not today. Will share example in sometime. – user2668552 Jul 23 '21 at 18:16
1

I did this for now in my code using java.time. I took the offset from input date, added it to current utc time and then took the local dates of both and compared. Not really sure if this covers scenarios like day light saving.

    ZonedDateTime zonedScheduleDate = ZonedDateTime.parse(inputDate);
    ZoneId zone = zonedScheduleDate.getZone();
    ZonedDateTime instantAtUserTimezone = Instant.now().atZone(zone);
    return (int) DAYS.between(instantAtUserTimezone.toLocalDate(), zonedScheduleDate.toLocalDate());