简体   繁体   中英

Convert date and time to milliseconds

I have 2 inputs fields date and time

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

how can i convert this 2 inputs into milliseconds in java

thanks

Maybe this helps you:

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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM