[英]how to get difference between two date_time
I want to get the difference in seconds between end_date_time and system_date_time using JAVA, for example: 我想使用JAVA获得end_date_time和system_date_time之间的秒数差异,例如:
if
end_date_time = 2015-02-21 13:00:00
system_date_time = 2015-02-20 13:00:00
then
difference = 86400
Please tell how can I do this? 请告诉我该怎么做?
If you have a Date
type already, it's as simple as: 如果您已经有一个
Date
类型,那么就很简单:
(System.currentTimeMillis() - myDate.getTime()) / 1000;
If you have a string and need to convert it to a date, you use a SimpleDateFormat
instance: 如果您有字符串并且需要将其转换为日期,则可以使用
SimpleDateFormat
实例:
String dateString = "2015-02-21 13:00:00" // end_date_time
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd hh:mm:ss");
Date myDate = sdf.parse(dateString);
If you're using Java8, this is much easier: 如果您使用的是Java8,这会容易得多:
String dateString = "2007-12-03T10:15:30.00Z";
Instant.now().until(Instant.parse(dateString), ChronoUnit.SECONDS);
Your dateString
must be parseable in the ISO format, though: 不过,您的
dateString
必须可解析为ISO格式:
"yyyy-mm-ddThh:mm:ss.SZ"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.