miércoles, 28 de marzo de 2007

Parse a String to obtain a Date/GregorianCalendar object - Real's Java How-to


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

public class dformat {
public static void main (String a[]){
try {
/*
** we specify Locale.US since months are in english
** we want to parse a TimeStamp
*/
SimpleDateFormat sdf = new SimpleDateFormat
("dd-MMM-yyyy HH:mm:ss", Locale.US);
/*
** fix timezone in the SimpleDateFormat
** bug in JDK1.1
*/
sdf.setCalendar(Calendar.getInstance());
/*
** create a Date (no choice, parse returns a Date object)
*/
Date d = sdf.parse("24-Feb-1998 17:39:35");
System.out.println(d.toString());
/*
** create a GregorianCalendar from a Date object
*/
GregorianCalendar gc = new GregorianCalendar();
gc.setTime(d);
System.out.println(gc.getTime().toString());
}
catch (Exception e) {
e.printStackTrace();
}
}
}

Parse a String to obtain a Date/GregorianCalendar object - Real's Java How-to
Publicar un comentario