zoukankan      html  css  js  c++  java
  • Java8新特性——日期时间 API

    Java8新特性——日期时间 API

    Java 8通过发布新的Date-Time API (JSR 310)来进一步加强对日期与时间的处理。

    在旧版的 Java 中,日期时间 API 存在诸多问题,其中有:

    • 非线程安全:java.util.Date 是非线程安全的,所有的日期类都是可变的,这是Java日期类最大的问题之一。
    • 设计很差
      • 在java.util和java.sql的包中都有日期类。
      • 此外用于格式化和解析的类在java.text包中定义
      • java.util.Date同时包含日期和时间,而java.sql.Date仅包含日期,将其纳入java.sql包并不合理。
      • 这两个类都有相同的名字,不易区分。
    • 时区处理麻烦:日期类并不提供国际化,没有时区支持,因此Java引入了java.util.Calendar和java.util.TimeZone类,但他们同样存在上述所有的问题。

    Java 8 在 java.time 包下提供了很多新的 API。以下为两个比较重要的 API:

    • Local(本地) − 简化了日期时间的处理,没有时区的问题。
    • Zoned(时区) − 通过制定的时区处理日期时间。

    新的java.time包涵盖了所有处理日期,时间,日期/时间,时区,时刻(instants),过程(during)与时钟(clock)的操作。


    实例1:获取当前时间的相关信息。

    public class Test01 {
        public static void main(String[] args) {
            //now
            LocalDateTime now = LocalDateTime.now();
            System.out.println(now);    //2020-10-23T19:29:08.064
    
            //localDate
            LocalDate localDate = now.toLocalDate();
            System.out.println("localDate:"+localDate);//2020-10-23
    
            //localTime
            LocalTime localTime = now.toLocalTime();
            System.out.println("localTime:" + localTime); //19:29:08.064
    
            //dayOfYear
            int dayOfYear = now.getDayOfYear();
            System.out.println("dayOfYear:" + dayOfYear);   //297
    
            //dayOfMonth
            int dayOfMonth = now.getDayOfMonth();
            System.out.println("dayOfMonth:" + dayOfMonth); //23
    
            //dayOfWeek
            DayOfWeek dayOfWeek = now.getDayOfWeek();
            System.out.println("dayOfWeek:" + dayOfWeek);   //FRIDAY
    
            //year
            int year = now.getYear();
            System.out.println("year:" + year); //2020
    
            //month
            Month month = now.getMonth();
            System.out.println("month:"+month.getValue());  //10
    
            //hour
            int hour = now.getHour();
            System.out.println("hour:" + hour); //19
    
            //minute
            int minute = now.getMinute();
            System.out.println("minute:" + minute); //29
    
            //second
            int second = now.getSecond();
            System.out.println("second:" + second); //8
    
            //nano
            int nano = now.getNano();
            System.out.println("nano:" + nano); //64000000
        }
    }
    
    

    案例2:解析时间格式的字符串。

    public class Test02 {
        public static void main(String[] args) {
            LocalDate localDate = LocalDate.of(20220, 10, 23);
            System.out.println(localDate);  //+20220-10-23
    
            LocalTime localTime = LocalTime.of(19, 44, 55);
            System.out.println(localTime);  //19:44:55
    
            //解析字符串
            LocalTime time = LocalTime.parse("19:50:50");
            System.out.println(time);   //19:50:50
        }
    }
    
    

    参考:https://www.runoob.com/java/java8-datetime-api.html


  • 相关阅读:
    git 一般的使用操作
    php 导出excel,csv
    安装sublime_text_3
    安装mongodb记录,mongo基本命令记录
    redis 安装记录
    YII2 activeform样式修改
    go 安装下载
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.hbsjz/com.hbsjz.BaseMapActivity}:
    SQLServer创建触发器,更新表
    SQL Server触发器创建、删除、修改、查看示例步骤
  • 原文地址:https://www.cnblogs.com/nieaojie625/p/13865904.html
Copyright © 2011-2022 走看看