Time Formatter : 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 Formatter
   
// TimeFormatter.java
// $Id: TimeFormatter.java,v 1.5 2000/08/16 21:37:58 ylafon Exp $
// (c) COPYRIGHT MIT and INRIA, 1996.
// Please first read the full copyright statement in file COPYRIGHT.html


import java.util.Date;

/**
 * This class does date formatting using the same format strings accepted by the
 * strftime(3) UNIX call. This class has static methods only.
 @author <a href="mail:anto@w3.org">Antonio Ram&iacute;rez</a>
 */

public class TimeFormatter {

    private static String[] fullWeekDays =
    {
  "Sunday",
  "Monday",
  "Tuesday",
  "Wednesday",
  "Thursday",
  "Friday",
  "Saturday"
    ;

    private static String[] abrWeekDays =
    {
  "Sun",
  "Mon",
  "Tue",
  "Wed",
  "Thu",
  "Fri",
  "Sat"
    ;

    private static String[] fullMonths =
    {
  "January",
  "February",
  "March",
  "April",
  "May",
  "June",
  "July",
  "August",
  "September",
  "October",
  "November",
  "December"
    ;

    private static String[] abrMonths =
    {
  "Jan",
  "Feb",
  "Mar",
  "Apr",
  "May",
  "Jun",
  "Jul",
  "Aug",
  "Sep",
  "Oct",
  "Nov",
  "Dec"
    ;
  
    /**
     * Format the given date as a string, according to the given
     * format string.
     * The format string is of the form used by the strftime(3) UNIX
     * call.
     @param date The date to format
     @param format The formatting string
     @return the String with the formatted date.  */
    public static String format(Date date,String format) {
  StringBuffer buf = new StringBuffer(50;
  char ch;
  for(int i=0;i<format.length();i++) {
      ch = format.charAt(i;
      if(ch == '%') {
    ++i ;
    if(i == format.length()) break ;
    ch = format.charAt(i;
    if(ch == 'E') {
        // Alternate Era
        ++i;
    else if(ch == 'Q') {
        // Alternate numeric symbols
        ++i;
    }
    if(i == format.length()) break ;
    ch = format.charAt(i;
    switch(ch) {
      case 'A':
          buf.append(fullWeekDays[date.getDay()]) ;
          break ;
      case 'a':
          buf.append(abrWeekDays[date.getDay()]) ;
          break ;

      case 'B':
          buf.append(fullMonths[date.getMonth()]) ;
          break ;

      case 'b':
      case 'h':
          buf.append(abrMonths[date.getMonth()]) ;
          break ;

      case 'C':
          appendPadded(buf,(date.getYear()+19001002;
          break ;

      case 'c':
          buf.append(date.toLocaleString()) ;
          break ;

      case 'D':
          buf.append(TimeFormatter.format(date,"%m/%d/%y"));
          break;
          
      case 'd':
          appendPadded(buf,date.getDate(),2;
          break;

      case 'e':
          appendPadded(buf,date.getMonth()+1,2,' ';
          break;

      case 'H':
          appendPadded(buf,date.getHours(),2;
          break ;

      case 'I':
      case 'l':
          int a = date.getHours() 12 ;
          if(a==0a = 12 ;
          appendPadded(buf,a,2,ch=='I' '0' ' ';
          break ;

      case 'j':
          buf.append("[?]";
          // No simple way to get this as of now
          break ;

      case 'k':
          appendPadded(buf,date.getHours(),2,' ';
          break ;

      case 'M':
          appendPadded(buf,date.getMinutes(),2;
          break ;

      case 'm':
          appendPadded(buf,date.getMonth()+1,2;
          break ;

      case 'n':
          buf.append('\n';
          break ;

      case 'p':
          buf.append(date.getHours()<12 "am" "pm";
          break;

      case 'R':
          buf.append(TimeFormatter.format(date,"%H:%M")) ;
          break ;

      case 'r':
          buf.append(TimeFormatter.format(date,"%l:%M%p")) ;
          break ;

      case 'S':
          appendPadded(buf,date.getSeconds(),2;
          break ;
          
      case 'T':
          buf.append(TimeFormatter.format(date,"%H:%M:%S"));
          break ;

      case 't':
          buf.append('\t';
          break ;

      case 'U':
      case 'u':
      case 'V':
      case 'W':
          buf.append("[?]");
          // Weekdays are a pain, especially
          // without day of year (0-365) ;
          break ;

      case 'w':
          buf.append(date.getDay()) ;
          break ;

      case 'X':
          buf.append(TimeFormatter.format(date,"%H:%M:%S"));
          break ;

      case 'x':
          buf.append(TimeFormatter.format(date,"%B %e, %Y")) ;
          break ;

      case 'y':
          appendPadded(buf,(date.getYear()+1900100,2;
          break ;

      case 'Y':
          appendPadded(buf,(date.getYear()+1900),4;
          break ;

      case 'Z':
          String strdate = date.toString() ;
          buf.append(strdate.substring(20,23)) ;
          // (!)
          // There should be a better way
          // to do this...
          break ;
      case '%':
          buf.append('%';
          break ;
          
    }
      else {
    buf.append(ch;
      }
  }
  return buf.toString() ;
    }

    private static void appendPadded(StringBuffer buf,
             int n,
             int digits,
             char pad) {
  String foo = String.valueOf(n).trim() ;
  for(int i=0;i<digits-foo.length();i++
      buf.append(pad);
  buf.append(foo;
    }

    private static final void appendPadded(StringBuffer buf,
             int n,
             int digits) {
  appendPadded(buf,n,digits,'0';
    }

    /**
     * For testing purposes
     */
    public static void main(String[] args) {
  try {
      System.out.println(TimeFormatter
             .format(new Date(),args[0])) ;
  catch(Exception ex) {
      ex.printStackTrace() ;
  }
    }
}

   
    
    
  
Related examples in the same category
1. Get Time From Date
2. ISO8601 Date Time Format
3. Time Format
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.