Time Format : Time « Development Class « Java

Time Format


import java.text.DateFormat;
import java.text.FieldPosition;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

 * TimeStampFormatter.
 * <p/>
 * Date: Sep 17, 2005
 * Time: 1:01:13 PM
 * @author <a href="mailto:hs@tagtraum.com">Hendrik Schreiber</a>
public class TimeFormat extends DateFormat {

    private static final long ONE_SECOND = 1000l;
    private static final long ONE_MINUTE = ONE_SECOND * 60l;
    private static final long ONE_HOUR = ONE_MINUTE * 60l;
    private static final long ONE_DAY = ONE_HOUR * 24l;

    private SimpleDateFormat secondsFormat = new SimpleDateFormat("s's'");
    private SimpleDateFormat minuteFormat = new SimpleDateFormat("m'm'");
    private SimpleDateFormat hourFormat = new SimpleDateFormat("H'h'");
    private DateFormat fullFormat = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.MEDIUM);

    public TimeFormat() {
        final TimeZone utcTimeZone = TimeZone.getTimeZone("UTC");

    public StringBuffer format(Date date, StringBuffer toAppendTo, FieldPosition fieldPosition) {
        long time = date.getTime();
        if (time >= ONE_DAY * 365) {
            return fullFormat.format(date, toAppendTo, fieldPosition);
        if (time >= ONE_DAY * 3) {
            toAppendTo.append(time / ONE_DAY);
            if (time % ONE_DAY != 0) {
                hourFormat.format(date, toAppendTo, fieldPosition);
        else if (time >= ONE_HOUR) {
            toAppendTo.append(time / ONE_HOUR);

        if (time >= ONE_MINUTE && time % ONE_HOUR !=0) {
            minuteFormat.format(date, toAppendTo, fieldPosition);
        if (time >= ONE_SECOND && time % ONE_MINUTE !=0) {
            secondsFormat.format(date, toAppendTo, fieldPosition);
        return toAppendTo;

    public Date parse(String source, ParsePosition pos) {
        throw new RuntimeException("Not implemented.");



Related examples in the same category

1.Get Time From Date
2.ISO8601 Date Time Format
3.Time Formatter
4.Returns time string
5.Returns the given date with the time values cleared
6.Convert the time to the midnight of the currently set date
7.Compare both times and dates
8.Tells you if the date part of a datetime is in a certain time range
9.Returns the given date with time set to the end of the day
10.Convert milliseconds to readable string
11.Determines whether or not a date has any time values (hour, minute, seconds or millisecondsReturns the given date with the time values cleared
12.Returns a formatted String from time
13.Time library
14.Elapsed time in hours/minutes/seconds
15.Sets the time on the same day to 00:00:00.000
16.Determines if given times span at least an entire day
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