Java,日期时间API

JDK8.0之前的API

1.System类的currentTimeMillis():

  • 获取当前时间对应的毫秒数,long类型,时间戳
  • 当前时间与1970年1月1日0时0分0秒之间的毫秒数

2.两种Date类:

java.util.Date
  • 构造器:Date date1 = new Date();//创建一个基于当前系统时间的对象
  • 常用方法:getTime()、toString()(可得到年月日星期时分秒,如Tue Dec 02 08:03:24 CST 2025)
java 复制代码
@Test
    public void test1()
    {
        Date date1 = new Date();//创建一个基于当前系统时间的对象
        System.out.println(date1.toString());//Tue Dec 02 08:03:24 CST 2025
        long milliTimes = date1.getTime();//得到一个对应时间的时间戳
        System.out.println(milliTimes);//1764598448012
    }
java.sql.Date
  • 构造器:java.sql.Date date2 = new java.sql.Date(1764598448012L);//创建一个基于指定时间戳的对象
  • 常用方法:getTime()、toString()(仅可得到年月日,如2025-12-01)
java 复制代码
@Test
    public void test2()
    {
        java.sql.Date date1 = new java.sql.Date(1764598448012L);////创建一个基于指定时间戳的对象
        System.out.println(date1.toString());//2025-12-01
        System.out.println(date1.getTime());//1764598448012
    }

3.SimpleDateFormat类:

  • 用于日期时间的格式化和解析。
  • 格式化:日期 ----> 字符串
    • format(Date date)
  • 解析:字符串 ----> 日期
    • parse(String str)//String需要以对应格式才能解析
java 复制代码
 @Test
    public void test3() throws ParseException
    {
        SimpleDateFormat sdf = new SimpleDateFormat();
        Date date1 = new Date();
        String strDate = sdf.format(date1);//格式化:日期 ----> 字符串
        System.out.println(strDate);//2025/12/2 上午8:10

        Date date2 = sdf.parse("2025/12/2 上午8:10");//解析:字符串 ----> 日期
        System.out.println(date2);//Tue Dec 02 08:10:00 CST 2025
    }
  • 构造器:SimpleDateFormat sdf = new SimpleDateFormat(String pattern);

    • 空参构造器:为默认格式
    • 有参构造器:为指定格式
    java 复制代码
    @Test
        public void test4()
        {
            //以指定格式,对Date进行格式化
            SimpleDateFormat sdf = new SimpleDateFormat("EEE,d MMM yyyy HH:mm:ss Z");
            Date date1 = new Date();
            String sdfStr = sdf.format(date1);
            System.out.println(sdfStr);//周二,2 12月 2025 08:20:46 +0800
    
            sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            sdfStr = sdf.format(date1);
            System.out.println(sdfStr);//2025-12-02 08:23:08
        }

4.Calender类(日历类)//抽象类:

  • 实例化:由于Calendar是一个抽象类,所以需要创建其子类的实例,可通过Calendar的静态方法getInstance()即可获得

  • 常用方法:

    • get(int filed):得到当前指定日期天数
    • set(int filed,int xx):设定为指定天数
    • add(int filed):添加指定天数
    • getTime():Calendar ----> Date
    • setTime():使用指定Date重置Calendar
    java 复制代码
    @Test
        public void test5()
        {
            Calendar calendar = Calendar.getInstance();
            System.out.println(calendar.getClass());//class java.util.GregorianCalendar
    
            //get(int filed)
            System.out.println(calendar.get(Calendar.DAY_OF_MONTH));//2
            System.out.println(calendar.get(Calendar.DAY_OF_YEAR));//336
    
            //set(int filed, int xx)
            calendar.set(Calendar.DAY_OF_MONTH,23);
            System.out.println(calendar.get(Calendar.DAY_OF_MONTH));//23
    
            //add(int filed, int amount)
            calendar.add(Calendar.DAY_OF_MONTH,4);
            System.out.println(calendar.get(Calendar.DAY_OF_MONTH));//27
    
            //getTime():Calendar ----> Date
            Date date = calendar.getTime();
            System.out.println(date);//Sat Dec 27 08:53:52 CST 2025
    
            //setTime():使用指定Date重置Calendar
            Date date1 = new Date();
            calendar.setTime(date1);
            System.out.println(calendar.get(Calendar.DAY_OF_MONTH));//2
        }

JDK8.0中的API

5.JDK8.0之前的API过时的原因:

  • 可变性:像日期时间的类应该是不可变的。
  • 偏移性:Date中的年份是从1900开始的,月份从0开始的。
  • 格式化:格式化只对Date有用,Calendar则不行。
  • 此外,它们也不是线程安全的,不能处理润秒等。
  • 所以,Java就引入了java.time来取代之前的API

