Format time in milliseconds : Time « Development Class « Java






Format time in milliseconds

     

/**
 * 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.Calendar;
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";
  public static String stringSecsFormat(long msecs) {
    GregorianCalendar cal = new GregorianCalendar();
    StringBuffer sBuf = new StringBuffer(11);

    cal.setTime(new Date(msecs));

    int hour = cal.get(Calendar.HOUR);

    if (hour == 0)
      hour = 12;

    if (hour < 10)
      sBuf.append(" ");

    sBuf.append(Integer.toString(hour));
    sBuf.append(":");

    int minute = cal.get(Calendar.MINUTE);

    if (minute < 10)
      sBuf.append("0");

    sBuf.append(Integer.toString(minute));
    sBuf.append(":");

    int secs = cal.get(Calendar.SECOND);

    if (secs < 10) {
      sBuf.append("0");
    }
    sBuf.append(Integer.toString(secs));

    sBuf.append(" ");
    sBuf.append(cal.get(Calendar.AM_PM) == Calendar.AM ? "AM" : "PM");

    return (sBuf.toString());
  }




}

   
    
    
    
    
  








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.Determines if given times span at least an entire day
18.Converts a given time in milliseconds into a XMLGregorianCalendar object.
19.Time Distance
20.Time Formatter
21.Format time
22.A utility class for representing a span of time.
23.GmtCalendar is a useful class for working with times that are based using GMT time.
24.Time Period
25.Represents the TSTInfo strcture within a time-stamp token (RFC 3161).
26.SimpleTimer enables bounded and unbounded waits.
27.Takes a time in milliseconds and returns an hours, minutes and seconds representation.