1

I have to calculate the time until the next day at ~ 2:00 AM

Following code seems to work fine

public static long millisTillNextDay() {
    Date now = new Date();
    Calendar cal = Calendar.getInstance();
    cal.set(Calendar.DAY_OF_MONTH, cal.get(Calendar.DAY_OF_MONTH) + 1);
    cal.set(Calendar.HOUR, 3);
    cal.set(Calendar.MINUTE, 0);
    cal.set(Calendar.SECOND, 0);
    return cal.getTimeInMillis() - now.getTime();
}

However, a few days ago it happened that this method calculated the time until 02:00 PM instead and I do not know why. Because of this I want to create some test scenarios to simulate different values for Calendar.getInstance().

Andy
  • 17,423
  • 9
  • 52
  • 69
stg
  • 2,757
  • 2
  • 28
  • 55
  • there might be a timezone change meanwhile on the machine this code run. – Juvanis Jan 10 '14 at 11:07
  • I would *strongly* advise you to use `java.time.*` instead of `java.util.Calendar`/`java.util.Date` if you *possibly* can. You've found *one* time-related bug in your code that probably wouldn't have happened using a better API... who knows how many more there may be? – Jon Skeet Jul 23 '17 at 18:32

5 Answers5

4

You must set the HOUR_OF_DAY field instead of HOUR. The former doesn't mind the AM/PM distinction and always sets the absolute hour of the day.

Quotes from the documentation:

HOUR is used for the 12-hour clock (0 - 11).

HOUR_OF_DAY is used for the 24-hour clock.

Community
  • 1
  • 1
Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
  • Do you mean cal.set(Calendar.HOUR, 3); will set the hour to 2 PM if it is for example 6 PM and it will set it to 2 AM if it is 6 AM? – stg Jan 10 '14 at 11:10
  • @stg Exactly. If you don't touch the `AM_PM` field, that is how the behavior is specified. – Marko Topolnik Jan 10 '14 at 11:10
  • Thank you very much, I did not know this and will check this immediately. – stg Jan 10 '14 at 11:11
  • I was able to verify this behavior, your answer solved my problem. Thanks again. – stg Jan 10 '14 at 11:19
3

For testing purposes, you could refactor the method to:

//the original method
public static long millisTillNextDay() {
    return millisTillNextDay(new Date());
}

//a new method that is testable
public static long millisTillNextDay(Date date) {
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    cal.set(Calendar.DAY_OF_MONTH, cal.get(Calendar.DAY_OF_MONTH) + 1);
    cal.set(Calendar.HOUR, 3);
    cal.set(Calendar.MINUTE, 0);
    cal.set(Calendar.SECOND, 0);
    return cal.getTimeInMillis() - date.getTime();
}

It now becomes easy to test the second method and pass in whatever dates you like.

assylias
  • 321,522
  • 82
  • 660
  • 783
1

Just use one of the methods for Calendar that lets you specify the time.

For example:

long l = System.currentTimeMillis();
long limit = l + 24*60*60*1000;

for (long l = System.currentTimeMillis();l<=limit;l+=60*1000) {
    Calendar cal = new Calendar();
    cal.setTimeInMillis(l);
    // Do test
}
Tim B
  • 40,716
  • 16
  • 83
  • 128
1

If you're looking for a way to test, I suggest to change the method signature to accept now and then write a TestCase with JUnit injecting different dates. Like

public void MillisTillNextDayTest extends TestCase {
    public void testNewYearEveMidnight() {
        Date midnight = new Date(2014, 0, 1, 0, 0, 0);
        assertEquals(2*3600*1000, MyUtils.millisTillNextDay(midnight));
    }

    public void test3am() {
        Date now = new Date(2014, 0, 1, 3, 0, 0);
        assertEquals(23*3600*1000, MyUtils.millisTillNextDay(now));
    }

    public void test1pm() {
        Date now = new Date(2014, 0, 1, 13, 0, 0);
        assertEquals(13*3600*1000, MyUtils.millisTillNextDay(now));
    }

    public void testLeapYear() {
        Date now = new Date(2012, 1, 28, 13, 0, 0);
        assertEquals(13*3600*1000, MyUtils.millisTillNextDay(now));
    }
}
Raffaele Rossi
  • 1,079
  • 5
  • 11
1

tl;dr

ZoneId z = ZoneId.of( "Pacific/Auckland" ) ;
ZonedDateTime now = ZonedDateTime.now( z ) ;
Duration d = Duration.between( 
    now , 
    ZonedDateTime.of( 
        now.toLocalDate().plusDays(1) , 
        LocalTime.of( 2 , 0 )
        z 
    ) ;
).toString()

