LocalDate的minusDays方法详解

LocalDate的minusDays方法详解

public LocalDate minusDays(long daysToSubtract)

返回此LocalDate的副本,并减去指定的天数。

该方法根据需要减去月份和年份字段的日期字段减去指定的金额,以确保结果保持有效。 如果超过最大/最小年份,结果将无效。

例如,2009-01-01减去一天会导致2008-12-31。

此实例是不可变的,不受此方法调用的影响。

  • 参数

    daysToSubtract - 减去的日子可能是负数

  • 结果

    一个 LocalDate基于这个日期减去,而不是null

  • 异常

    DateTimeException - 如果结果超出支持的日期范围

import java.time.DayOfWeek;
import java.time.LocalDate;

public class CalendarTest {
    public static void main(String[] args) {
        LocalDate date = LocalDate.now();
        int month = date.getMonthValue();
        System.out.println("month = " + month);
        int today = date.getDayOfMonth();
        System.out.println("today = " + today);

        date = date.minusDays(today - 1);
        System.out.println("date = " + date);

        System.out.println("today - 1 = " + (today - 1));

        LocalDate date2 = LocalDate.now();
        int day = date2.getDayOfMonth();
        date2 = date2.minusDays(15);

        System.out.println("day = " + day);
        System.out.println("date2 = " + date2);

        DayOfWeek weekday = date.getDayOfWeek();
        System.out.println("weekday = " + weekday);

        int value = weekday.getValue();
        System.out.println("value = " + value);

        System.out.println("getMonthValue = " + date.getMonthValue());

        System.out.println("Mon Tue Wed Thu Fri Sat Sun");
        for (int i = 1; i <value; i++){
            System.out.print("   ");
        }
        while (date.getMonthValue() == month){
            System.out.printf("%3d", date.getDayOfMonth());

            if (date.getDayOfMonth() == today){
                System.out.print("*");
            } else {
                System.out.print(" ");
            }
            date = date.plusDays(1);
            if (date.getDayOfWeek().getValue() == 1){
                System.out.println();
            }
        }
        if (date.getDayOfWeek().getValue() != 1){
            System.out.println();
        }
    }
}

运行结果:

month = 4
today = 17
date = 2023-04-01
today - 1 = 16
day = 17
date2 = 2023-04-02
weekday = SATURDAY
value = 6
getMonthValue = 4
Mon Tue Wed Thu Fri Sat Sun
                 1   2 
  3   4   5   6   7   8   9 
 10  11  12  13  14  15  16 
 17* 18  19  20  21  22  23 
 24  25  26  27  28  29  30 

