Java 现代化日期时间api使用教程_java中日期用什么数据类型

createh54个月前 (03-01)技术教程33

简介

Java 中,处理日期和时间对于许多应用程序都是必不可少的。Java 随着时间的推移而发展,随着 Java 8 的引入,引入了 java.time 包,为日期和时间操作提供了更现代、更全面的API。

旧版 java.util.Date 类(Java 8 之前)

Java 8 之前,Java 使用 java.util.Date 类来表示日期和时间。然而,它存在许多设计问题,并且不易于使用(例如,易变性、日期和时间之间的混淆)。

使用java.util.Date的示例

import java.util.Date;

public class DateExample {
    public static void main(String[] args) {
        // Create a new Date object with the current date and time
        Date date = new Date();
        
        // Print the current date and time
        System.out.println("Current Date and Time: " + date);
        
        // Create a Date object with a specific time (milliseconds since epoch)
        Date specificDate = new Date(2025, 2, 7); // Deprecated way of creating Date (not recommended)
        System.out.println("Specific Date: " + specificDate);
        
        // Get the time in milliseconds since epoch
        long timeMillis = date.getTime();
        System.out.println("Milliseconds since epoch: " + timeMillis);
    }
}

现代 java.time 类(Java 8 及更高版本)

LocalDate – 表示不带时间的日期(年、月、日)

只需要日期(不需要时间或时区)时使用此类

import java.time.LocalDate;

public class LocalDateExample {
    public static void main(String[] args) {
        // Current date
        LocalDate currentDate = LocalDate.now();
        System.out.println("Current Date: " + currentDate);
        
        // Specific date
        LocalDate specificDate = LocalDate.of(2025, 2, 7);
        System.out.println("Specific Date: " + specificDate);
        
        // Getting date information
        System.out.println("Year: " + currentDate.getYear());
        System.out.println("Month: " + currentDate.getMonth());
        System.out.println("Day of Month: " + currentDate.getDayOfMonth());
    }
}
  • 获取当前日期并添加一天
LocalDate tomorrow = LocalDate.now().plusDays(1);
  • 获取当前日期并减去一个月
LocalDate previousMonthSameDay = LocalDate.now().minus(1, ChronoUnit.MONTHS);
  • 解析日期 2016-06-12 并分别获得星期几和第几个月份。返回值:第一个是一个表示 DayOfWeek 的对象,而第二个是一个表示月份序数的 int
DayOfWeek sunday = LocalDate.parse("2016-06-12").getDayOfWeek();

int twelve = LocalDate.parse("2016-06-12").getDayOfMonth();
  • 测试某个日期是否是闰年
boolean leapYear = LocalDate.now().isLeapYear();
  • 确定一个日期与另一个日期的关系是发生在另一个日期之前还是之后
boolean notBefore = LocalDate.parse("2016-06-12")
  .isBefore(LocalDate.parse("2016-06-11"));

boolean isAfter = LocalDate.parse("2016-06-12")
  .isAfter(LocalDate.parse("2016-06-11"));
  • 可以从给定日期获得日期边界
LocalDateTime beginningOfDay = LocalDate.parse("2016-06-12").atStartOfDay();
LocalDate firstDayOfMonth = LocalDate.parse("2016-06-12")
  .with(TemporalAdjusters.firstDayOfMonth());

LocalTime – 表示不带日期或时区的时间(小时、分钟、秒、纳秒)

import java.time.LocalTime;

public class LocalTimeExample {
    public static void main(String[] args) {
        // Current time
        LocalTime currentTime = LocalTime.now();
        System.out.println("Current Time: " + currentTime);
        
        // Specific time
        LocalTime specificTime = LocalTime.of(14, 30, 15);
        System.out.println("Specific Time: " + specificTime);
        
        // Getting time information
        System.out.println("Hour: " + currentTime.getHour());
        System.out.println("Minute: " + currentTime.getMinute());
        System.out.println("Second: " + currentTime.getSecond());
    }
}
  • 解析字符串创建 LocalTime
LocalTime sixThirty = LocalTime.parse("06:30");
  • 工厂方法也可用于创建 LocalTime
LocalTime sixThirty = LocalTime.of(6, 30);
  • 联合 plus 方法一起使用
LocalTime sevenThirty = LocalTime.parse("06:30").plus(1, ChronoUnit.HOURS);
  • 获取小时数
int six = LocalTime.parse("06:30").getHour();
  • 检查特定时间是在另一个特定时间之前还是之后
boolean isbefore = LocalTime.parse("06:30").isBefore(LocalTime.parse("07:30"));
  • 通过 LocalTime 类中的常量获取一天的最大、最小和中午时间
LocalTime maxTime = LocalTime.MAX

// 输出: 23:59:59.99

LocalDateTime – 表示日期和时间,但不包含时区

import java.time.LocalDateTime;

public class LocalDateTimeExample {
    public static void main(String[] args) {
        // Current date and time
        LocalDateTime currentDateTime = LocalDateTime.now();
        System.out.println("Current Date and Time: " + currentDateTime);
        
        // Specific date and time
        LocalDateTime specificDateTime = LocalDateTime.of(2025, 2, 7, 14, 30);
        System.out.println("Specific Date and Time: " + specificDateTime);
    }
}
  • 使用 parse 解析字符串创建日期时间
LocalDateTime.parse("2015-02-20T06:30:00");
  • plusDays 添加天数
localDateTime.plusDays(1);
  • minusHours 减小时数
localDateTime.minusHours(2);
  • 获取月份
