java获取指定年(月)的第一天(秒)和最后一天(秒)

该代码片段展示了如何使用Java 8的LocalDate和LocalDateTime类来获取指定年份和月份的第一天及最后一天、第一秒及最后一秒。这些方法包括firstDateOfYear(), lastDateOfYear(), firstDateOfMonth(), lastDateOfMonth(), firstTimeOfYear(), lastTimeOfYear(), firstTimeOfMonth()和lastTimeOfMonth(),它们提供了计算日期和时间边界值的简洁方式。

直接上代码

public class LocalDateUtil {

    public static void main(String[] args) {
        System.out.println("firstDateOfYear: " + firstDateOfYear(2023));
        System.out.println("lastDateOfYear: " + lastDateOfYear(2023));
        System.out.println("firstDateOfMonth: " + firstDateOfMonth(2023, 2));
        System.out.println("lastDateOfMonth: " + lastDateOfMonth(2023, 2));
        System.out.println("firstTimeOfYear: " + firstTimeOfYear(2023));
        System.out.println("lastTimeOfYear: " + lastTimeOfYear(2023));
        System.out.println("firstTimeOfMonth: " + firstTimeOfMonth(2023, 2));
        System.out.println("lastTimeOfMonth: " + lastTimeOfMonth(2023, 2));
    }

    /**
     * 指定年份的第一天
     *
     * @param year 年份
     */
    public static LocalDate firstDateOfYear(int year) {
        return LocalDate.ofYearDay(year, 1);
    }

    /**
     * 指定年份的最后一天(用指定年份的第一天加1年,再减去1天)
     *
     * @param year
     * @return
     */
    public static LocalDate lastDateOfYear(int year) {
        return firstDateOfYear(year).plusYears(1).minusDays(1);
    }

    /**
     * 指定年月的第一天
     *
     * @param year  年份
     * @param month 月份
     */
    public static LocalDate firstDateOfMonth(int year, int month) {
        return LocalDate.of(year, month, 1);
    }

    /**
     * 指定年月的最后一天(用指定年月的第一天加1月,再减去1天)
     *
     * @param year  年份
     * @param month 月份
     */
    public static LocalDate lastDateOfMonth(int year, int month) {
        return firstDateOfMonth(year, month).plusMonths(1).minusDays(1);
    }

    /**
     * 指定年份的第一秒
     *
     * @param year
     * @return
     */
    public static LocalDateTime firstTimeOfYear(int year) {
        return LocalDateTime.of(LocalDate.ofYearDay(year, 1), LocalTime.MIN);
    }

    /**
     * 指定年份的最后一秒(用指定年份的第一秒加1年,再减去1秒)
     *
     * @param year
     * @return
     */
    public static LocalDateTime lastTimeOfYear(int year) {
        return firstTimeOfYear(year).plusYears(1).minusSeconds(1);
    }

    /**
     * 指定年月的第一秒
     *
     * @param year  年份
     * @param month 月份
     */
    public static LocalDateTime firstTimeOfMonth(int year, int month) {
        return LocalDateTime.of(LocalDate.of(year, month, 1), LocalTime.MIN);
    }

    /**
     * 指定年月的最后一秒(用指定年月的第一秒加1月,再减去1秒)
     *
     * @param year  年份
     * @param month 月份
     */
    public static LocalDateTime lastTimeOfMonth(int year, int month) {
        return firstTimeOfMonth(year, month).plusMonths(1).minusSeconds(1);
    }
}

Java获取指定份的第一天最后一天,可以通过`Calendar`类或`java.time`包实现。以下是两种实现方式。 ### 使用 `Calendar` 类 ```java import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; public class Main { public static void main(String[] args) { int year = 2023; int month = Calendar.FEBRUARY; // 注意:份从 0 开始,0 表示一 // 获取指定第一天 Calendar firstDayCal = Calendar.getInstance(); firstDayCal.set(year, month, 1); Date firstDay = firstDayCal.getTime(); System.out.println("First day of the month: " + new SimpleDateFormat("yyyy-MM-dd").format(firstDay)); // 获取指定最后一天 Calendar lastDayCal = Calendar.getInstance(); lastDayCal.set(year, month, 1); int lastDay = lastDayCal.getActualMaximum(Calendar.DAY_OF_MONTH); lastDayCal.set(year, month, lastDay); Date lastDayDate = lastDayCal.getTime(); System.out.println("Last day of the month: " + new SimpleDateFormat("yyyy-MM-dd").format(lastDayDate)); } } ``` ### 使用 `java.time` 包(推荐) 从 Java 8 开始,`java.time` 包提供了更简洁线程安全的方式处理日期时间。 ```java import java.time.LocalDate; import java.time.format.DateTimeFormatter; public class Main { public static void main(String[] args) { int year = 2023; int month = 2; // 注意:份从 1 开始,2 表示二 // 获取指定第一天 LocalDate firstDay = LocalDate.of(year, month, 1); System.out.println("First day of the month: " + firstDay.format(DateTimeFormatter.ISO_LOCAL_DATE)); // 获取指定最后一天 LocalDate lastDay = firstDay.withDayOfMonth(firstDay.lengthOfMonth()); System.out.println("Last day of the month: " + lastDay.format(DateTimeFormatter.ISO_LOCAL_DATE)); } } ``` ### 解释 1. **Calendar 类**: - 使用 `Calendar.set(year, month, 1)` 设置到指定第一天。 - 使用 `getActualMaximum(Calendar.DAY_OF_MONTH)` 获取最后一天的天数。 - 通过 `set(year, month, lastDay)` 设置到最后一天。 2. **java.time 包**: - 使用 `LocalDate.of(year, month, 1)` 创建指定第一天。 - 使用 `withDayOfMonth(lengthOfMonth())` 获取最后一天。 这两种方法都可以有效地获取指定第一天最后一天。`java.time` 包是现代的日期时间API,推荐使用这种方式,因为它更加直观且线程安全[^2]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值