Search code examples
javaandroiddatetimemilliseconds

How to get the time of the day in milliseconds?


I want to get the time of a day in milliseconds, I do not this day to have any specific date, just a time. I made something, thought it worked, but then went debugging and concluded that it doesn't work how I want it to.

I want to use this to check if the current time is between both my specified startTime and endTime.

    long startTime = settings.getLong("startTime", 0);
    long endTime = settings.getLong("endTime", 0);

    if ((currentTime.getMillis() >= startTime)
            && (currentTime.getMillis() <= endTime)) {
            //Do stuff here

 }

How I am setting the time of the propeties startTime and endTime:

                        Calendar startTime = Calendar.getInstance();
                        startTime.set(Calendar.HOUR_OF_DAY, 16);
                        startTime.set(Calendar.MINUTE, 00);
                        editor.putLong("startTime",
                                startTime.getTimeInMillis());

                        Calendar endTime = Calendar.getInstance();
                        endTime.set(Calendar.HOUR_OF_DAY, 16);
                        endTime.set(Calendar.MINUTE, 00);
                        endTime.add(Calendar.HOUR_OF_DAY, 11);
                        editor.putLong("endTime",
                                endTime.getTimeInMillis());
                        editor.commit();

However this will mean that both startTimeand endTime will have this a specific date attached to it.

I hope I explained it well, any help is appreciated!


Solution

  • Avoid Milliseconds

    No need to mess with milliseconds for your purpose. Using milliseconds for date-time is confusing and error-prone.

    What you need is a decent date-time library rather than the notoriously troublesome bundled java.util.Date & .Calendar classes.

    Joda-Time

    If you are certain you want to ignore dates and ignore time zones, here's some example code using the LocalTime class offered by the third-party free-of-cost Joda-Time library.

    LocalTime start = new LocalTime( 10, 0, 0 );
    LocalTime stop = new LocalTime( 14, 30, 0 );
    LocalTime target = LocalTime.now();
    boolean isNowInSpan = !( ( target.isBefore( target ) ) | ( target.isAfter( stop ) ) );
    

    Adjust that last line according to your business logic needs. You might want:

    • The beginning and ending are inclusive
    • The beginning and ending are exclusive
    • "Half-Open" where the beginning is inclusive and the ending is exclusive
      (usually best for date-time work)

    Dump to console…

    System.out.println( "start: " + start );
    System.out.println( "stop: " + stop );
    System.out.println( "target: " + target );
    System.out.println( "isNowInSpan: " + isNowInSpan );
    

    When run…

    start: 10:00:00.000
    stop: 14:30:00.000
    target: 23:49:37.779
    isNowInSpan: false
    

    Another Example

    Time-of-day-only is not usually the right way to go. When new to date-time work, a naïve programmer may at first think that time-only simplifies things. On the contrary, this example shows how spinning around the clock creates complications. Using date+time+timeZone is usually the best approach in the long run.

    LocalTime now = LocalTime.now();
    LocalTime start = new LocalTime( 13, 0, 0, 0 );
    LocalTime stop = start.plusHours( 11 );
    
    System.out.println( "now: " + now );
    System.out.println( "start: " + start );
    System.out.println( "stop: " + stop );
    
    if ( now.isAfter( start ) ) {
        System.out.println( "After start" );
    }
    
    if ( now.isBefore( stop ) ) {
        System.out.println( "Before stop" );
    }
    

    When run…

    now: 14:00:32.496
    start: 13:00:00.000
    stop: 00:00:00.000
    After start
    

    java.time

    Java 8 brings the new java.time package, inspired by Joda-Time, defined by JSR 310.

    In java.time, you will find a LocalTime class similar to the one in Joda-Time.