Most of the content of this article is based on official documents and online experience summary, and has been sorted and accumulated through personal practice, for reference only.
1. Convert date and time string into milliseconds
@Testpublic void test() throws ParseException { String dateTime = "2016-12-31 12:30:45 123"; Calendar calendar = Calendar.getInstance(); calendar.setTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS").parse(dateTime)); System.out.println("Date[2016-12-31 12:30:45 123] corresponds to milliseconds: " + calendar.getTimeInMillis());} Running results:
2. Convert milliseconds into date and time string
@Testpublic void test() { long millisecond = 1483159625851l; Date date = new Date(millisecond); SimpleDateFormat format = new SimpleDateFormat("yyyyy year MM month dd date hh:mm:ss SSS a"); System.out.println("ms[1483159625851] corresponding date and time string: " + format.format(date));}Running results:
For more date and time string formats, please refer to: Java: SimpleDateFormat
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.