Java:如何获取一个月中的 x 天日期(例如 2012 年 2 月的第三个星期一)

2023-02-09Java开发问题
9

本文介绍了Java:如何获取一个月中的 x 天日期(例如 2012 年 2 月的第三个星期一)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我有点挣扎.

我想将我的日历设置为:2012 年 2 月的第三个星期一.而且我没有找到任何使用 Java 的方法.

I want to setup my Calendar to let's say: Third Monday in February 2012. And I didn't find any way of doing this using Java.

例如,如果我想为 2011 年圣诞节设置日历,我可以很容易地做到这一点,这样:

For example, if I wanted to set my calendar for Christmas 2011, I can do this easily, this way:

Calendar when = Calendar.getInstance();
when.set (Calendar.MONTH, Calendar.DECEMBER);
when.set (Calendar.DAY_OF_MONTH, 25)
when.set (Calendar.YEAR, 2011);

但是我不知道如何为 2012 年阵亡将士纪念日(5 月的最后一个星期一)进行设置.这是我的代码,但显然是错误的,因为我根本无法假设 5 月的最后一个星期一是当年 5 月的第 4 周:

But I am lost as to how to set it up for let's say Memorial Day 2012, which is the last Monday of May. This is my code, but it's obviously wrong, because I simply can't assume that the last Monday of May will be in the 4th week of May that year:

Calendar when = Calendar.getInstance ();
when.set (Calendar.DAY_OF_WEEK,Calendar.MONDAY);
when.set (Calendar.MONTH, Calendar.MAY);
when.set (Calendar.WEEK_OF_MONTH, 4)
when.set (Calendar.YEAR, 2012);

关于如何以编程方式找出 2012 年 5 月的哪一周(如上例)是最后一个星期一的任何建议?假设我能得到这些信息,我应该能够让上面的代码工作.

Any suggestions as to how I can programatically find out, in which week of the month of May 2012 (in example above) is the last Monday? Assuming I can get that information, I should be able to get my code above to work.

我需要一些基本上适用于任何其他示例的东西.可以为相同场景提供确切日期的东西.例子:

I need something which would basically work for any other examples. Something which could give an exact day for the same scenarios. Examples:

日期是:

  • 2015 年 5 月的第三个星期四
  • 2050 年 6 月的第一个星期一
  • 2012 年 12 月的第四个星期二
  • 2000 年 7 月的第二个星期三

我的项目确实需要这个,我确信它很简单,但是我在没有任何实际结果的情况下打破了我的头脑:)而且在网上也找不到任何东西.

I really need this for my project and I am sure it's simple, but I am breaking my head on this without any real results to show for :) And also couldn't find anything on the net.

添加:

好的,这是我一个月内最后一个星期一的工作:

Ok, this is where I've got for the last Monday in a month:

when.set (GregorianCalendar.MONTH, GregorianCalendar.MAY);
when.set (GregorianCalendar.DAY_OF_WEEK, Calendar.MONDAY);
when.set (GregorianCalendar.DAY_OF_WEEK_IN_MONTH, -1);
when.set (Calendar.YEAR, 2012);

但我不确定我将如何在同一个月的星期一做几秒钟,像这样?

But I am not sure how would I go about doing for example seconds Monday in the same month, like this?

when.set (GregorianCalendar.DAY_OF_WEEK_IN_MONTH, 2);

有什么建议吗?

推荐答案

在 Java 中进行日期运算(一般来说,对日期时间做任何事情,除了最琐碎的事情)Joda-Time 就是答案:

To do date arithmetic in Java (and in general, to do anything with datetimes, except for the most trivial things) Joda-Time is the answer:

public static LocalDate getNDayOfMonth(int dayweek,int nthweek,int month,int year)  {
   LocalDate d = new LocalDate(year, month, 1).withDayOfWeek(dayweek);
   if(d.getMonthOfYear() != month) d = d.plusWeeks(1);
   return d.plusWeeks(nthweek-1);
}

public static LocalDate getLastWeekdayOfMonth(int dayweek,int month,int year) {
   LocalDate d = new LocalDate(year, month, 1).plusMonths(1).withDayOfWeek(dayweek);
   if(d.getMonthOfYear() != month) d = d.minusWeeks(1);
  return d;
}

public static void main(String[] args) {
   // second wednesday of oct-2011
   LocalDate d = getNDayOfMonth( DateTimeConstants.WEDNESDAY, 2, 10, 2011);
   System.out.println(d);
   // last wednesday of oct-2011
   LocalDate dlast = getLastWeekdayOfMonth( DateTimeConstants.WEDNESDAY,  10, 2011);
   System.out.println(dlast);
}

<小时>

编辑:从 Java 8 (2014) 开始,新的 Date API(包 java.time)受 Jodatime 的启发/类似,应该是首选.


Edit: Since Java 8 (2014) the new Date API (package java.time), which is inspired by/similar to Jodatime, should be preferred.

这篇关于Java:如何获取一个月中的 x 天日期(例如 2012 年 2 月的第三个星期一)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

如何使用 JAVA 向 COM PORT 发送数据?
How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)...
2024-08-25 Java开发问题
21

如何使报表页面方向更改为“rtl"?
How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)...
2024-08-25 Java开发问题
19

在 Eclipse 项目中使用西里尔文 .properties 文件
Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)...
2024-08-25 Java开发问题
18

有没有办法在 Java 中检测 RTL 语言?
Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)...
2024-08-25 Java开发问题
11

如何在 Java 中从 DB 加载资源包消息?
How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)...
2024-08-25 Java开发问题
13

如何更改 Java 中的默认语言环境设置以使其保持一致?
How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)...
2024-08-25 Java开发问题
13