Determines if given times span at least an entire day : Time « Development Class « Java

Determines if given times span at least an entire day


   Copyright 2007

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   See the License for the specific language governing permissions and
   limitations under the License.
//package com.gcalsync.util;

 * @author Thomas Oldervoll,
 * @author $Author$
 * @version $Rev: 40 $
 * @date $Date$
import java.util.Calendar;
import java.util.Date;

public class Util{
      * Determines if given times span at least an entire day
      * @param startTime starting time in ms since 1970 Jan 1
      * @param endTime ending time in ms since 1970 Jan 1
      * @returns <code>true</code>: starts and ends daily at
      *        midnight<br><code>false</code> otherwise
    public static boolean isAllDay(long startTime, long endTime)
      boolean startsMidnight = isMidnight(startTime);
      boolean endsMidnight = isMidnight(endTime);
      long timeDiff = (endTime - startTime)/1000/60/60;

      //daily if difference in hours is a multiple of 24
      boolean daily = (timeDiff >= 24) && ((timeDiff % 24) == 0);

      //starts and ends daily at 12:00am
      return (daily &&  startsMidnight && endsMidnight);
    public static boolean isMidnight(long time)
      Calendar calendar = Calendar.getInstance();
      calendar.setTime(new Date(time));
      return isMidnight(calendar);
      private static boolean isMidnight(Calendar calendar) {
          return (calendar.get(Calendar.HOUR_OF_DAY) == 0) &&
           (calendar.get(Calendar.MINUTE) == 0);



Related examples in the same category

1.Get Time From Date
2.ISO8601 Date Time Format
3.Time Format
4.Time Formatter
5.Returns time string
6.Returns the given date with the time values cleared
7.Convert the time to the midnight of the currently set date
8.Compare both times and dates
9.Tells you if the date part of a datetime is in a certain time range
10.Returns the given date with time set to the end of the day
11.Convert milliseconds to readable string
12.Determines whether or not a date has any time values (hour, minute, seconds or millisecondsReturns the given date with the time values cleared
13.Returns a formatted String from time
14.Time library
15.Elapsed time in hours/minutes/seconds
16.Sets the time on the same day to 00:00:00.000
17.Converts a given time in milliseconds into a XMLGregorianCalendar object.
18.Time Distance
19.Time Formatter
20.Format time
21.A utility class for representing a span of time.
22.GmtCalendar is a useful class for working with times that are based using GMT time.
23.Time Period
24.Represents the TSTInfo strcture within a time-stamp token (RFC 3161).
25.SimpleTimer enables bounded and unbounded waits.
26.Takes a time in milliseconds and returns an hours, minutes and seconds representation.
27.Format time in milliseconds