Parse String(24-Feb-2009 17:39:35) to a Date/GregorianCalendar
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
public class Main {
public static Calendar parseTimestamp(String timestamp) throws Exception {
SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss", Locale.US);
Date d = sdf.parse(timestamp);
Calendar cal = Calendar.getInstance();
cal.setTime(d);
return cal;
}
public static void main(String a[]) throws Exception {
String timestampToParse = "24-Feb-2009 17:39:35";
System.out.println("Timestamp : " + timestampToParse);
SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println("Calendar : " + sdf.format(parseTimestamp(timestampToParse).getTime()));
}
}
Home
Java Book
Runnable examples
Java Book
Runnable examples
Date Parse:
- Parse string(31/12/06) into date
- Parse string(29 jan 2002) into date
- Parse string(02.47.44 PM) to date time
- Parse time string(15:30:18) to date
- Parse string(29-Jan-02) to date
- Parse string(Feb 28, 2002) to date value
- Convert String(20/12/2005) to Date object
- Parse String(2004-02-29) to a date
- Parse string(2002.01.29.08.36.33) into a date and time
- Parse String(24-Feb-2009 17:39:35) to a Date/GregorianCalendar
- Parse string(Tue, 29 Jan 2004 21:14:02 -0500) to date
- Parse date string in short form
- Parse date string in default Locale Canada format