TimeUtil.java Source code

Java tutorial

Introduction

Here is the source code for TimeUtil.java

Source

/**
 * Copyright (c) 2006 Richard Rodgers
 *
 * 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.monad.homerun.util;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * TimeUtil is a utility class with static methods to convert times in various
 * formats into other formats
 */

public class TimeUtil {
    private static final int MINS_PER_DAY = 60 * 24;
    private static final long MS_PER_DAY = 1000 * 60 * MINS_PER_DAY;

    private static final int SEC = 1000;
    private static final int MIN = SEC * 60;
    private static final int HOUR = MIN * 60;
    private static final int DAY = HOUR * 24;
    private static final long WEEK = DAY * 7;
    private static final long YEAR = WEEK * 52;

    private static final long[] buckets = { YEAR, WEEK, DAY, HOUR, MIN, SEC };
    private static final String[] bucketNames = { "year", "week", "day", "hour", "minute", "second" };

    private static GregorianCalendar statFmtCal = new GregorianCalendar();

    private static final String ts24Pat = "H:mm:ss yy-MM-dd";

    // convert time to a sliding window format
    public static String windowFormat(long msecs) {
        Date tDate = new Date(msecs);
        long now = System.currentTimeMillis();
        long diff = now - msecs;
        // start with the last 24 hours
        long comp = MS_PER_DAY;
        // start comparing
        if (diff < comp) {
            // it's within the last 24 hours - just return the time
            DateFormat formatter = DateFormat.getTimeInstance(DateFormat.SHORT);
            return formatter.format(tDate);
        }
        // now expand to another day
        comp += MS_PER_DAY;
        if (diff < comp) {
            // it's within the last 48 hours - just return the time
            DateFormat formatter = DateFormat.getTimeInstance(DateFormat.SHORT);
            return "Yesterday " + formatter.format(tDate);
        }
        // now up it to a week
        comp += 5 * MS_PER_DAY;
        if (diff < comp) {
            // return the day of the week and the time
            // get time to be formatted into a calendar
            GregorianCalendar tCal = new GregorianCalendar();
            tCal.setTime(tDate);
            SimpleDateFormat formatter = new SimpleDateFormat("EEEE");
            return formatter.format(tDate);
        }
        // just return full date
        DateFormat formatter = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
        return formatter.format(tDate);
    }
}