java 计算时差_java计算时间差的几种方法

用JAVA计算日期,时间差的几种方法

1. 手工计算

2.采用一个三方的jar包(Joda time library)

3. 利用TimeUnit类(java.util.concurrent 包中)

方法一:手工计算import java.text.SimpleDateFormat;

import java.util.Date;

public class DateDifferentExample {

public static void main(String[] args) {

String dateStart = "01/14/2012 09:29:58";

String dateStop = "01/15/2012 10:31:48";

//HH converts hour in 24 hours format (0-23), day calculation

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);

//in milliseconds

long diff = d2.getTime() - d1.getTime();

long diffSeconds = diff / 1000 % 60;

long diffMinutes = diff / (60 * 1000)

你可能感兴趣的:(java,计算时差)