久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

Java:如何獲取一個(gè)月中的 x 天日期(例如 2012 年

Java: How do I get the date of x day in a month ( e.g. Third Monday in February 2012)(Java:如何獲取一個(gè)月中的 x 天日期(例如 2012 年 2 月的第三個(gè)星期一))
本文介紹了Java:如何獲取一個(gè)月中的 x 天日期(例如 2012 年 2 月的第三個(gè)星期一)的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(wèn)題描述

限時(shí)送ChatGPT賬號(hào)..

我有點(diǎn)掙扎.

我想將我的日歷設(shè)置為:2012 年 2 月的第三個(gè)星期一.而且我沒(méi)有找到任何使用 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 年圣誕節(jié)設(shè)置日歷,我可以很容易地做到這一點(diǎn),這樣:

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 年陣亡將士紀(jì)念日(5 月的最后一個(gè)星期一)進(jìn)行設(shè)置.這是我的代碼,但顯然是錯(cuò)誤的,因?yàn)槲腋緹o(wú)法假設(shè) 5 月的最后一個(gè)星期一是當(dāng)年 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);

關(guān)于如何以編程方式找出 2012 年 5 月的哪一周(如上例)是最后一個(gè)星期一的任何建議?假設(shè)我能得到這些信息,我應(yīng)該能夠讓上面的代碼工作.

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.

我需要一些基本上適用于任何其他示例的東西.可以為相同場(chǎng)景提供確切日期的東西.例子:

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 月的第三個(gè)星期四
  • 2050 年 6 月的第一個(gè)星期一
  • 2012 年 12 月的第四個(gè)星期二
  • 2000 年 7 月的第二個(gè)星期三

我的項(xiàng)目確實(shí)需要這個(gè),我確信它很簡(jiǎn)單,但是我在沒(méi)有任何實(shí)際結(jié)果的情況下打破了我的頭腦:)而且在網(wǎng)上也找不到任何東西.

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.

添加:

好的,這是我一個(gè)月內(nèi)最后一個(gè)星期一的工作:

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);

但我不確定我將如何在同一個(gè)月的星期一做幾秒鐘,像這樣?

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 中進(jìn)行日期運(yùn)算(一般來(lái)說(shuō),對(duì)日期時(shí)間做任何事情,除了最瑣碎的事情)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);
}

<小時(shí)>

編輯:從 Java 8 (2014) 開始,新的 Date API(包 java.time)受 Jodatime 的啟發(fā)/類似,應(yīng)該是首選.


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

這篇關(guān)于Java:如何獲取一個(gè)月中的 x 天日期(例如 2012 年 2 月的第三個(gè)星期一)的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Parsing an ISO 8601 string local date-time as if in UTC(解析 ISO 8601 字符串本地日期時(shí)間,就像在 UTC 中一樣)
How to convert Gregorian string to Gregorian Calendar?(如何將公歷字符串轉(zhuǎn)換為公歷?)
Java: What/where are the maximum and minimum values of a GregorianCalendar?(Java:GregorianCalendar 的最大值和最小值是什么/在哪里?)
Calendar to Date conversion for dates before 15 Oct 1582. Gregorian to Julian calendar switch(1582 年 10 月 15 日之前日期的日歷到日期轉(zhuǎn)換.公歷到儒略歷切換)
java Calendar setFirstDayOfWeek not working(java日歷setFirstDayOfWeek不起作用)
Java: getting current Day of the Week value(Java:獲取當(dāng)前星期幾的值)
主站蜘蛛池模板: 在线观看黄色电影 | 国产精品久久精品 | 欧美日一区二区 | 久久一区精品 | 青青草一区二区 | 在线视频中文字幕 | 99久久99久久精品国产片果冰 | 四虎海外| 欧州一区 | 欧美一区二区三区视频 | 亚洲一区二区视频 | 亚洲精品无 | 神马福利 | 超碰成人免费 | 在线日韩欧美 | 日本一区二区视频 | 欧美日韩综合一区 | 在线免费看毛片 | 精品欧美乱码久久久久久1区2区 | 欧美一区两区 | 成人久久一区 | 嫩呦国产一区二区三区av | 久久亚洲综合 | www.亚洲一区二区三区 | 亚洲天堂999| 极情综合网 | 亚洲iv一区二区三区 | 亚洲婷婷六月天 | 天天干天天玩天天操 | 国产乱码精品一区二区三区五月婷 | 精品免费在线 | 久久久久久久电影 | 国产中文字幕在线 | 亚洲欧洲精品成人久久奇米网 | 精品久久久久久国产 | 亚洲第一成人影院 | 99re66在线观看精品热 | 国产福利在线 | 国产精品久久久久影院色老大 | 午夜免费在线电影 | 日本不卡视频在线播放 |