跟上Java的脚步(四) 时间 API(8)

时间相关笔记-》》》https://www.cnblogs.com/DarGi2019/p/11763527.html

java.time

LocalDate、LocalTime、LocalDateTime: LocalDateTime,是LocalDate和LocalTime的合体。

某一天:

 今天:

 代码:

     LocalDate date = LocalDate.now();
        int year = date.getYear();
        int month=date.getMonthValue();
        Month month2 = date.getMonth();
        int day = date.getDayOfMonth();
        DayOfWeek dow = date.getDayOfWeek();
        int len = date.lengthOfMonth();
        boolean leap = date.isLeapYear();// 是否是闰年

        System.out.println(""+year+"-"+month+"-"+day+"("+month2+","+dow+",这个月一共有"+len+"天)");

 第几天:

 

Instant:机器的时间格式

 

Duration/Period

两个日期差:分别差1-> 1小时 33分 巴拉巴拉秒 。2->差 一个月20天。 没毛病。

 

 代码-》

Duration duration = Duration.between(dateTime,LocalDateTime.of(2020,6,19,12,12,12));
        System.out.println(duration);


        Period period = Period.between(LocalDate.of(2019,11,30),LocalDate.of(2019,10,10));
        System.out.println(period);

举一些栗子:

 赋值:with 2019年的今天

加 减 :plus minus

        LocalDate date = LocalDate.now();
        date=date.withYear(2019);
        System.out.println(date);

        date=date.plus(6, ChronoUnit.MONTHS);// 加六个月
        System.out.println(date);
        date= date.plusWeeks(1);//加一周
        System.out.println(date);
        date= date.minusYears(2);// 减两年
        System.out.println(date);  

 

 

TemporalAdjusters

 

 代码:

LocalDate date = LocalDate.now();
        System.out.println("今天是:"+date);
        LocalDate date1 = date.with(nextOrSame(DayOfWeek.SUNDAY));
        System.out.println("周日是:"+date1);// 最近的符合条件的:周日
        LocalDate date2 = date.with(firstDayOfMonth());
            System.out.println("月初是:"+date2);
            date2 = date.with(lastDayOfMonth());
            System.out.println("月底是:"+date2);
        LocalDate date3 = date.with(dayOfWeekInMonth(1,DayOfWeek.SUNDAY));
        System.out.println("这个月第一周的周日是:"+date3);
            date3 = date.with(dayOfWeekInMonth(-1,DayOfWeek.SUNDAY));
            System.out.println("这个月最后一周的周日是:"+date3);
        LocalDate date4 = date.with(firstDayOfNextMonth());
            System.out.println("下个月第一天是:"+date4);
            date4 = date.with(firstDayOfNextYear());
            System.out.println("明年第一天是:"+date4);
        LocalDate date5 = date.with(firstInMonth(DayOfWeek.SUNDAY));
        System.out.println("这个月第一个周日是:"+date5);
        LocalDate date6 = date.with(next(DayOfWeek.SUNDAY));
        System.out.println("下周日是:"+date6);
        date6 = date.with(previous(DayOfWeek.SUNDAY));
        System.out.println("上周日是:"+date6);
View Code

DateTimeFormatter:输出 日期格式化

 

LocalDate date = LocalDate.now();
        String str = date.format(DateTimeFormatter.BASIC_ISO_DATE);
        System.out.println(str);
        str = date.format(DateTimeFormatter.ISO_LOCAL_DATE);
        System.out.println(str);
View Code

 逆:

 代码

LocalDate date1 = LocalDate.parse("2020-02-02",DateTimeFormatter.ISO_LOCAL_DATE);
        System.out.println(date1);
        date1 = LocalDate.parse("20200202",DateTimeFormatter.BASIC_ISO_DATE);
        System.out.println(date1);
View Code

 自定义输出:

 

代码

DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy/MM/dd");
        LocalDate date = LocalDate.now();
        String str = date.format(fmt);
        System.out.println(str);
        LocalDate date1 = LocalDate.parse(str,fmt);
        System.out.println(date1);
View Code

 再来两个栗子:

 

 代码:

DateTimeFormatter fmt = DateTimeFormatter.ofPattern("d MMMM yyyy", Locale.CHINA);
        LocalDate date = LocalDate.now();
        String str = date.format(fmt);
        System.out.println(str);
        LocalDate date1 = LocalDate.parse(str,fmt);
        System.out.println(date1);
View Code

 

 

 代码:

DateTimeFormatter fmt = new DateTimeFormatterBuilder()
                .appendText(ChronoField.DAY_OF_MONTH)
                .appendLiteral("/")
                .appendText(ChronoField.MONTH_OF_YEAR)
                .appendLiteral("/")
                .appendText(ChronoField.YEAR)
                .parseCaseInsensitive()
                .toFormatter(Locale.CHINA);
        LocalDate date = LocalDate.now();
        String str = date.format(fmt);
        System.out.println(str);
        LocalDate date1 = LocalDate.parse(str,fmt);
        System.out.println(date1);
View Code

**TimeZone-时区计算用。

 

 

 

 

 

@

posted @ 2020-07-08 10:36  DarGi  阅读(214)  评论(0编辑  收藏  举报