Java日期和时间(二)

本文详细介绍了Java8中新增的日期和时间处理类,包括LocalDate、LocalTime、LocalDateTime、ZonedDateTime、Instant、DateTimeFormatter、Period和Duration,以及它们各自的功能、获取和修改时间的方法,以及如何使用它们进行时间间隔计算和格式化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

新增的日期和时间

为什么要学习新增的日期和时间

1、代替Calendar

        LocalDate:年、月、日

        LocalTime:时、分、秒

        LocalDateTime:年、月、日、时、分、秒

        ZoneId:时区

        ZoneldDatetime:带时区的时间

2、代替Date 

        Instant:时间戳/时间线

 3、代替SimpleDateForamt

        DateTimeFormatter:用于时间的格式化和解析

4、其他补充 

        Period:时间间隔(年、月、日)

        Duration:时间间隔(时、分、秒,纳秒)

LocalDate、LocalTime、LocalDateTime

LocalDate:代表本地日期(年、月、日、星期)

LocalTime:代表本地时间(时、分、秒、纳秒)

LocalDateTime:代表本地日期、时间(年、月、日、星期、时、分、秒、纳秒)

它们获取对象的方案

方法名示例
public static Xxxx now():获取系统当前时间对应的该对象

LocalDate ld = LocaDate.now();

LocalTime ld = LocalTime.now();

LocalDateTime ld = LocalDateTime.now();

public static Xxxx of(…):获取指定时间的对象

LocalDate localDate1 = LocalDate.of(2099,11,11)

LocalTime localTime1 = LocalTime.of(9,8,59)

LocalDateTime localDateTime1 = LocalDateTime.of(2025,11,16,14,30,01)

LocalDate 

LocalDate的常用API(都是处理年、月、日、星期相关的)
方法名说明
public int geYear()获取年
public int getMonthValue()获取月份 (1-12)
public int getDayOfMonth()获取日
public int getDayOfYear()获取当前是一年中的第几天
public DayOfWeek getDayOfWeek()获取星期几:ld.getDayOfWeek().getValue()
方法名说明
withYear、withMonth、withDayOfMonth、 withDayOfYear直接修改某个信息,返回新日期对象
plusYears、plusMonths、plusDays、plusWeeks把某个信息加多少,返回新日期对象
minusYears、minusMonths、minusDays, minusWeeks把某个信息减多少,返回新日期对象
equals isBefore isAften判断两个日期对象,是否相等,在前还是在后
import java.time.LocalDate;

public class Test {
    public static void main(String[] args){
        // 获取本地日期对象(不可变对象)
        LocalDate ld = LocalDate.now(); // 年 月 日
        System.out.println(ld);

        // 获取日期对象中的信息
        int year = ld.getYear(); // 年
        int month = ld.getMonthValue(); // 月(1-12)
        int day = ld.getDayOfMonth(); // 日
        int dayOfYear = ld.getDayOfYear(); // 一年中的第几天
        int dayOfWeek = ld.getDayOfWeek().getValue(); // 星期几

        // 直接修改某个信息:withYear、withMonth、withDayOfMonth、withDayOfYear
        LocalDate ld2 = ld.withYear(2099);
        LocalDate ld3 = ld.withMonth(12);
        System.out.println(ld2);
        System.out.println(ld3);
        System.out.println(ld);

        // 把某个信息加多少:plusYears、plusMonths、plusDays、plusWeeks
        LocalDate ld4 = ld.plusYears(2);
        LocalDate ld5 = ld.plusMonths(2);

        // 把某个信息减多少:minusYears、minusMonths、minusDays、minusWeeks
        LocalDate ld6 = ld.minusYears(2);
        LocalDate ld7 = ld.minusMonths(2);

        // 获取指定日期的LocalDate对象
        LocalDate ld8 = LocalDate.of(2099,12,12);
        LocalDate ld9 = LocalDate.of(2099,12,12);

        // 判断2个日期对象是否相等,再前还是再后:equals isBefore isAfter
        System.out.println(ld8.equals(ld9));
        System.out.println(ld8.isAfter(ld));
        System.out.println(ld8.isBefore(ld));
    }
}