2.LocalDate、LocalTime和LocalDateTime类:

  • 实例化:

    • now();获取当前日期和时间对应的实例
    • of();获取指定的日期、时间对应的实例
    java 复制代码
    @Test
        public void test1()
        {
            //now();获取当前日期和时间对应的实例
            LocalDate localDate = LocalDate.now();
            LocalTime localTime = LocalTime.now();
            LocalDateTime localDateTime = LocalDateTime.now();
            System.out.println(localDate);//2025-12-02
            System.out.println(localTime);//10:26:11.893155300
            System.out.println(localDateTime);//2025-12-02T10:26:11.893155300
    
            //of();获取指定的日期、时间对应的实例
            LocalDate localDate1 = LocalDate.of(2025,12,2);
            LocalTime localTime1 = LocalTime.of(10,26,11);
            LocalDateTime localDateTime1 = LocalDateTime.of(2025,12,2,10,29,12);
            System.out.println(localDate1);//2025-12-02
            System.out.println(localTime1);//10:26:11
            System.out.println(localDateTime1);//2025-12-02T10:29:12
        }
  • 常用方法:

    • getXxx():类似get()
    • withXxx():类似set()
    • plusXxx():类似add()
    java 复制代码
    @Test
    public void test2()
    {
        //getXxx()
        LocalDateTime localDateTime = LocalDateTime.now();
        System.out.println(localDateTime.getDayOfMonth());//2
    
        //体现不可变性
        //withXxx()
        LocalDateTime localDateTime1 = localDateTime.withDayOfMonth(12);
        System.out.println(localDateTime);//2025-12-02T10:38:03.642448400
        System.out.println(localDateTime1);//2025-12-02T10:38:03.642448400
        System.out.println(localDateTime.getDayOfMonth());//2
        System.out.println(localDateTime1.getDayOfMonth());//12
    
        //plusXxx()
        LocalDateTime localDateTime2 = localDateTime1.plusDays(5);
        System.out.println(localDateTime1);//2025-12-12T10:40:05.247582800
        System.out.println(localDateTime2);//2025-12-17T10:40:05.247582800
        System.out.println(localDateTime1.getDayOfMonth());//12
        System.out.println(localDateTime2.getDayOfMonth());//17
    }

6.Instant类:

  • Instant:类似于Date类,时间线上的一个瞬时点,用来记录应用程序中时间的时间戳。

  • 实例化:

    • now():以当前格林威治时间对应的实例。
    • ofEpochMilli(long epochMilli):以指定格林威治时间对应的实例。
  • 常用方法:

    • toEpochMilli():获取对应Instant对象的时间戳,类似于Date().getTime()。
    java 复制代码
    @Test
    public void test3()
    {
        //now();默认为格林威治时间
        Instant instant = Instant.now();
        System.out.println(instant);//2025-12-02T02:50:50.636435400Z
    
        //设置时间为东八区时间
        OffsetDateTime offsetDateTime = instant.atOffset(ZoneOffset.ofHours(8));
        System.out.println(offsetDateTime);//2025-12-02T10:53:24.552167200+08:00
    
        //ofEpochMilli();
        Instant instant1 = Instant.ofEpochMilli(32312341232L);
        System.out.println(instant1);//1971-01-09T23:39:01.232Z
    
        //toEpochMilli();
        long milliTime = instant.toEpochMilli();
        System.out.println(milliTime);//1764644598934
        System.out.println(new Date().getTime());//1764644598940
    }

7.DateTimeFormatter类:

  • 类似于SimpleDateFormat。

  • 实例化:(自定义格式)

    • DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
  • 常用方法:

    • 格式化:format()
    • 解析:from()
    java 复制代码
    @Test
    public void test4()
    {
        //自定义格式:如ofPattern("yyyy-MM-dd HH:mm:ss")
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    
        //格式化:日期、时间 ----> 字符串
        LocalDateTime localDateTime = LocalDateTime.now();
        String strDateTime = dateTimeFormatter.format(localDateTime);
        System.out.println(strDateTime);//2025-12-02 11:19:45
    
        //解析:字符串 ----> 日期、时间
        TemporalAccessor parse = dateTimeFormatter.parse("2025-12-02 15:19:45");
        LocalDateTime localDateTime1 = LocalDateTime.from(parse);;
        System.out.println(localDateTime1);//2025-12-02T15:19:45
    }
相关推荐
前端老曹23 分钟前
Jspreadsheet CE V5 使用手册(保姆版) 二
开发语言·前端·vue.js·学习
秋邱24 分钟前
AR 定位技术深度解析:从 GPS 到视觉 SLAM 的轻量化实现
开发语言·前端·网络·人工智能·python·html·ar
Boop_wu26 分钟前
[Java EE] 多线程进阶(3) [线程安全集合类]
开发语言·windows·python
老华带你飞28 分钟前
动物救助|流浪狗救助|基于Springboot+vue的流浪狗救助平台设计与实现(源码+数据库+文档)
java·数据库·vue.js·spring boot·论文·毕设·流浪动物救助平台
雨疏风骤124029 分钟前
【FreeRTOS】任务、任务状态
开发语言·stm32·c#·rtos
Hernon31 分钟前
微服务架构设计:从零打造互联网车贷系统 —— 业务背景与架构蓝图
java·微服务·架构·微服务架构设计
SongYuLong的博客33 分钟前
开源 C 标准库(C Library)
c语言·开发语言·开源
9523633 分钟前
二叉平衡树
java·数据结构·学习·算法
木棉知行者33 分钟前
(二)Python基本语句
开发语言·python