Wednesday, April 15, 2009

Java Date to Epoch

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class DateConverter {
public static void main(String[] args) throws ParseException {
SimpleDateFormat df = (SimpleDateFormat) SimpleDateFormat.getInstance();
df.applyPattern("yyyy MMM dd HH:mm:ss");
Date date = df.parse("2009 Apr 14 16:46:23");
System.out.println(date);
Calendar cal = new GregorianCalendar();
cal.setTime(date);
System.out.println(cal.getTimeInMillis());
}
}

No comments: