• 跟上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-时区计算用。

    @

  • 相关阅读:
    C# winIO32位,64位的使用(运行时要用管理员身份)
    C#实现的三种方式实现模拟键盘按键
    C#打印日志的小技巧
    write wall ping ifconfig mail last traceroute netstat setup mount
    安装常用工具 zip unzip bzip2 gcc gcc++编译器 cmake编译器
    gzip/gunzip tar -zcf/-zxvf zip /unzip bzip2/bunzip2 tar -cjf/tar -xjf
    help
    Asp.Net 高性能框架 SqlSugar.ORM 2.3
    centos 查看版本(转)
    浅谈OCR之Tesseract
  • 原文地址:https://www.cnblogs.com/DarGi2019/p/13140822.html
Copyright © 2020-2023  润新知