Java Date和Time-(第七天)

Java Date和Time-(第七天)

一、简介

日期Date:

2019-11-20
2020-1-1

时间Time:

12:30:59
2020-1-1 20:21:59

时区TimeZone
GMT+08:00 东八区
UTC+08:00
CST 缩写表示(不推荐使用)
Asia/ShangHai

地区Local
zh_CN 2020-03-14
en_US 03/14/2020

价格

类别 中国 美国
价格 10000.00 10,000.00
日期 2020-03-14 03/14/2020

二、Date

计算机中的日期和时间表示方式:
2020-03-14 8:15:00 GMT+08:00
03/14/2020 8:15:00 Americal/New_York

计算机中的时间存储是采用的Epoch Time 从1970年1月1日零点到现在经历的秒数
时间戳
JDK中的java.util 包括Date和Calendar
java.time(JDK>1.8)包括LocalTime、LocalDate、ZonedDateTime 、Instant等

获取当前时间
new Date()
toString()
long getTime() 获取时间戳

Date t1 = new Date();
System.out.println(t1);//Sat Mar 14 16:04:28 CST 2020
long l = 1574208900123L;
Date t2= new Date(l);
System.out.println(t2);//Wed Nov 20 08:15:00 CST 2019

自定义输出格式SimpleDateFormat

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println(sdf.format(date));
       
        Date ss = null;
        try {
            ss = sdf.parse("2019-12-15 05:01:00");
        } catch (ParseException e) {
            e.printStackTrace();
        }
        System.out.println(ss);

Date不能进行时区的换算,不能对日期和时间进行加减,不能计算两个日期相差多少天,不能计算某个月第一个星期一是几号

三、Calendar

Calendar c = Calendar.getInstance();
        int y = c.get(Calendar.YEAR);
        int m = 1 + c.get(Calendar.MONTH);//加1
        int d = c.get(Calendar.DAY_OF_MONTH);
        int w = c.get(Calendar.DAY_OF_WEEK);1~7分别表示周日,周一,……,周六
        int hh = c.get(Calendar.HOUR_OF_DAY);
        int mm = c.get(Calendar.MINUTE);
        int ss = c.get(Calendar.SECOND);
        int ms = c.get(Calendar.MILLISECOND);
        System.out.println(y + "-" + m + "-" + d + " " + w + " " + hh + ":" + mm + ":" + ss + "." + ms);

注意到Calendar获取年月日这些信息变成了get(int field),返回的年份不必转换,返回的月份仍然要加1,返回的星期要特别注意,1~7分别表示周日,周一,……,周六

// 当前时间:
        Calendar c = Calendar.getInstance();
        // 清除所有:
        c.clear();
        // 设置2019年:
        c.set(Calendar.YEAR, 2019);
        // 设置9月:注意8表示9月:
        c.set(Calendar.MONTH, 8);//月份是从0开始的
        // 设置2日:
        c.set(Calendar.DATE, 2);
        // 设置时间:
        c.set(Calendar.HOUR_OF_DAY, 21);
        c.set(Calendar.MINUTE, 22);
        c.set(Calendar.SECOND, 23);
        System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(c.getTime()));
        // 2019-09-02 21:22:23

setTime(Date)
set(int field,int value)

c.setTime(new Date());
c.clear();
Date d = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2020-03-14 08:01:00");
c.setTime(d);

add(int field,int value)

 // 加5天并减去2小时:
        c.add(Calendar.DAY_OF_MONTH, 5);
        c.add(Calendar.HOUR_OF_DAY, -2);

getTimeZone()
setTimeZone()

// 设置为北京时区:
c.setTimeZone(TimeZone.getTimeZone(“Asia/Shanghai”));
// 设置年月日时分秒:
c.set(2019, 10 /* 11月 */, 20, 8, 15, 0);
// 显示时间:
var sdf = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”);
sdf.setTimeZone(TimeZone.getTimeZone(“America/New_York”));
System.out.println(sdf.format(c.getTime()));
// 2019-11-19 19:15:00

三种获取时间戳的方法

  • System.currentTimeMillis()
  • new Date().getTime()
  • Calendar.getInstance().getTimeInMillis()

四、java.time

LocalDate/LocalTime/LocalDateTime
ZonedDateTime/Zoneld
Instant
Formatter

新API特点

  • 严格区分日期、时间
  • 不变类
  • Month范围1~12(Jan-Dec)
  • Week范围1~7(Mon-Sun)