Process finished with exit code 0
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
package com.aapoint.util; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.time.temporal.TemporalAdjusters; public class LocalDateTimeUtil { /** * 比较 localDateTime2 是否在localDateTime1之前(比较大小) * @param localDateTime1 * @param localDateTime2 * @return */ public static Boolean compare(LocalDateTime localDateTime1,LocalDateTime localDateTime2){ return localDateTime1.isBefore(localDateTime2); } /** * 获取当前月份前/后的月份的第一天 * @param i 指定距离当前月份的时间 * @param state 状态 0.当月 1.前 2.后 * @return */ public static String firstDay(Integer state,Integer i){ LocalDateTime date = null; //type 类型 0.月 1.天 2.小时 3.分钟 4.秒 date = getLocalDateTime(state,0,i); //获取该月份的第一天 String firstDay = date.with(TemporalAdjusters.firstDayOfMonth()).format(DateTimeFormatter.ofPattern("yyyy-MM-dd")); // System.out.println("第一天为:"+firstDay); return firstDay; } /** * 获取当前月份前/后的月份的最后一天 * @param i 指定距离当前月份的时间 * @param state 状态 0.当月 1.前 2.后 * @return */ public static String lastDay(Integer state,Integer i){ LocalDateTime date = null; //type 类型 0.月 1.天 2.小时 3.分钟 4.秒 date = getLocalDateTime(state,0,i); //获取该月份的最后一天 String lastDay = date.with(TemporalAdjusters.lastDayOfMonth()).format(DateTimeFormatter.ofPattern("yyyy-MM-dd")); // System.out.println("最后一天为:"+lastDay); return lastDay; } /** * 获取当时间前/后的时间(天) * @param i 指定距离当前月份的时间 * @param state 状态 0.当月 1.前 2.后 * @return */ public static String obtainDay(Integer state,Integer i){ LocalDateTime date = null; //type 类型 0.月 1.天 2.小时 3.分钟 4.秒 date = getLocalDateTime(state,1,i); //获取天 String day = date.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")); // System.out.println("获取的时间为(天):"+day); return day; } /** * 获取当时间前/后的时间(小时) * @param i 指定距离当前月份的时间 * @param state 状态 0.当月 1.前 2.后 * @return */ public static String obtainHours(Integer state,Integer i){ LocalDateTime date = null; //type 类型 0.月 1.天 2.小时 3.分钟 4.秒 date = getLocalDateTime(state,2,i); //获取该月份的最后一天 String hours = date.format(DateTimeFormatter.ofPattern("HH:mm:ss")); // System.out.println("获取的时间为(小时):"+hours); return hours; } /** * 获取当时间前/后的时间(小时) * @param i 指定距离当前月份的时间 * @param state 状态 0.当月 1.前 2.后 * @return */ public static String obtainMinutes(Integer state,Integer i){ LocalDateTime date = null; //type 类型 0.月 1.天 2.小时 3.分钟 4.秒 date = getLocalDateTime(state,3,i); //获取该月份的最后一天 String minutes = date.format(DateTimeFormatter.ofPattern("HH:mm:ss")); // System.out.println("获取的时间为(分钟):"+minutes); return minutes; } /** * 获取当时间前/后的时间(小时) * @param i 指定距离当前月份的时间 * @param state 状态 0.当月 1.前 2.后 * @return */ public static String obtainSeconds(Integer state,Integer i){ LocalDateTime date = null; //type 类型 0.月 1.天 2.小时 3.分钟 4.秒 date = getLocalDateTime(state,4,i); //获取该月份的最后一天 String seconds = date.format(DateTimeFormatter.ofPattern("HH:mm:ss")); // System.out.println("获取的时间为(秒):"+seconds); return seconds; } public static void main(String[] args) { System.out.println("当前时间为:"+LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))); System.out.println("前一个月份的第一天为:"+LocalDateTimeUtil.firstDay(1,1)); System.out.println("前一个月份的最后一天为:"+LocalDateTimeUtil.lastDay(1,1)); System.out.println("当前时间的前一天为:"+LocalDateTimeUtil.obtainDay(1,1)); System.out.println("当前时间的后一天为:"+LocalDateTimeUtil.obtainDay(2,1)); System.out.println("当前时间的前一小时为:"+LocalDateTimeUtil.obtainHours(1,1)); System.out.println("当前时间的后一小时为:"+LocalDateTimeUtil.obtainHours(2,1)); System.out.println("当前时间的前一分钟为:"+LocalDateTimeUtil.obtainMinutes(1,1)); System.out.println("当前时间的后一分钟为:"+LocalDateTimeUtil.obtainMinutes(2,1)); System.out.println("当前时间的前一秒为:"+LocalDateTimeUtil.obtainSeconds(1,1)); System.out.println("当前时间的后一秒为:"+LocalDateTimeUtil.obtainSeconds(2,1)); } private static LocalDateTime getLocalDateTime(Integer state,Integer type,Integer i) { LocalDateTime date; if(state == 0){ date = LocalDateTime.now(); }else if(state == 1){ if(type == 0) { //获取月 date = LocalDateTime.now().minusMonths(i); }else if(type == 1){ //获取天 date = LocalDateTime.now().minusDays(i); }else if(type == 2){ //获取小时 date = LocalDateTime.now().minusHours(i); }else if(type == 3){ //获取分钟 date = LocalDateTime.now().minusMinutes(i);
LocalDate类是Java 8引入的一个日期类,用于表示日期,不包含时间和时区信息。它提供了一系列方法来获取、设置和操作日期。以下是LocalDate类的一些常用方法: 1. `now()`:静态方法,返回当前日期。 2. `of(int year, int month, int dayOfMonth)`:静态方法,根据指定的年、月、日创建一个LocalDate对象。 3. `parse(CharSequence text)`:静态方法,将文本解析为LocalDate对象。 4. `getYear()`:获取年份。 5. `getMonth()`:获取月份,返回一个Month枚举值。 6. `getMonthValue()`:获取月份的值,1表示一月,12表示十二月。 7. `getDayOfMonth()`:获取日期。 8. `getDayOfWeek()`:获取星期几,返回一个DayOfWeek枚举值。 9. `isLeapYear()`:判断当前年份是否是闰年。 10. `isBefore(LocalDate other)`:判断当前日期是否在指定日期之前。 11. `isAfter(LocalDate other)`:判断当前日期是否在指定日期之后。 12. `isEqual(LocalDate other)`:判断当前日期是否与指定日期相等。 13. `plusYears(long years)`:返回当前日期增加指定年数后的日期。 14. `plusMonths(long months)`:返回当前日期增加指定月数后的日期。 15. `plusDays(long days)`:返回当前日期增加指定天数后的日期。 除了上述方法外,LocalDate类还提供了一些其他的方法来操作日期,如计算两个日期之间的天数差、获取本月的最后一天等。需要注意的是,LocalDate类是不可变的,每次对其进行修改操作都会返回一个新的实例。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小葛先生

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值