LocalTime

LocalTime的常用API(都是处理时、分、秒、纳秒相关的)
方法名说明
public int getHour()获取小时
public int getMinute()获取分
public int getSecond()获取秒
public int getNano()获取纳秒
方法名说明
withYear、withMonth、withDayOfMonth、 withDayOfYear直接修改某个信息,返回新日期对象
plusYears、plusMonths、plusDays、plusWeeks把某个信息加多少,返回新日期对象
minusYears、minusMonths、minusDays, minusWeeks把某个信息减多少,返回新日期对象
equals isBefore isAften判断两个日期对象,是否相等,在前还是在后
import java.time.LocalTime;

public class Test {
    public static void main(String[] args){
        // 获取本地时间对象
        LocalTime lt = LocalTime.now(); // 时 分 秒
        System.out.println(lt);

        // 获取时间中的信息
        int hour = lt.getHour(); // 时
        int minute = lt.getMinute(); // 分
        int second = lt.getSecond(); // 秒
        int nano = lt.getNano(); // 纳秒

        // 修改时间
        LocalTime lt2 = lt.withHour(10);
        LocalTime lt3 = lt.withMinute(10);
        LocalTime lt4 = lt.withSecond(10);
        LocalTime lt5 = lt.withNano(10);

        // 加多少:plusHours、plusMinutes、plusSeconds、plusNanos
        LocalTime lt6 = lt.plusHours(10);
        LocalTime lt7 = lt.plusMinutes(10);

        // 减多少:minusHours、minusMinutes、minusSeconds、minusNanos
        LocalTime lt8 = lt.minusHours(10);
        LocalTime lt9 = lt.minusMinutes(10);

        // 获取指定时间的LocalTime对象
        LocalTime lt10 = LocalTime.of(12,12,12);
        LocalTime lt11 = LocalTime.of(12,12,12);

        // 判断2个日期对象是否相等,再前还是再后:equals isBefore isAfter
        System.out.println(lt10.equals(lt11));
        System.out.println(lt10.isAfter(lt));
        System.out.println(lt10.isBefore(lt));
    }
}

LocalDateTime

LocalTime的常用API(都是处理年、月、日、时、分、秒、纳秒相关的)
方法名说明

getYear、getMonthValue、getDayOfMonth、getDayOfYear、

getDayOfWeek、getHour、getMinute、getSecond、getNano

获取年月日、时分秒、纳秒等

withYear、 withMonth、withDayOfMonth、 withDayOfYear、

withHour、withMinute、 withSecond、withNano

修改某个信息,返回新日期时间对象
plusYears、plusMonths、plusDays、 plusWeeks、plusHours、plusMinutes、plusSeconds、plusNanos把某个信息加多少,返回新日期时间对象
minusYears、 minusMonths、minusDays、minusWeeks、
minusHours、minusMinutes、 minusSeconds、minusNanos
把某个信息减多少,返回新日期时间对象
equals isBefore isAfter判断2个时间对象,是否相等,在前还是在后
import java.time.LocalDateTime;

