[Java ]一天中的时间

2018-06-01  本文已影响0人  但莫

很多人都不确定一天中的准确开始时间是什么,写一个简单的程序说明一下。

one day`s begin time is 00:00:00

one day`s end time is 23:59:59

        String pattern = "yyyy-MM-dd HH:mm:ss";
        SimpleDateFormat sf = new SimpleDateFormat(pattern);
        Date yesterday = sf.parse("2016-12-11 23:59:59");
        Date todayBegin = sf.parse("2016-12-12 00:00:00");
        Date today1 = sf.parse("2016-12-12 00:00:01");
        Date todayend = sf.parse("2016-12-12 23:23:59");

        System.out.println(sf.format(yesterday) + " is before " + sf.format(todayBegin) + ":" + yesterday.before(todayBegin));
        System.out.println(sf.format(todayBegin) + " is before " + sf.format(today1) + ":" + todayBegin.before(today1));
        System.out.println(sf.format(todayBegin) + " is before " + sf.format(todayend) + ":" + todayBegin.before(todayend));
        System.out.println(sf.format(today1) + " is before " + sf.format(todayend) + ":" + today1.before(todayend));

结果如下

2016-12-11 23:59:59 is before 2016-12-12 00:00:00:true
2016-12-12 00:00:00 is before 2016-12-12 00:00:01:true
2016-12-12 00:00:00 is before 2016-12-12 23:23:59:true
2016-12-12 00:00:01 is before 2016-12-12 23:23:59:true
上一篇下一篇

猜你喜欢

热点阅读