How to determine day of week by passing specific date in Java? – 如何在Java中通过传递特定日期来确定星期几?

最后修改: 2020年 3月 31日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

In this short tutorial, we’ll see how to extract the day of the week as a number and as text from a Java date.

在这个简短的教程中,我们将看到如何从一个Java日期中以数字和文本形式提取星期几。

2. Problem

2 问题

Business logic often needs the day of the week. Why? For one, working hours and service levels differ between workdays and weekends. Therefore, getting the day as a number is necessary for a lot of systems. But we also may need the day as a text for display.

业务逻辑经常需要星期几。为什么呢?其一,工作日和周末的工作时间和服务水平不同。因此,对于很多系统来说,以数字形式获取日期是必要的。但我们也可能需要把日子作为文本来显示。

So, how do we extract the day of the week from dates in Java?

那么,我们如何从Java中的日期中提取星期几呢?

3. Solution With java.util.Date

3.使用java.util.Date的解决方案

java.util.Date has been the Java date class since Java 1.0. Code that started with Java version 7 or lower probably uses this class.

java.util.Date自Java 1.0以来一直是Java日期类。从Java 7或更低版本开始的代码可能使用这个类。

3.1. Day of Week as a Number

3.1.作为数字的星期

First, we extract the day as a number using java.util.Calendar:

首先,我们使用java.util.Calendar将日期提取为一个数字

public static int getDayNumberOld(Date date) {
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    return cal.get(Calendar.DAY_OF_WEEK);
}

The resulting number ranges from 1 (Sunday) to 7 (Saturday). Calendar defines constants for this: Calendar.SUNDAY – Calendar.SATURDAY.

产生的数字范围从1(星期日)到7(星期六)Calendar为此定义了一些常数。Calendar.SUNDAYCalendar.SATURDAY

3.2. Day of Week as Text

3.2.作为文本的星期

Now we extract the day as text. We pass in a Locale to determine the language:

现在,我们将这一天提取为文本。我们传入一个Locale来确定语言。

public static String getDayStringOld(Date date, Locale locale) {
    DateFormat formatter = new SimpleDateFormat("EEEE", locale);
    return formatter.format(date);
}

This returns the full day in your language, such as “Monday” in English or “Montag” in German.

返回您的语言的全天,例如英语的 “Monday “或德语的 “Montag”。

4. Solution With java.time.LocalDate

4.使用java.time.LocalDate的解决方案

Java 8 overhauled date and time handling and introduced java.time.LocalDate for dates. Therefore, Java projects that only run on Java versions 8 or higher should use this class!

Java 8全面修改了日期和时间处理,并为日期引入了java.time.LocalDate。因此,只在Java 8或更高版本上运行的Java项目应该使用这个类!

4.1. Day of Week as a Number

4.1.作为数字的星期

Extracting the day as a number is trivial now:

把日子作为一个数字来提取是微不足道的了。

public static int getDayNumberNew(LocalDate date) {
    DayOfWeek day = date.getDayOfWeek();
    return day.getValue();
}

The resulting number still ranges from 1 to 7. But this time, Monday is 1 and Sunday is 7! The day of the week has its own enum — DayOfWeek. As expected, the enum values are MONDAY – SUNDAY.

但这次,周一是1,周日是7一周的日子有自己的enumDayOfWeek一周的日子有它自己的enumDayOfWeek。正如预期的那样,enum值是MONDAYSUNDAY

4.2. Day of Week as Text

4.2.作为文本的星期

Now we extract the day as text again. We also pass in a Locale:

现在,我们再次将日期提取为文本。我们还传入一个Locale

public static String getDayStringNew(LocalDate date, Locale locale) {
    DayOfWeek day = date.getDayOfWeek();
    return day.getDisplayName(TextStyle.FULL, locale);
}

Just as with java.util.Date, this returns the full day in the chosen language.

java.util.Date一样,这将返回所选语言中的整日。

5. Conclusion

5.总结

In this article, we extracted the day of the week from Java dates. We saw how to return both a number and a text using java.util.Date and java.time.LocalDate.

在这篇文章中,我们从Java日期中提取了星期几。我们看到了如何使用java.util.Datejava.time.LocalDate来返回一个数字和一个文本。

As always, the code is available over on GitHub.

像往常一样,代码可在GitHub上获得