时间

2017-09-05  本文已影响0人  B萌面超人

import java.text.ParseException;

import java.text.SimpleDateFormat;

import java.util.Date;

public class Test06 {

public static void main(String[] args) throws ParseException {

//时间类

Date date = new Date();

System.out.println(date);

// 年-月-天  时:分:秒

//yyyy-MM-dd HH:mm:ss

//时间格式

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

//把时间按照当前的格式转化成字符串

String str = dateFormat.format(date);

System.out.println(str);

//计算俩个时间的差

//2017:09:04 00:00:00 - 2017 :10:01 00:00:00 差多少天

//

SimpleDateFormat dateFormat2 = new SimpleDateFormat("yyyy:MM:dd HH:mm:ss");

Date date1 = dateFormat2.parse("2017:09:04 00:00:00");

Date date2 = dateFormat2.parse("2017:10:01 00:00:00");

//获取时间距离1970年1月1日 的毫秒数

long time1 = date1.getTime();

long time2 = date2.getTime();

long day = (time2 - time1) / 1000 / 60 / 60 / 24;

System.out.println(day);

}

}

上一篇 下一篇

猜你喜欢

热点阅读