我有两个java日期时间值,我想知道该过程所需的总时间。
private static Date crawlStartDate,crawlEndDate,totalCrawlTime;
我怎么能得到它?时间格式是yyyyMMddhhmmss
由于
答案 0 :(得分:1)
public static void main(String[] argv) {
long lStartTime = new Date().getTime(); // start time
createArray(); // some tasks to eat time
long lEndTime = new Date().getTime(); // end time
long difference = lEndTime - lStartTime; // check different
System.out.println("Elapsed milliseconds: " + difference);
}
public static void createArray() {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
String sArray[] = new String[1000000];
for (int i = 0; i < 1000000; i++)
sArray[i] = "Array " + i;
}
答案 1 :(得分:1)
Java解决方案:
long d1=crawlStartDate.getTime()
long d2=crawlEndDate.getTime()
long diff = d2 - d1;
long diffSeconds = diff / 1000 % 60;
long diffMinutes = diff / (60 * 1000) % 60;
如果您在Linux / BSD中工作,请使用time java MyJavaProcess
。 time
这里是unix utility。例如,我可以用这种方式测量一些过程:
$ time java Hello
One value
Two value
real 0m0.070s
user 0m0.068s
sys 0m0.000s
答案 2 :(得分:0)
在流程执行前获取时间
long t1=crawlStartDate.getTime()
在流程执行后获得时间
long t2=crawlEndDate.getTime()
然后计算差异
long timeDiff=t2-t1;
Date date=new Date(timeDiff);
使用SimpleDateFormat-
格式化它DateFormat formatet = new SimpleDateFormat("yyyyMMddHHmmss");
String formattedDate= formater.format(date);
答案 3 :(得分:0)
enter code here
String dateStart = "01/14/2012 09:29:58";
String dateStop = "01/15/2012 10:31:48";
SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
Date d1 = null;
Date d2 = null;
try {
d1= format.parse(dateStart);
d2 = format.parse(dateStop);
long diff = d2.getTime() - d1.getTime();
long diffDays = diff / (24 * 60 * 60 * 1000);
System.out.print("Days:" + diffDays);
}catch(Exception e ) {
}
答案 4 :(得分:0)
试试这个
long differenceInSec = (crawlEndDate.getTime() - crawlEndDate.getTime())/1000;
答案 5 :(得分:0)
如果您已更新为JAVA8,请尝试以下代码
LocalDateTime dateTime = LocalDateTime.of(2014, 06, 27, 15, 12);
LocalDateTime dateTime2 = LocalDateTime.now();
int diffInNano = java.time.Duration.between(dateTime, dateTime2)
.getNano();
long diffInSeconds = java.time.Duration.between(dateTime, dateTime2)
.getSeconds();
long diffInMilli = java.time.Duration.between(dateTime, dateTime2)
.toMillis();
long diffInMinutes = java.time.Duration.between(dateTime, dateTime2)
.toMinutes();
long diffInHours = java.time.Duration.between(dateTime, dateTime2)
.toHours();
System.out.println(diffInNano);
System.out.println(diffInSeconds);
System.out.println(diffInMilli);
System.out.println(diffInMinutes);
System.out.println(diffInHours);