Time Format : Time « Development Class « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » Development Class » Time 




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");
        minuteFormat.setTimeZone(utcTimeZone);
        hourFormat.setTimeZone(utcTimeZone);
    }

    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);
            toAppendTo.append('d');
            if (time % ONE_DAY != 0) {
                hourFormat.format(date, toAppendTo, fieldPosition);
            }
        }
        else if (time >= ONE_HOUR) {
            toAppendTo.append(time / ONE_HOUR);
            toAppendTo.append('h');
        }

        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
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.