Time Formatter : Time « Development Class « Java






Time Formatter

     
/**
 * This file is distributed under the GPL
 * $Id: TimeFormatter.java 1865 2009-08-10 18:29:27Z scotta $
 */

//package net.bnubot.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

/**
 * @author scotta
 */
public class TimeFormatter {
  public static final long SECOND = 1000;
  public static final long MINUTE = 60 * SECOND;
  public static final long HOUR = 60 * MINUTE;
  public static final long DAY = 24 * HOUR;
  public static final long WEEK = 7 * DAY;

  public static TimeZone timeZone = TimeZone.getDefault();
  public static String tsFormat = "%1$tH:%1$tM:%1$tS.%1$tL";

  public static Calendar getCalendar() {
    return Calendar.getInstance(timeZone);
  }

  /**
   * @return a nicely formatted timestamp
   */
  public static String getTimestamp() {
    try {
      return String.format(tsFormat, getCalendar());
    } catch(NoSuchMethodError e) {
      return "";
    }
  }

  /**
   * Display a formatted length of time
   * @param time The time, in milliseconds
   * @return A String with the formatted length of time
   */
  public static String formatTime(long time) {
    return formatTime(time, true);
  }

  /**
   * Display a formatted length of time
   * @param time The time, in milliseconds
   * @param ms Whether to display milliseconds
   * @return A String with the formatted length of time
   */
  public static String formatTime(long time, boolean ms) {
    if(time < 0)
      return "(neg) " + formatTime(time * -1, ms);

    if(time == 0)
      return ms ? "0ms" : "0s";

    String text = "";
    if(ms) {
      if(time < 1000*60) // 60 seconds
        text = Long.toString(time % 1000) + "ms";
      time /= 1000;
    } else {
      time--;
      time /= 1000;
      time++;
    }

    if(time > 0) {
      if(time < 60*60) // 60 minutes
        text = Long.toString(time % 60) + "s " + text;
      time /= 60;
      if(time > 0) {
        if(time < 60*24) // 24 hours
          text = Long.toString(time % 60) + "m " + text;
        time /= 60;
        if(time > 0) {
          if(time < 24*7)  // 7 days
            text = Long.toString(time % 24) + "h " + text;
          time /= 24;
          if(time > 0)
            text = Long.toString(time) + "d " + text;
        }
      }
    }

    return text.trim();
  }

  public static long parseDuration(String duration)
  throws NumberFormatException {
    long out = 0;
    for(String part : duration.split(" ")) {
      if(part.length() == 0)
        continue;

      int i = 0;
      for(; i < part.length(); i++) {
        char c = part.charAt(i);
        if((c < '0') || (c > '9'))
          break;
      }
      long base = Long.parseLong(part.substring(0, i));
      String unit = part.substring(i).toLowerCase();

      switch(unit.charAt(0)) {
      case 's':
        if("s".equals(unit)
        || "second".equals(unit)
        || "seconds".equals(unit)) {
          out += base * SECOND;
          continue;
        }
        break;
      case 'm':
        if("m".equals(unit)
        || "min".equals(unit)
        || "mins".equals(unit)
        || "minute".equals(unit)
        || "minutes".equals(unit)) {
          out += base * MINUTE;
          continue;
        }
        break;
      case 'h':
        if("h".equals(unit)
        || "hour".equals(unit)
        || "hours".equals(unit)) {
          out += base * HOUR;
          continue;
        }
        break;
      case 'd':
        if("d".equals(unit)
        || "day".equals(unit)
        || "days".equals(unit)) {
          out += base * DAY;
          continue;
        }
        break;
      case 'w':
        if("w".equals(unit)
        || "week".equals(unit)
        || "weeks".equals(unit)) {
          out += base * WEEK;
          continue;
        }
        break;
      }
      throw new NumberFormatException("Invalid unit: " + unit);
    }
    return out;
  }

  /**
   * Converts a Windows FileTime structure to a Date
   * @param ft contains a 64-bit value representing the number of 100-nanosecond intervals since January 1, 1601 (UTC).
   * @return a Date with the correct time
   */
  public static Date fileTime(long ft) {
    // Date.parse("1/1/1601") == 11644455600000L
    long date = ft / 10000 - 11644455600000L;
    date += timeZone.getOffset(date);
    return new Date(date);
  }

  private static DateFormat df = DateFormat.getDateInstance();
  private static DateFormat dtf = DateFormat.getDateTimeInstance();

  public static long parseDate(String d) throws ParseException {
    df.setCalendar(getCalendar());
    return df.parse(d).getTime();
  }

  public static String formatDate(Date d) {
    df.setCalendar(getCalendar());
    return df.format(d);
  }

  public static long parseDateTime(String dt) throws ParseException {
    dtf.setCalendar(getCalendar());
    return dtf.parse(dt).getTime();
  }

  public static String formatDateTime(Date d) {
    dtf.setCalendar(getCalendar());
    return dtf.format(d);
  }
}

   
    
    
    
    
  








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.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