java.time获取所选月份的第一天

时间:2017-02-12 16:31:35

标签: java

我想在选定的月份获得第一天的一周。主要条件是仅使用java.time。我需要这样的smth:

DayOfWeek dayOfWeek = LocalDate.now().with(TemporalAdjusters.firstDayOfMonth()).getDayOfWeek();

我已经拥有了YearMonth,这是我输入年份和月份的数字。我怎样才能获得每月第一天的dayOfWeek?我是这样做的:

DayOfWeek dayOfWeek = yearMonth.atDay(1).getDayOfWeek();

但是我得到了一个评论,我有"魔术数字"我需要通过正确使用库java.time消除。

3 个答案:

答案 0 :(得分:1)

幻数

  

我得到了一个评论,我有"魔术数字"我需要通过正确使用库java.time来消除。

不,你没有使用magic number;评论不正确。在看到硬编码的文字数作为参数传递时,该评论的作者跳得太快了。实际上,将一个字面数作为一个参数传递是可疑的“神奇数字”,并且值得再次看,但在这种情况下是非常合适的。

术语magic number指的是数字的使用,其目的/意义/作用不是立即显而易见的。将1传递给YearMonth.atDay()非常明显,这意味着本月的第一天。

我个人希望YearMonth班级提供atFirstOfMonth,就像atEndOfMonth一样。我的动机是避免这个问题:敏感地发现作为参数传递的硬编码文字数字。但没什么大不了的。 您的代码对atDay( 1 )的来电是正确而明确的。使用TemporalAdjuster也是正确的,但不是那么明显。

单行,如果您喜欢短代码(我没有):

YearMonth.of( year , month ).atDay( 1 ).getDayOfWeek()

接下来是一些讨论来阐明这个话题。

YearMonth

  

我已经拥有了YearMonth,这是我输入年份和月份的数字。

Java为此提供了一个类,代表整个月:YearMonth

YearMonth ym = YearMonth.now() ;  // Capture the current year-month as seen through the wall-clock time used by the people of the JVM’s current default time zone.

我建议在你的代码库周围传递这个类的对象,而不是仅仅使用年份和时间的整数。月。使用对象可提供类型安全性,确保有效值,并使您的代码更具自我文档。

时区

最好指定时区。对于任何给定的时刻,日期在全球范围内因地区而异。最好明确指定期望/预期的时区,而不是隐式依赖JVM的当前默认值,该默认值可以在运行时期间随时更改。

continent/region的格式指定proper time zone name,例如America/MontrealAfrica/CasablancaPacific/Auckland。切勿使用诸如ESTIST之类的3-4字母缩写,因为它们不是真正的时区,不是标准化的,甚至不是唯一的(!)。

ZoneId z = ZoneId.of( "America/Montreal" ) ;  // Specify desired/expected time zone. Or explicitly ask for JVM’s current default: `ZoneId.systemDefault()`.
YearMonth ym = YearMonth.now( z ) ;  // Capture the current year-month as seen through the wall-clock time used by the people of a particular region (time zone).

LocalDate

LocalDate为单位获取每月第一天的日期。

LocalDate ld = ym.atDay( 1 ) ;  // Get the first day of the month.

DayOfWeek

DayOfWeek枚举提供了七个预先存在的对象,每周一天。这些不是仅仅是字符串,而是智能对象。

DayOfWeek dow = ld.getDayOfWeek() ;
String output = dow.getDisplayName( TextStyle.FULL , Locale.CANADA_FRENCH ) ;  // Generate a String representing the name of this day-of-week localized to the human language and cultural norms of a particular `Locale`. 

答案 1 :(得分:0)

如果你有月份和年份而你只需要一个月的第一天,我会做这样的事情:

DayOfWeek firstDay = LocalDate.of(year, month, 1).getDayOfWeek();

基本上,您构建了dayOfMonth param为1的日期。

根据您在一个月的第一天的解决方案,假设yearMonthLocalDate这应该有效:

DayOfWeek dayOfWeek =yearMonth.with(TemporalAdjusters.firstDayOfMonth()).getDayOfWeek();
System.out.println(dayOfWeek);

答案 2 :(得分:0)

这是一个只传递LocalDate对象的解决方案:

public static String getFirstWeekDay(LocalDate date) {

        int day = 1;
        int month = date.getMonthValue();
        int year = date.getYear();

        LocalDate newDate = LocalDate.of(year, month, day);
        String dayOfWeek = newDate.getDayOfWeek().toString();

        return dayOfWeek;
}

或者,您可以使用以下方法获取DayOfWeek Enum:

public static DayOfWeek getFirstWeekDay(LocalDate date){

        int day = 1;
        int month = date.getMonthValue();
        int year = date.getYear();

        LocalDate newDate = LocalDate.of(year, month, day);

        return newDate.getDayOfWeek();
}

我希望这会有所帮助。

相关问题