我想计算间隔直到某个日期。我测试了这段代码:
String dateStop = "25/11/17 09:00:00";
// Custom date format
SimpleDateFormat format = new SimpleDateFormat("yy/MM/dd HH:mm:ss");
LocalDateTime d1 = LocalDateTime.now();
Date d2 = null;
try
{
d2 = format.parse(dateStop);
}
catch (ParseException e)
{
e.printStackTrace();
}
// Get msec from each, and subtract.
long diff = d2.getTime() - d1.getTime();
long days = TimeUnit.MILLISECONDS.toDays(diff);
long remainingHoursInMillis = diff - TimeUnit.DAYS.toMillis(days);
long hours = TimeUnit.MILLISECONDS.toHours(remainingHoursInMillis);
long remainingMinutesInMillis = remainingHoursInMillis - TimeUnit.HOURS.toMillis(hours);
long minutes = TimeUnit.MILLISECONDS.toMinutes(remainingMinutesInMillis);
long remainingSecondsInMillis = remainingMinutesInMillis - TimeUnit.MINUTES.toMillis(minutes);
long seconds = TimeUnit.MILLISECONDS.toSeconds(remainingSecondsInMillis);
System.out.println("Days: " + days + ", hours: " + hours + ", minutes: " + minutes + ", seconds: " + seconds);
但我如何获得当前日期并计算时差?我在这里得到例外:location:类型为LocalDateTime的变量d1
我如何解决这个问题?
答案 0 :(得分:0)
getTime()
没有LocalDateTime
。请使用Date
的{{1}}类代替或转换为它:
d1