[英]How to convert a custom Date Time string to milliseconds using Joda Time?
[英]Convert date and time to milliseconds
我有2個輸入字段日期和時間
ex: 23/05/2014 as date and 5:30 as time (24 hour format)
我如何在Java中將這2個輸入轉換為毫秒
謝謝
也許這可以幫助您:
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
}
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();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.