public class Test {
    public static void main(String[] args){
        // 获取本地日期、时间对象
        LocalDateTime ldt = LocalDateTime.now(); // 年 月 日 时 分 秒
        System.out.println(ldt);

        // 获取日期、时间中的信息
        int year = ldt.getYear(); // 年
        int month = ldt.getMonthValue(); // 月(1-12)
        int day = ldt.getDayOfMonth(); // 日
        int dayOfYear = ldt.getDayOfYear(); // 一年中的第几天
        int dayOfWeek = ldt.getDayOfWeek().getValue(); // 星期几
        int hour = ldt.getHour(); // 时
        int minute = ldt.getMinute(); // 分
        int second = ldt.getSecond(); // 秒
        int nano = ldt.getNano(); // 纳秒

        // 修改时间
        LocalDateTime ldt2 = ldt.withYear(2099);
        LocalDateTime ldt3 = ldt.withMinute(59);

        // 加多少
        LocalDateTime ldt4 = ldt.plusYears(2);
        LocalDateTime ldt5 = ldt.plusMinutes(3);

        // 减多少
        LocalDateTime ldt6 = ldt.minusYears(2);
        LocalDateTime ldt7 = ldt.minusMinutes(3);

        // 获取指定日期、时间的LocalDateTime对象
        LocalDateTime ldt8 = LocalDateTime.of(2099,12,12,12,12,12);
        LocalDateTime ldt9 = LocalDateTime.of(2099,12,12,12,12,12);

        // 判断2个日期、时间对象是否相等,再前还是再后:equals isBefore isAfter
        System.out.println(ldt8.equals(ldt9));
        System.out.println(ldt8.isAfter(ldt));
        System.out.println(ldt8.isBefore(ldt));
    }
}

Zoneld、ZonedDateTime

Zoneld:代表时区id

Zoneld时区的常见方法

方法名说明
public static Set<String> getAvailableZonelds()获取Java中支持的所有时区
public static Zoneld systemDefault()获取系统默认时区
public static Zoneld of(String zoneld)获取一个指定时区

ZonedDateTime 

ZonedDateTime带时区时间的常见方法

方法名说明
public static ZonedDateTime now()获取当前时区的ZonedDateTime对象
public static ZonedDateTime now(Zoneld zone)获取指定时区的ZonedDateTime对象
getYear、getMonthValue、getDayofMonth、getDayOfYeargetbayofWeek、
getHour、getMinute、getSecond、getNano
获取年月日、时分秒、纳秒等
public ZonedDateTime withxxx(时间)修改时间系列的方法
public ZonedDateTime minusxxx(时间)减少时间系列的方法
public ZonedDateTime plusxxx(时间)增加时间系列的方法
import java.time.Clock;
import java.time.ZoneId;
import java.time.ZonedDateTime;

public class Test {
    public static void main(String[] args){
        // Zoneld的常见方法
        // 获取系统默认的时区
        ZoneId zoneId = ZoneId.systemDefault();
        System.out.println(zoneId.getId());
        System.out.println(zoneId);

        // 获取Java支持的全部时区id
        System.out.println(ZoneId.getAvailableZoneIds());

        // 把某个时区id封装成Zoneid对象
        ZoneId zoneId1 = ZoneId.of("America/New_York");

        // ZonedDateTime:带时区的时间
        // 获取系统某个时区的ZondDateTime对象
        ZonedDateTime now = ZonedDateTime.now(zoneId1);
        System.out.println(now);

        // 世界标准时间
        ZonedDateTime now1 = ZonedDateTime.now(Clock.systemUTC());
        System.out.println(now1);

        // 获取系统默认时区的ZondDateTime对象
        ZonedDateTime now2 = ZonedDateTime.now();
        System.out.println(now2);
    }
}

Instant  时间线上的某个时刻/时间戳

  • 通过获取Instant的对象可以拿到此刻的时间,该时间由两部分组成:从1970-01-01  00:00:00开始走到此刻的总秒数 + 不够一秒的纳秒数
方法名说明
public static Instant now()获取当前时间的Instant对象 (标准时间)
public long getEpochSecond()获取从1970-01-01T00:00: 00开始记录的秒数
public int getNano()从时间线开始,获取从第二个开始的纳秒数
plusMillis plusSeconds plusNanos判断系列的方法
minusMillis minusSeconds minusNanos减少时间系列的方法
equals、isBefore、 isAfter增加时间系列的方法
import java.time.Instant;

