問題描述
我有點掙扎.
我想將我的日歷設置為: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 月的第三個星期一)的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!