The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
September 2000

Advertisement

Advertisement

This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.

Message:

SimpleDateFormat Examples....

Posted by Jeff Kinsey on September 24, 2000 at 4:20 PM

> Hi,
> i'm having trouble making a method that returns
> a date using SimpleDateFormat() .
> i need to make one and i cannot use date can anybody
> help please?


Here are four examples of the use of the SimpleDateFormat....


<>
package examples;

import java.util.*;
import java.text.*;

public class SimpleDateFormatE {
public static void main(String[] args) {

System.out.println("\r\nSimpleDateFormat1: parsing a date in the format MM/dd/yy...");
SimpleDateFormat sdf1 = new SimpleDateFormat("MM/dd/yy");
try {
Date d = sdf1.parse("11/25/98", new ParsePosition(0));
StringBuffer sb = new StringBuffer();
sdf1.format(d, sb, new FieldPosition(0));
System.out.println(sb.toString());
}
catch (Exception e) {
e.printStackTrace();
}

System.out.println("\r\nSimpleDateFormat2 parsing a date in the format dd-MM-yy...");
SimpleDateFormat sdf2 = new SimpleDateFormat("dd-MMM-yy");
try {
Date d = sdf2.parse("25-NOV-98", new ParsePosition(0));
StringBuffer sb = new StringBuffer();
sdf1.format(d, sb, new FieldPosition(0));
System.out.println(sb.toString());
}
catch (Exception e) {
e.printStackTrace();
}

System.out.println("\r\nSimpleDateFormat3: Formatting a date... ");
SimpleDateFormat sdf3a = new SimpleDateFormat("dd-MMM-yy");
SimpleDateFormat sdf3b = new SimpleDateFormat("MM/dd/yyyy");
SimpleDateFormat sdf3c = new SimpleDateFormat("EEE MMM d hh:mm:ss z yyyy");
try {
Date d = new Date();

StringBuffer sb1 = new StringBuffer();
sdf3a.format(d, sb1, new FieldPosition(0));
System.out.println(sb1.toString());

StringBuffer sb2 = new StringBuffer();
sdf3b.format(d, sb2, new FieldPosition(0));
System.out.println(sb2.toString());

StringBuffer sb3 = new StringBuffer();
sdf3c.format(d, sb3, new FieldPosition(0));
System.out.println(sb3.toString());
}
catch (Exception e) {
e.printStackTrace();
}

System.out.println("\r\nSimpleDateFormat4: Formatting a date with different time zones...");
SimpleDateFormat sdf4 = new SimpleDateFormat("EEE MMM d hh:mm:ss z yyyy");
TimeZone tz = TimeZone.getTimeZone("PST");
String[] timeZoneIDs = tz.getAvailableIDs();
for (int i = 0; i < timeZoneIDs.length; i++) {
System.out.println("time zone: " + timeZoneIDs[i]);
sdf4.setTimeZone(tz = TimeZone.getTimeZone(timeZoneIDs[i]));
try {
Date d = new Date();
StringBuffer aStringBuffer = new StringBuffer();
sdf4.format(d, aStringBuffer, new FieldPosition(0));
System.out.println(aStringBuffer.toString());
}
catch (Exception e) {
e.printStackTrace();
}
}
}
}
<>





Replies:
  • Use this Sriram June 15, 2001 at 8:04 PM (0)

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us