本文介紹了有什么好方法可以得到即將到來的星期三的日期嗎?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
限時送ChatGPT賬號..
有沒有什么好的方法可以獲取下周三的日期?即如果今天是星期二,我想得到這周星期三的日期;如果今天是星期三,我想得到下星期三的日期;如果今天是星期四,我想得到下周星期三的日期.
Is there a good way to get the date of the coming Wednesday? That is, if today is Tuesday, I want to get the date of Wednesday in this week; if today is Wednesday, I want to get the date of next Wednesday; if today is Thursday, I want to get the date of Wednesday in the following week.
謝謝.
推薦答案
基本算法如下:
- 獲取當前日期
- 獲取星期幾
- 找出與星期三的不同之處
- 如果差值不是正數,則加 7(即堅持下一個/未來日期)
- 添加差異
這里有一個片段展示如何使用 java.util.Calendar
來做到這一點:
Here's a snippet to show how to do this with java.util.Calendar
:
import java.util.Calendar;
public class NextWednesday {
public static Calendar nextDayOfWeek(int dow) {
Calendar date = Calendar.getInstance();
int diff = dow - date.get(Calendar.DAY_OF_WEEK);
if (diff <= 0) {
diff += 7;
}
date.add(Calendar.DAY_OF_MONTH, diff);
return date;
}
public static void main(String[] args) {
System.out.printf(
"%ta, %<tb %<te, %<tY",
nextDayOfWeek(Calendar.WEDNESDAY)
);
}
}
相對于我的此時此地,上述代碼段的輸出是 "Wed, Aug 18, 2010"
.
Relative to my here and now, the output of the above snippet is "Wed, Aug 18, 2010"
.
java.util.Calendar代碼>
java.util.Formatter
- 用于格式化字符串語法
這篇關于有什么好方法可以得到即將到來的星期三的日期嗎?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!