[ Back | Previous | Next ]

Using Date formatting with java.text.SimpleDateFormat

Package:
java.text.*
Product:
JDK
Release:
1.1.x
Related Links:
DateFormat
DecimalFormat
NumberFormat
SimpleDateFormat
Comment:
public static String toEDIDate(java.sql.Date o) {
	StringBuffer date = new SimpleDateFormat("yyMMdd").format(o, new StringBuffer(), new FieldPosition(0));
	return date.toString();
}
// DateFormat.java
// jdk1.1.3
//
import java.util.*;
import java.text.*;

class DateFormat {

  String format(Date visited) {
    SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yy"); 
    FieldPosition pos = new FieldPosition(0);
    StringBuffer empty = new StringBuffer();
    StringBuffer date = sdf.format(visited, empty, pos);
    return date.toString();
  }

  public static void main( String [] args ) {
    DateFormat df = new DateFormat();
    Date day = new Date();
    System.out.println( "The date is = " + day );
    System.out.println( "reformatted = " + df.format(day) );
  }

Or change main into: 

     SimpleDateFormat formatter
         = new SimpleDateFormat ("yyyy.MM.dd ww 'at' hh:mm:ss a zzz");
     Date day = new Date();
     System.out.println("for:" + formatter.format( day ));


For the correct syntax of the date format see notes on the syntax.
1