LocalDate now1 = LocalDate.now();
        System.out.println(now1);
        LocalDateTime now2 = LocalDateTime.now();
        System.out.println(now2);
        LocalTime now3 = LocalTime.now();
        System.out.println(now3);

        LocalDate of = LocalDate.of(2020, 03, 13);
        System.out.println(of);

Formatter

DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String time = dtf.format(LocalDateTime.now());
        System.out.println(time);

        LocalDateTime parse = LocalDateTime.parse("2019-03-14 08:01:00", dtf);
        System.out.println(parse);

对日期和时间进行加减
plusDays() 加天
minusHours() 减
plusWeeks() 加周

调整年:withYear()
调整月:withMonth()
调整日:withDayOfMonth()
调整时:withHour()
调整分:withMinute()
调整秒:withSecond()

 LocalDateTime dt = LocalDateTime.of(2019, 10, 26, 20, 30, 59);
        System.out.println(dt);
        // 日期变为31日:
        LocalDateTime dt2 = dt.withDayOfMonth(31);
        System.out.println(dt2); // 2019-10-31T20:30:59
        // 月份变为9:
        LocalDateTime dt3 = dt2.withMonth(9);
        System.out.println(dt3); // 2019-09-30T20:30:59

要判断两个LocalDateTime的先后,可以使用isBefore()、isAfter()方法,对于LocalDate和LocalTime类似

LocalDateTime now = LocalDateTime.now();
        LocalDateTime target = LocalDateTime.of(2019, 11, 19, 8, 15, 0);
        System.out.println(now.isBefore(target));
        System.out.println(LocalDate.now().isBefore(LocalDate.of(2019, 11, 19)));
        System.out.println(LocalTime.now().isAfter(LocalTime.parse("08:15:00")));

Duration和Period
Duration表示两个时刻之间的时间间隔。另一个类似的Period表示两个日期之间的天数

LocalDateTime start = LocalDateTime.of(2019, 11, 19, 8, 15, 0);
        LocalDateTime end = LocalDateTime.of(2020, 1, 9, 19, 25, 30);
        Duration d = Duration.between(start, end);
        System.out.println(d); // PT1235H10M30S

        Period p = LocalDate.of(2019, 11, 19).until(LocalDate.of(2020, 1, 9));
        System.out.println(p); // P1M21D

五、ZonedDateTime

LocalDateTime总是表示本地日期和时间,要表示一个带时区的日期和时间,我们就需要ZonedDateTime。

可以简单地把ZonedDateTime理解成LocalDateTime加ZoneId。ZoneId是java.time引入的新的时区类,注意和旧的java.util.TimeZone区别。

ZonedDateTime zbj = ZonedDateTime.now(); // 默认时区
        ZonedDateTime zny = ZonedDateTime.now(ZoneId.of("America/New_York")); // 用指定时区获取当前时间
        System.out.println(zbj);
        System.out.println(zny);

六、DateTimeFormatter

使用旧的Date对象时,我们用SimpleDateFormat进行格式化显示。使用新的LocalDateTime或ZonedLocalDateTime时,我们要进行格式化显示,就要使用DateTimeFormatter。

和SimpleDateFormat不同的是,DateTimeFormatter不但是不变对象,它还是线程安全的。线程的概念我们会在后面涉及到。现在我们只需要记住:因为SimpleDateFormat不是线程安全的,使用的时候,只能在方法内部创建新的局部变量。而DateTimeFormatter可以只创建一个实例,到处引用。

创建DateTimeFormatter时,我们仍然通过传入格式化字符串实现:

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");

格式化字符串的使用方式与SimpleDateFormat完全一致。

另一种创建DateTimeFormatter的方法是,传入格式化字符串时,同时指定Locale:

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("E, yyyy-MMMM-dd HH:mm", Locale.US);

七、Instant

计算机存储的当前时间,本质上只是一个不断递增的整数。Java提供的System.currentTimeMillis()返回的就是以毫秒表示的当前时间戳。

这个当前时间戳在java.time中以Instant类型表示,我们用Instant.now()获取当前时间戳,效果和System.currentTimeMillis()类似。

Instant now = Instant.now();
        System.out.println(now.getEpochSecond()); // 秒
        System.out.println(now.toEpochMilli()); // 毫秒
发布了44 篇原创文章 · 获赞 0 · 访问量 1414

猜你喜欢

转载自blog.csdn.net/weixin_44872254/article/details/104861620