将日期和时间转换为毫秒

时间:2014-10-27 07:39:22

标签: java

我有2个输入字段日期和时间

ex: 23/05/2014 as date and 5:30 as time (24 hour format)

如何在java

中将这2个输入转换为毫秒

感谢

2 个答案:

答案 0 :(得分:1)

也许这会对你有所帮助:

String dstr = ... // from date input field
String tstr = ... // from time input field

String pattern = "dd/MM/yyyy H:mm";
String dateTimeStr = dstr + " " + tstr;

SimpleDateFormat fmt = new SimpleDateFormat(pattern);
try {
    Date date = fmt.parse(dateTimeStr);
    long msecs = date.getTime();
} catch (ParseException ex) {
    // Handle parsing error here
}

答案 1 :(得分:1)

String givenDateString = "Tue Apr 23 16:08:28 GMT+05:30 2013"; 
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
try {
    Date mDate = sdf.parse(givenDateString);
    long timeInMilliseconds = mDate.getTime();
    System.out.println("Date in milli :: " + timeInMilliseconds);
} catch (ParseException e) {
            e.printStackTrace();
}