java/groovy 计算时间

Original 2016-11-10 14:13:19 388
abstract:JavaString dateStart = "01/14/2015 08:29:58";String dateStop = "01/15/2015 11:31:48";//HH converts hour in 24 hours

Java

String dateStart = "01/14/2015 08:29:58";String dateStop = "01/15/2015 11:31:48";//HH converts hour in 24 hours format (0-23), day calculationSimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");Date d1 = null;Date d2 = null;d1 = format.parse(dateStart);d2 = format.parse(dateStop);//in millisecondslong diff = d2.getTime() - d1.getTime();long diffSeconds = diff / 1000 % 60;long diffMinutes = diff / (60 * 1000) % 60;long diffHours = diff / (60 * 60 * 1000) % 24;long diffDays = diff / (24 * 60 * 60 * 1000);System.out.print(diffDays + " days, ");System.out.print(diffHours + " hours, ");System.out.print(diffMinutes + " minutes, ");System.out.print(diffSeconds + " seconds.");

Groovy 

import java.text.SimpleDateFormat;
def dateStart = "01/14/2015 08:29:58";
def dateStop = "01/15/2015 11:31:48";
 
SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
 
Date d1=format.parse(dateStart)
Date d2=format.parse(dateStop)
 
log.info(d1.getTime())
 
long diffMilliseconds = d2.getTime() - d1.getTime()
long diffSecondsTemp = diffMilliseconds / 1000
long diffSeconds = diffSecondsTemp % 60
log.info(diffSeconds)
 
long diffMinutesTemp = diffMilliseconds / (60 * 1000)
long diffMinutes = diffMinutesTemp % 60
log.info(diffMinutes)
 
long diffHoursTemp = diffMilliseconds / (60 * 60 * 1000)
long diffHours = diffHoursTemp % 24
log.info(diffHours)
 
long diffDays = diffMilliseconds / (24 * 60 * 60 * 1000)
log.info(diffDays)

   


Release Notes

Popular Entries