EN
Java - get milliseconds from date time in string
8 points
Date
class -getTime()
methodInstant
class -toEpochMilli()
methodCalendar
class -getTimeInMillis()
method
xxxxxxxxxx
1
String dateTimeStr = "2019-08-10 02:39:00:123";
2
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss:SSS");
3
Date date = sdf.parse(dateTimeStr);
4
5
System.out.println(date.getTime());
Output:
xxxxxxxxxx
1
1565397540123
xxxxxxxxxx
1
String dateTimeStr = "2019-08-10 02:39:00:123";
2
LocalDateTime localDateTime = LocalDateTime.parse(dateTimeStr,
3
DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss:SSS"));
4
5
Instant instant = localDateTime.atZone(ZoneId.systemDefault()).toInstant();
6
7
System.out.println(instant.toEpochMilli()); // 1565397540123
Output:
xxxxxxxxxx
1
1565397540123
xxxxxxxxxx
1
String dateTimeStr = "2019-08-10 02:39:00:123";
2
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss:SSS");
3
Date date = sdf.parse(dateTimeStr);
4
5
Calendar calendar = Calendar.getInstance();
6
calendar.setTime(date);
7
8
System.out.println(calendar.getTimeInMillis()); // 1565397540123
Output:
xxxxxxxxxx
1
1565397540123