Java LocalDate类| 带示例的getDayOfWeek()方法

LocalDate类的getDayOfWeek()方法 (LocalDate Class getDayOfWeek() method)

  • getDayOfWeek() method is available in java.time package.

    getDayOfWeek()方法在java.time包中可用。

  • getDayOfWeek() method is used to get the value for the field day-of-week that is an enum DayOfWeek of this LocalDate.

    getDayOfWeek()方法用于获取星期几字段的值,该值是此LocalDate的枚举DayOfWeek。

  • getDayOfWeek() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.

    getDayOfWeek()方法是一个非静态方法,只能通过类对象访问,如果尝试使用类名称访问该方法,则会收到错误消息。

  • getDayOfWeek() method does not throw an exception at the time of getting the day of the week.

    getDayOfWeek()方法在获取星期几时不会引发异常。

Syntax:

句法:

    public DayOfWeek getDayOfWeek();

Parameter(s):

参数:

  • None

    没有

Return value:

返回值:

The return type of this method is DayOfWeek, it returns the field value day-of-week.

此方法的返回类型为DayOfWeek ,它返回星期几的字段值。

Example:

例:

// Java program to demonstrate the example 
// of getDayOfWeek() method 
// of LocalDate

import java.time.*;

public class GetDayOfWeekOfLocalDate {
    public static void main(String args[]) {
        // Instantiates two LocalDate
        LocalDate l_da1 = LocalDate.parse("2007-04-05");
        LocalDate l_da2 = LocalDate.now();

        // Display l_da1,l_da2
        System.out.println("LocalDate l_da1 and l_da2: ");
        System.out.println("l_da1: " + l_da1);
        System.out.println("l_da2: " + l_da2);

        System.out.println();

        // Here, this method gets the value for
        // the field day-of-week in this
        // date object l_da1 that is based
        // on an enum DayOfWeek
        DayOfWeek dow = l_da1.getDayOfWeek();

        // Display dow
        System.out.println("l_da1.getDayOfWeek(): " + dow);

        // Here, this method gets the value for
        // the field day-of-week in this
        // date object l_da2 that is based
        // on an enum DayOfWeek
        dow = l_da2.getDayOfWeek();

        // Display dow
        System.out.println("l_da2.getDayOfWeek(): " + dow);
    }
}

Output

输出量

LocalDate l_da1 and l_da2: 
l_da1: 2007-04-05
l_da2: 2020-05-29

l_da1.getDayOfWeek(): THURSDAY
l_da2.getDayOfWeek(): FRIDAY


翻译自: https://www.includehelp.com/java/localdate-getdayofweek-method-with-example.aspx

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是Java实现一个节假日工具示例代码: ```java import java.time.LocalDate; import java.util.HashMap; import java.util.Map; public class HolidayUtils { private static Map<LocalDate, String> holidayMap = new HashMap<>(); static { holidayMap.put(LocalDate.of(2022, 1, 1), "元旦"); holidayMap.put(LocalDate.of(2022, 2, 1), "春节"); holidayMap.put(LocalDate.of(2022, 4, 4), "清明节"); holidayMap.put(LocalDate.of(2022, 5, 1), "劳动节"); holidayMap.put(LocalDate.of(2022, 6, 1), "儿童节"); holidayMap.put(LocalDate.of(2022, 9, 10), "教师节"); holidayMap.put(LocalDate.of(2022, 10, 1), "国庆节"); holidayMap.put(LocalDate.of(2022, 12, 25), "圣诞节"); } /** * 判断指定日期是否是节假日 * * @param date 指定日期 * @return 如果是节假日,返回节假日名称,否则返回null */ public static String isHoliday(LocalDate date) { return holidayMap.get(date); } /** * 判断指定日期是否是工作日(即非周六周日和节假日) * * @param date 指定日期 * @return 如果是工作日,返回true,否则返回false */ public static boolean isWorkday(LocalDate date) { int weekDay = date.getDayOfWeek().getValue(); if (weekDay == 6 || weekDay == 7) { return false; } String holidayName = isHoliday(date); if (holidayName != null) { return false; } return true; } } ``` 在上面的代码中,我们定义了一个HolidayUtils,其中包含了两个方法:isHoliday()和isWorkday()。 isHoliday()方法用于判断指定日期是否是节假日,如果是节假日,返回节假日名称,否则返回null。 isWorkday()方法用于判断指定日期是否是工作日,即非周六周日和节假日。如果是工作日,返回true,否则返回false。 在的静态代码块中,我们初始化了一个节假日和对应节假日名称的Map,可以根据自己的需求进行修改。 使用该工具时,只需要引入该,然后调用isHoliday()和isWorkday()方法即可实现节假日和工作日的判断。例如: ```java LocalDate date = LocalDate.of(2022, 10, 2); if (HolidayUtils.isHoliday(date) != null) { System.out.println(date + "是节假日:" + HolidayUtils.isHoliday(date)); } else if (HolidayUtils.isWorkday(date)) { System.out.println(date + "是工作日。"); } else { System.out.println(date + "是周末。"); } ``` 输出结果为: ``` 2022-10-02是周末。 ``` 这样就完成了一个简单的节假日工具的实现。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值