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

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

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 月的第三個星期一)的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

我有點掙扎.

我想將我的日歷設置為: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模板網!

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

相關文檔推薦

Parsing an ISO 8601 string local date-time as if in UTC(解析 ISO 8601 字符串本地日期時間,就像在 UTC 中一樣)
How to convert Gregorian string to Gregorian Calendar?(如何將公歷字符串轉換為公歷?)
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 日之前日期的日歷到日期轉換.公歷到儒略歷切換)
java Calendar setFirstDayOfWeek not working(java日歷setFirstDayOfWeek不起作用)
Java: getting current Day of the Week value(Java:獲取當前星期幾的值)
主站蜘蛛池模板: 求毛片 | 国产福利91精品 | 成人国产免费视频 | 免费一级毛片 | 成人精品久久 | 国产一区二区影院 | 亚洲欧美日韩在线不卡 | 午夜小电影 | 色www精品视频在线观看 | 色综合av| 国产精品久久久久久婷婷天堂 | 日韩三级 | 久久久久久91香蕉国产 | 国产区免费视频 | 女人牲交视频一级毛片 | 国产伦精品一区二区三区视频金莲 | 日韩欧美一区二区三区在线播放 | 国产精品久久av | 日韩欧美视频 | 亚洲va中文字幕 | 亚洲一区二区三区桃乃木香奈 | 欧美日韩国产高清 | 亚洲成网 | 一区二区三区国产精品 | 国产欧美性成人精品午夜 | 亚洲在线免费观看 | 国产欧美日韩一区 | 美女在线一区二区 | 国产成人自拍一区 | 毛片免费看 | 国产极品车模吞精高潮呻吟 | 国产精品免费观看视频 | 一本色道久久综合亚洲精品高清 | 久久国产婷婷国产香蕉 | 午夜精品久久 | 国产成人精品a视频 | 懂色一区二区三区免费观看 | 色综合99 | 亚洲一区二区在线 | 蜜桃精品在线 | 成人av一区 |