Util.java Source code

Java tutorial

Introduction

Here is the source code for Util.java

Source

/*
   Copyright 2007 batcage@gmail.com
    
   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
    
   http://www.apache.org/licenses/LICENSE-2.0
    
   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
//package com.gcalsync.util;

/**
 *
 * @author Thomas Oldervoll, thomas@zenior.no
 * @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);
    }

}