【Java 基础】24 日期操作


日期时间是我们常见的操作对象,Java 也提供了强大的日期和时间处理类库,使我们可以方便的进行日期的表示、计算、格式化等。

Java 为我们提供了下面这些类
在这里插入图片描述
其中最常用的是 LocalDateTime

下面让我们逐一介绍一下,类有点多,并不需要我们全记住,当做一个字典去查阅即可。

1.Date

在 Java 早期版本中,主要使用 java.util.Date 类来表示日期和时间

示例代码:

public class Demo {
    public static void main(String[] args) {
        // 获取当前时间
        Date currentDate = new Date();
        System.out.println(currentDate);

        // 获取时间戳
        long timestamp = currentDate.getTime();
        System.out.println(timestamp);
    }
}

运行结果:

Thu Dec 07 20:58:11 CST 2023
1701953891262

2.LocalDate

表示日期 ,提供了丰富的方法来进行日期操作

示例代码:

public class Demo {
    public static void main(String[] args) {
        // 获取当前日期
        LocalDate currentDate = LocalDate.now();
        System.out.println(currentDate);
    }
}

输出结果:

2023-12-07

3.LocalTime

表示时间,提供了丰富的方法来进行时间的操作

示例代码:

public class Demo {
    public static void main(String[] args) {
        // 获取当前时间
        LocalTime currentTime = LocalTime.now();
        System.out.println(currentTime);
    }
}

输出结果:

21:08:21.256

4.LocalDateTime

表示日期时间 ,提供了丰富的方法来进行时间的操作

示例代码:

public class Demo {
    public static void main(String[] args) {
        // 获取当前日期时间
        LocalDateTime currentDateTime = LocalDateTime.now();
        System.out.println(currentDateTime);
    }
}

输出结果:

2023-12-07T21:08:21.256

5.DateTimeFormatter

用于格式化和解析日期时间,它提供了丰富的预定义格式,同时也支持自定义格式

示例代码:

public class Demo {
    public static void main(String[] args) {
        LocalDateTime currentDateTime = LocalDateTime.now();

        // 使用预定义格式
        String formattedDateTime = currentDateTime.format(DateTimeFormatter.ISO_DATE_TIME);
        System.out.println(formattedDateTime);

        // 使用自定义格式
        DateTimeFormatter customFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String customFormattedDateTime = currentDateTime.format(customFormatter);
        System.out.println(customFormattedDateTime);
    }
}

输出结果:

2023-12-07T21:02:23.796
2023-12-07 21:02:23

6.Period

用于表示日期之间的差距

示例代码:

public class Demo {
    public static void main(String[] args) {
        LocalDate startDate = LocalDate.of(2020, 1, 1);
        LocalDate endDate = LocalDate.now();

        // 计算日期之间的差距
        Period period = Period.between(startDate, endDate);
        System.out.println("Years: " + period.getYears() + ", Months: " + period.getMonths() + ", Days: " + period.getDays());
    }
}

输出结果:

Years: 3, Months: 11, Days: 6

7.Duration

用于表示时间之间的差距

示例代码:

public class Demo {
    public static void main(String[] args) {
        LocalDate startDate = LocalDate.of(2020, 1, 1);
        LocalDate endDate = LocalDate.now();

        // 计算时间之间的差距
        long daysDiff = ChronoUnit.DAYS.between(startDate, endDate);
        System.out.println("Days Difference: " + daysDiff);
    }
}

输出结果:

Days Difference: 1436

8.ZoneId

用于表示时区

示例代码:

public class Demo {
    public static void main(String[] args) {
        LocalDateTime localDateTime = LocalDateTime.now();

        // 获取系统默认时区
        ZoneId systemZone = ZoneId.systemDefault();
        ZonedDateTime zonedDateTime = ZonedDateTime.of(localDateTime, systemZone);
        System.out.println(zonedDateTime);
    }
}

输出结果:

2023-12-07T21:07:11.523+08:00[Asia/Shanghai]

9.ZonedDateTime

用于获取日期和时间以及时区信息

示例代码:

public class Demo {
    public static void main(String[] args) {
        LocalDateTime localDateTime = LocalDateTime.now();

        // 获取指定时区
        ZoneId newYorkZone = ZoneId.of("America/New_York");
        ZonedDateTime newYorkDateTime = ZonedDateTime.of(localDateTime, newYorkZone);
        System.out.println(newYorkDateTime);
    }
}

输出结果:

2023-12-07T21:07:11.523-05:00[America/New_York]

总结

Java 提供了丰富的日期和时间处理类,包括 DateLocalDateLocalTimeLocalDateTimeDateTimeFormatterPeriodDurationZoneIdZonedDateTime。选择合适的类取决于具体的需求,但通常推荐使用新的日期和时间 API,例如 LocalDateTime,以获得更好的可读。

  • 9
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值