PT8H32M57.264S

Time-Zone

Other answers are correct except for one big flaw: time zone. All the code shown defaults to the JVM's current default time zone. When deploying code to another machine, you could have unexpected behavior. Daylight Saving Time (DST) or other anomalies tied to a time zone could alter the calculation.

java.time

The modern approach uses the java.time classes.

A time zone is crucial in determining a date. For any given moment, the date varies around the globe by zone. For example, a few minutes after midnight in Paris France is a new day while still “yesterday” in Montréal Québec.

Specify a proper time zone name in the format of continent/region, such as America/Montreal, Africa/Casablanca, or Pacific/Auckland. Never use the 3-4 letter abbreviation such as EST or IST as they are not true time zones, not standardized, and not even unique(!).

ZoneId z = ZoneId.of( "America/Montreal" );
ZonedDateTime now = ZonedDateTime.now( z );

We need to build the target date-time, tomorrow at 2 AM. The tomorrow's date as a LocalDate (date-only value) plus the target time as a LocalTime (time-of-day only value). The java.time classes use 24-hour clock by default, so we needn't worry about your 2 AM vs 2 PM problem. A 2 PM values would have an input of 14 hours, so no ambiguity.

LocalDate tomorrow = now.toLocalDate().plusDays( 1 ) ;
LocalTime targetTime = LocalTime.of( 2 , 0 ) ;  // 2 hours, zero minutes.

Caveat: If you happened to pick a particular date and time that is invalid, such as a Daylight Saving Time (DST) cutover, the ZonedDateTime class adjusts to fit. Read the class documentation to see if its algorithms for adjusting fit your needs/expectations. For example, on the date of the DST "Spring-forward" date, you would end up with 3 AM rather than 2 AM as the 02:00 hour does not exist on that date, only 01:00 & 03:00 hours exist. This will not affect your calculation of a duration. The space-time continuum did not jump, just our clock-on-the-wall representation jumped.

Put them together with the time zone to get 2 AM tomorrow.

ZonedDateTime target = ZonedDateTime.of( tomorrow , targetTime , z ) ;

Our ultimate goal it to capture the elapsed time between now and that target tomorrow. To represent a span-of-time unattached to the timeline, use the Duration or Period classes.

Duration d = Duration.between( now , target ) ;

Call toString to generate a String formatted according to the ISO 8601 standard, PnYnMnDTnHnMnS. The P marks the beginning, the T separates the years-months-days portion from the hours-minutes-seconds portion.

PT8H32M57.264S


About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.


Joda-Time

UPDATE: The Joda-Time project is now in maintenance mode, with the team advising migration to the java.time classes. See Tutorial by Oracle. This section here left intact for history.

Below is some example code using the Joda-Time 2.3 library.

The DateTime class knows its own time zone.

The method withTime creates a new (immutable) instance copied from the existing instance, but with the specified time of day.

Joda-Time has a few classes to represent a span of time: Period, Duration, Interval. The Period class by defaults renders a string based on the ISO 8601 Duration format of PnYnMnDTnHnMnS. You asked for milliseconds, so I calculate that in the example. See this question, Joda time, Period to total millis, for more detail.

Example Code

// © 2013 Basil Bourque. This source code may be used freely forever by anyone taking full responsibility for doing so.
// import org.joda.time.*;

// Better to specify a time zone explicitly rather than rely on default.
// Time Zone list… http://joda-time.sourceforge.net/timezones.html  (not quite up-to-date, read page for details)
DateTimeZone timeZone = DateTimeZone.forID( "Europe/Paris" );

DateTime now = new DateTime( timeZone );
DateTime tomorrowTwoInMorning = now.plusDays( 1 ).withTime( 2, 0, 0, 0 );

// Calculate the span of time between them.
Period period = new Period( now, tomorrowTwoInMorning );
long milliseconds = period.toStandardDuration().getMillis();

Dump to console…

System.out.println( "now: " + now );
System.out.println( "tomorrowTwoInMorning: " + tomorrowTwoInMorning );
System.out.println( "period: " + period );
System.out.println( "milliseconds: " + milliseconds );

When run…

now: 2014-01-11T00:51:38.059+01:00
tomorrowTwoInMorning: 2014-01-12T02:00:00.000+01:00
period: P1DT1H8M21.941S
milliseconds: 90501941
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154