• java8 LocalDateTime


    LocalDateTime

    java8使用了LocalDateTime和DateTimeFormatter。比之前的Date和Carlendar有所改进。
    DateTimeFormatter是线程安全的。DateTimeFormatter中很多属性使用了final修饰。

    LocalDate: 只能设置仅含年月日的格式,表示没有时区的日期, LocalDate是不可变并且线程安全的
    LocalTime: 只能设置仅含时分秒的格式,表示没有时区的时间, LocalTime是不可变并且线程安全的
    LocalDateTime: 可以设置含年月日时分秒的格式 , 表示没有时区的日期时间, LocalDateTime是不可变并且线程安全的

    ZoneId: 时区ID,用来确定Instant和LocalDateTime互相转换的规则
    Instant: 用来表示时间线上的一个点(瞬时)
    Clock: 用于访问当前时刻、日期、时间,用到时区
    Duration: 用秒和纳秒表示时间的数量(长短),用于计算两个日期的“时间”间隔

    构建LocalDateTime对象:

    	public  void generateLocalDate() {
    		LocalDate date = LocalDate.of(2000, 1, 8);
    		LocalDateTime dateTime = LocalDateTime.of(2020, 1, 8, 15, 16, 17);
    		System.out.println("date:" + date);
    		System.out.println("dateTime:" + dateTime);
    	}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    获取LocalDateTime的年月日:

    	public  void getLocalDateTimeMonth() {
    		LocalDateTime dateTime = LocalDateTime.of(2020, 1, 8, 15, 16, 17);
    		int year=dateTime.getYear();
    		int month=dateTime.getMonth().getValue();
    		int day=dateTime.getDayOfMonth();
    		System.out.println("dateTime对应的年份为:"+year);
    		System.out.println("dateTime对应的月份为:"+month);
    		System.out.println("dateTime对应的天数为:"+day);
    	}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    字符串String转LocalDateTime:

    	public  void stringToTime() {
    		DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    		String datetimeText = "2020-04-01 23:59:59";
    		LocalDateTime localDateTime = LocalDateTime.parse(datetimeText, formatter);
    		// LocalDateTime的toString()方法,在日期和时间中间加入了"T"字符串
    		System.out.println(localDateTime);
    	}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    LocalDateTime 转字符串String:

    	public  void localDateTimeToString() {
    		DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    		LocalDateTime dateTime = LocalDateTime.of(2020, 1, 8, 15, 16, 17);
    		String dateTimeStr = dateTime.format(formatter);
    		System.out.println("dateTimeStr为:" + dateTime);
    	}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    LocalDateTime 转时间戳毫秒

          public static long getMillsByLocalDateTime(LocalDateTime localDateTime) {
            long mills = localDateTime.toInstant(ZoneOffset.of("+8")).toEpochMilli();
            return mills;
        }
    
    • 1
    • 2
    • 3
    • 4

    时间戳毫秒转LocalDateTime

        public static LocalDateTime getLocalDateTimeByMills() {
            long mills = 1640419285000L;
            LocalDateTime result = new Date(mills).toInstant().atOffset(ZoneOffset.of("+8")).toLocalDateTime();
            return result;
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    Date转Instant:

            Instant instant= date.toInstant();
    
    • 1

    Instant转Date:

            Date date = Date.from(instant);
    
    • 1

    Date转LocalDateTime :

    public static void dateToLocalDateTime(Date date) {
            Instant dateInstant = date.toInstant();
            LocalDateTime localDateTime = LocalDateTime.ofInstant(dateInstant, ZoneId.systemDefault());
        }
    
    • 1
    • 2
    • 3
    • 4

    LocalDateTime转Date:

        public static Date parse(LocalDateTime localDateTime) {
             return Date.from(localDateTime.atZone( ZoneId.systemDefault()).toInstant());
        }
    
    • 1
    • 2
    • 3

    LocalDateTime 获取LocalDate:

    LocalDate localDate = localDateTime.toLocalDate();
    
    • 1

    LocalDateTime 获取LocalTime:

    LocalTime localTime = localDateTime.toLocalTime();
    
    • 1

    LocalDate

    Date转LocalDate:

        public void dateToLocalDate(Date date) {
            Instant dateInstant = date.toInstant();
            LocalDate localDate = LocalDateTime.ofInstant(dateInstant, ZoneId.systemDefault()).toLocalDate();
        }
    
    • 1
    • 2
    • 3
    • 4

    LocalDate转Date:

        public static Date parse(LocalDate localDate) {
            return Date.from(localDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant());
        }
    
    • 1
    • 2
    • 3

    localDate转String :

    public static String localDateToString(Date date) {
            Instant dateInstant = date.toInstant();
            LocalDate localDate = LocalDateTime.ofInstant(dateInstant, ZoneId.systemDefault()).toLocalDate();
            // localDate转String 
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            String localDateStr = localDate.format(formatter);
            System.out.println(localDateStr);
            return localDateStr;
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    String转LocalDate:

    public static void stringToLocalDate() {
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            String datetimeText = "2020-04-01";
            LocalDate localDate = LocalDate.parse(datetimeText, formatter);
            System.out.println(localDate);
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    LocalDate获取年月日:

        int year = localDate.getYear();
        int monthValue = localDate.getMonthValue();
        int dayOfMonth = localDate.getDayOfMonth();
    
    
    • 1
    • 2
    • 3
    • 4

    LocalDate修改年月日:

        public static void changeYearMonthDay() {
            LocalDate localDate = LocalDate.now();
            //修改年份
            LocalDate withYearResult = localDate.withYear(2020);
            //修改月份
            LocalDate withMonthResult = localDate.withMonth(5);
            //修改日
            LocalDate withDayResult = localDate.withDayOfMonth(20);
            System.out.println( withYearResult + "," + withMonthResult+ ","+ withDayResult);
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    LocalDate比较日期大小compareTo:

    compareTo()方法
    在两边相等(=)的情况下返回 0
    在左边大于(>)右边时返回 1 
    在左边小于(<)右边时返回 -1

          public static void localDateCompareTo() {
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            String datetimeText = "2020-04-01";
            LocalDate localDate = LocalDate.parse(datetimeText, formatter);
    
            LocalDate nowDate = LocalDate.now();
            System.out.println(localDate);
            System.out.println("nowDate:"+nowDate);
    
            if (nowDate.compareTo(localDate) < 0) {
                System.out.println("nowDate小于"+ datetimeText);
            } else {
                System.out.println("nowDate大于等于"+ datetimeText);
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    LocalDate比较日期的前后:

    after 只在大于(>)情况下才为true (相等时不会)
    before 只在小于(<)情况下才为true (相等时不会)

    public static void isBeforeAfter() {
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            String datetimeText = "2020-04-01";
            LocalDate localDate = LocalDate.parse(datetimeText, formatter);
    
            LocalDate nowDate = LocalDate.now();
            System.out.println(localDate);
            System.out.println("nowDate:"+nowDate);
    
            if (nowDate.isBefore(localDate)) {
                System.out.println(nowDate+ "在"+ datetimeText +"之前");
            } else {
                System.out.println(nowDate+ "在"+ datetimeText +"之后");
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    获取日期差距 / 获取日期时间间隔 :

    public static void durationDateTime() {
            LocalDateTime date3 = LocalDateTime.now();
            LocalDateTime date4 = LocalDateTime.of(2018, 1, 13, 22, 30, 10);
            Duration duration = Duration.between(date3, date4);
            System.out.println(date3 + " 与 " + date4 + " 间隔  " + "\n"
                    + " 天 :" + duration.toDays() + "\n"
                    + " 时 :" + duration.toHours() + "\n"
                    + " 分 :" + duration.toMinutes() + "\n"
                    + " 毫秒 :" + duration.toMillis() + "\n"
                    + " 纳秒 :" + duration.toNanos() + "\n"
            );
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    日期时间计算:

    	public void addMonthDay() {
    		LocalDateTime dt = LocalDateTime.of(2020, 1, 26, 20, 30, 59);
    		System.out.println(dt);
    		// 加5天减3小时:
    		LocalDateTime dt2 = dt.plusDays(5).minusHours(3);
    		System.out.println(dt2); // 2019-10-31T17:30:59
    		// 减1月:
    		LocalDateTime dt3 = dt2.minusMonths(1);
    		System.out.println(dt3); // 2019-09-30T17:30:59
    	}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    LocalTime

    LocalTime获取时分秒:
            //获取小时
            int hour = localTime.getHour();
            int hour1 = localTime.get(ChronoField.HOUR_OF_DAY);
            //获取分
            int minute = localTime.getMinute();
            int minute1 = localTime.get(ChronoField.MINUTE_OF_HOUR);
            //获取秒
            int second = localTime.getMinute();
            int second1 = localTime.get(ChronoField.SECOND_OF_MINUTE);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    参考资料:

    https://www.liaoxuefeng.com/wiki/1252599548343744/1303871087444002
    https://blog.csdn.net/weixin_38405253/article/details/100765007
    https://www.jianshu.com/p/048ee8580639

  • 相关阅读:
    JS执行机制--同步与异步
    MySQL存储引擎详解(一)-InnoDB架构
    新手如何选择外汇交易平台?
    Vue路由--无痕浏览 & NodeJs环境搭建
    Gin简单明了的教程---上
    高精度工厂人员定位,打造数字化智慧工厂
    Maven之POM文件build标签详解
    【Prometheus】Node Exporter常用查询PromQL 语句大总结
    Mac中无法运行旧版本印象笔记:版本太旧 你的本地印象笔记数据是由新版印象笔记管理
    python3数学计算(四则运算、数学函数)整除、取余、幂运算、平方根、阶乘、对数、三角函数、平均值、标准差、积分、微分等、python数学计算
  • 原文地址:https://blog.csdn.net/sinat_32502451/article/details/138199632