Time Format : Time « Development Class « Java

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. JDK 6
25. JNDI LDAP
26. JPA
27. JSP
28. JSTL
29. Language Basics
30. Network Protocol
31. PDF RTF
32. Reflection
33. Regular Expressions
34. Scripting
35. Security
36. Servlets
37. Spring
38. Swing Components
39. Swing JFC
40. SWT JFace Eclipse
41. Threads
42. Tiny Application
43. Velocity
44. Web Services SOA
45. XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java » Development Class » TimeScreenshots 
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
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.