public class Test {
    public static void main(String[] args){
        // 创建Instant对象,获取此刻时间信息
        Instant now = Instant.now();

        // 获取总秒数
        long second = now.getEpochSecond();
        System.out.println(second);

        // 不够一秒的纳秒数
        int nano = now.getNano();
        System.out.println(nano);

        // 增加、减少时间
        Instant instant1 = now.plusNanos(111);
        Instant instant2 = now.minusMillis(2);

        System.out.println(now);

        // Instant对象的作用:做代码的性能分析,或者记录用户的操作时间点
        Instant now1 = Instant.now();
        // 代码行……
        Instant now2 = Instant.now();
    }
}
  • 作用:可以用来记录代码的执行时间,或者记录用户的操作某个时间时间点。
  • 传统的Date类,只能精确到毫秒,并且是可变对象
  • 新增的Instant类,可以精确到纳秒,并且是不可变对象,推荐用Instant代替Date。

DateTimeFormatter

方法名说明
public static DateTimeFormatter ofpattern(时间格式)获取格式化器对象
public String format(时间对象)格式化时间

LocalDateTime提供的格式化、解析时间的方法 

方法名说明
public String format(DateTimeFormatter formatter)格式化时间
public static LocalDateTime parse(CharSequence text, DateTimeFormatter formatter)解析时间
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class Test {
    public static void main(String[] args) {
        // 创建一个日期时间格式化器对象
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");

        // 对时间进行格式化
        LocalDateTime now = LocalDateTime.now();
        System.out.println(now);

        String rs = formatter.format(now);
        System.out.println(rs);

        // 格式化时间的另一种方案
        String rs2 = now.format(formatter);
        System.out.println(rs2);

        // 解析时间
        String dateStr = "2029年12月12日 12:12:12";
        LocalDateTime ldt = LocalDateTime.parse(dateStr,formatter);
        System.out.println(ldt);
    }
}

Period、Duration

Period(一段时期)

  • 可以用于计算两个LocalDate对象相差的年数、月数、天数。
方法名说明
public static Period between(LocalDate start, LocalDate end)传入2个日期对象,得到Period对象
public int getYears()计算隔几年,并返回
public int getMonths()计算隔几个月,并返回
public int getDays()计算隔多少天,并返回
import java.time.LocalDate;
import java.time.Period;

public class Test {
    public static void main(String[] args) {
        LocalDate start = LocalDate.of(2029,8,10);
        LocalDate end = LocalDate.of(2029,12,15);

        // 创建Period对象,封装两个日期对象
        Period period = Period.between(start,end);

        // 通过Period对象获取两个日期对象相差的信息
        System.out.println(period.getYears());
        System.out.println(period.getMonths());
        System.out.println(period.getDays());
    }
}

Duration(持续时间)

  • 可以用于计算两个时间对象相差的天数、小数数、分数、秒数、纳秒数;支持LocalTime、LocalDateTime、Instant等时间。
方法名说明
public static Duration between(开始时间对象1, 截止时间对象2)传入2个时间对象,得到Duration对象
public long toDays()计算隔多少天,并返回
public long toHours()计算隔多少小时,并返回
public long toMinutes()计算隔多少分,并返回
public long toSeconds()计算隔多少秒,井返回
public long toMillis()计算隔多少毫秒,并返回
public long toNanos()计算隔多少纳秒,并返回
import java.time.Duration;
import java.time.LocalDateTime;

public class Test {
    public static void main(String[] args) {
        LocalDateTime start = LocalDateTime.of(2029,11,11,11,10,10);
        LocalDateTime end = LocalDateTime.of(2029,11,11,11,11,11);

        // 创建Duration对象,封装两个时间对象
        Duration duration = Duration.between(start,end);

        // 通过Duration对象获取两个时间对象相差的信息
        System.out.println(duration.toDays());
        System.out.println(duration.toHours());
        System.out.println(duration.toMinutes());
        System.out.println(duration.toMillis());
        System.out.println(duration.toNanos());
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值