localDateTime.getMonth();

ZonedDateTime – 表示带时区的日期和时间

当需要考虑时区(例如 UTC、PST 等)时使用此类

import java.time.ZonedDateTime;
import java.time.ZoneId;

public class ZonedDateTimeExample {
    public static void main(String[] args) {
        // Current date and time in a specific timezone
        ZonedDateTime zonedDateTime = ZonedDateTime.now(ZoneId.of("America/New_York"));
        System.out.println("Current Date and Time in New York: " + zonedDateTime);
        
        // Specific date and time in a timezone
        ZonedDateTime specificZonedDateTime = ZonedDateTime.of(2025, 2, 7, 14, 30, 0, 0, ZoneId.of("Europe/London"));
        System.out.println("Specific Date and Time in London: " + specificZonedDateTime);
    }
}
  • 获取所有可用的时区
Set allZoneIds = ZoneId.getAvailableZoneIds();
  • 使用 parse 方法解析特定时区的日期时间
ZonedDateTime.parse("2015-05-03T10:15:30+01:00[Europe/Paris]");

Instant – 表示 UTC 中的某个时间点(时间戳)

此类用于时间戳,即自 Unix 纪元 (1970-01-01T00:00:00Z) 以来的秒数或毫秒数

import java.time.Instant;

public class InstantExample {
    public static void main(String[] args) {
        // Current timestamp
        Instant currentInstant = Instant.now();
        System.out.println("Current Timestamp: " + currentInstant);
        
        // Convert Instant to milliseconds since epoch
        long millisecondsSinceEpoch = currentInstant.toEpochMilli();
        System.out.println("Milliseconds since epoch: " + millisecondsSinceEpoch);
    }
}

Duration 和 Period - 测量两个 java.time 对象之间的时间

  • Duration:用于以秒和纳秒为单位测量时间
  • Period:用于测量年、月、日的时间
import java.time.Duration;
import java.time.LocalDateTime;

public class DurationExample {
    public static void main(String[] args) {
        LocalDateTime startTime = LocalDateTime.of(2025, 2, 7, 14, 0, 0);
        LocalDateTime endTime = LocalDateTime.of(2025, 2, 7, 16, 0, 0);
        
        Duration duration = Duration.between(startTime, endTime);
        System.out.println("Duration: " + duration.toHours() + " hours");
    }
}
  • Period 的用法
int five = Period.between(initialDate, finalDate).getDays();

格式化和解析日期


java.time.format.DateTimeFormatter 类用于格式化和解析日期和时间

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class DateFormatExample {
    public static void main(String[] args) {
        // Formatting
        LocalDate date = LocalDate.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String formattedDate = date.format(formatter);
        System.out.println("Formatted Date: " + formattedDate);
        
        // Parsing
        String dateString = "2025-02-07";
        LocalDate parsedDate = LocalDate.parse(dateString, formatter);
        System.out.println("Parsed Date: " + parsedDate);
    }
}

在传统日期和 java.time 之间转换

将 java.util.Date 转换为 java.time.LocalDate

import java.util.Date;
import java.time.LocalDate;
import java.time.ZoneId;

public class ConvertDateExample {
    public static void main(String[] args) {
        Date legacyDate = new Date();
        LocalDate localDate = legacyDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
        System.out.println("Converted LocalDate: " + localDate);
    }
}

将 java.time.LocalDate 转换为 java.util.Date

import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Date;

public class ConvertLocalDateExample {
    public static void main(String[] args) {
        LocalDate localDate = LocalDate.now();
        Date legacyDate = Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
        System.out.println("Converted java.util.Date: " + legacyDate);
    }
}

相关文章

Thread.onSpinWait()有什么作用?为什么要睡眠0毫秒?

概述今天在整理之前学习资料时,偶然看见之前自己写的demo:public class MyTest { static volatile boolean temp = true; pu...

Java并发编程实战:全面指南_java并发编程深度解析

Java并发编程实战:全面指南引言Java并发编程是现代软件开发中不可或缺的一部分,尤其是在处理高并发请求、大规模数据处理和实时系统时。随着多核处理器的普及,充分利用CPU资源成为了提升应用程序性能的...

面试官:讲讲雪花算法,越详细越好

前面文章在谈论分布式唯一ID生成的时候,有提到雪花算法,这一次,我们详细点讲解,只讲它。SnowFlake算法据国家大气研究中心的查尔斯·奈特称,一般的雪花大约由10^19个水分子组成。在雪花形成过程...

Java:代码世界的常青树,就业前景大揭秘

Java:当下依旧火热在当今数字化浪潮中,Java 作为一门经典的编程语言,始终占据着技术舞台的 C 位。先来看一组令人震撼的数据:根据 IDC 统计,全球范围内对 Java 开发工程师的需求竟达到全...

哪种编程语言又快又省电?有人对比了27种语言

编辑:小舟、张倩在手机快没电时,管理软件往往会提醒我们关掉某些耗电量高的应用。可见,除了硬件厂商外,软件厂商也应该重视能耗问题。在这篇文章中,研究者分析了一下各种编程语言的能耗对比。当能耗也成为了一个...

JDK 21虚拟线程:Java并发编程的革新利器

一、引言:并发编程的新曙光嘿,各位Java开发者!是不是每次面对高并发场景,都感觉像是在打一场硬仗?传统线程就像是一群“老炮儿”,虽然经验丰富,但在高并发的战场上,它们的“腿脚”越来越不灵便了。想象一...