本文介紹了有什么好方法可以得到即將到來(lái)的星期三的日期嗎?的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!
問(wèn)題描述
限時(shí)送ChatGPT賬號(hào)..
有沒(méi)有什么好的方法可以獲取下周三的日期?即如果今天是星期二,我想得到這周星期三的日期;如果今天是星期三,我想得到下星期三的日期;如果今天是星期四,我想得到下周星期三的日期.
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.
謝謝.
推薦答案
基本算法如下:
- 獲取當(dāng)前日期
- 獲取星期幾
- 找出與星期三的不同之處
- 如果差值不是正數(shù),則加 7(即堅(jiān)持下一個(gè)/未來(lái)日期)
- 添加差異
這里有一個(gè)片段展示如何使用 java.util.Calendar
來(lái)做到這一點(diǎn):
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)
);
}
}
相對(duì)于我的此時(shí)此地,上述代碼段的輸出是 "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
- 用于格式化字符串語(yǔ)法
這篇關(guān)于有什么好方法可以得到即將到來(lái)的星期三的日期嗎?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!