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

Java: How do I get the date of x day in a month ( e.g. Third Monday in February 2012)(Java:如何获取一个月中的 x 天日期(例如 2012 年 2 月的第三个星期一))
本文介绍了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 月的第三个星期一)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Month is not printed from a date - Java DateFormat(月份不是从日期打印的 - Java DateFormat)
java get week of year for given a date(java获取给定日期的一年中的一周)
Get the number of days, weeks, and months, since Epoch in Java(获取自 Java 中的 Epoch 以来的天数、周数和月数)
Is there a good way to get the date of the coming Wednesday?(有什么好方法可以得到即将到来的星期三的日期吗?)
Calendar view for Android GingerBread and before (APIlt;11)(Android GingerBread 及之前的日历视图 (API11))
How to save and retrieve Date in SharedPreferences(如何在 SharedPreferences 中保存和检索日期)