導航:首頁 > 編程語言 > java日期獲取星期

java日期獲取星期

發布時間:2022-10-01 10:24:21

java如何得到上周一和上周日的日期

/**
* 獲取上周五時間
*/
public Date lastFirday() {
//作用防止周日得到本周日期
Calendar calendar = Calendar.getInstance();
while (calendar.get(Calendar.DAY_OF_WEEK) != Calendar.MONDAY) {
calendar.add(Calendar.DAY_OF_WEEK, -1);
}
int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK) - 1;
int offset = 7 - dayOfWeek;
calendar.add(Calendar.DATE, offset - 9);
return DateTimeUtil.getFirstDayOfWeek(calendar.getTime(), 6);//這是從上周日開始數的到本周五為6
}

/**
* 獲取上周一時間
*/
public Date lastMonday() {
Calendar calendar = Calendar.getInstance();
while (calendar.get(Calendar.DAY_OF_WEEK) != Calendar.MONDAY) {
calendar.add(Calendar.DAY_OF_WEEK, -1);
}
int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK) - 1;
int offset = 1 - dayOfWeek;
calendar.add(Calendar.DATE, offset - 7);
return DateTimeUtil.getFirstDayOfWeek(calendar.getTime(), 2);
}

② java怎麼通過日期獲取星期幾

1、使用Calendar類
/**
* 獲取當前日期是星期幾<br>
*
* @param dt
* @return 當前日期是星期幾
*/
public static String getWeekOfDate(Date dt) {
String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
Calendar cal = Calendar.getInstance();
cal.setTime(dt);
int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
if (w < 0)
w = 0;
return weekDays[w];
}
2、使用SimpleDateFormat格式化日期

Date date=new Date();
SimpleDateFormat dateFm = new SimpleDateFormat("EEEE");
dateFm.format(date);
註:格式化字元串存在區分大小寫
對於創建SimpleDateFormat傳入的參數:EEEE代表星期,如「星期四」;MMMM代表中文月份,如「十一月」;MM代表月份,如「11」;
yyyy代表年份,如「2010」;dd代表天,如「25」

③ java如何實現獲取一年中所有周的星期一和星期天。(要求1月4號所在的周為新年第一周)

代碼截圖如下:從2021-1-1到2021-12-31日

運行結果

④ java獲取當前日期前三位,即星期幾

Calendar calendar = Calendar.getInstance();
SimpleDateFormat df = new SimpleDateFormat("yyyy-M-d");
String s = df.format(calendar.getTime());
System.out.println(s);
System.out.println(calendar.get(Calendar.DAY_OF_WEEK)-1);
因為星期是從周日開始的

⑤ java知道當前時間,怎樣知道星期幾

java中獲取星期幾可以這樣:
把時間格式化為字元串

public class Time {

public static void main(String[] args) {
Date date = new Date();
SimpleDateFormat strdate = new SimpleDateFormat("E");
String str = strdate.format(date);
System.out.println(str);
}
}

其中日期標志符為:
G年代
y 年
M 月
d 日
h 時在上午或下午 (1~12)
H 時在一天中 (0~23)
m 分
s 秒
S 毫秒
E 星期
D 一年中的第幾天
F 一月中第幾個星期幾
w 一年中第幾個星期
W 一月中第幾個星期
a 上午 / 下午標記符
k 時在一天中 (1~24)
K 時在上午或下午 (0~11)
z 時區

⑥ java如何獲取每個月的所有周六周日日期

publicstaticvoidmain(String[]args)throwsParseException{
intyear=2017;
Calendarcalendar=newGregorianCalendar(year,0,1);
inti=1;
while(calendar.get(Calendar.YEAR)<year+1){
calendar.set(Calendar.WEEK_OF_YEAR,i++);
calendar.set(Calendar.DAY_OF_WEEK,Calendar.SUNDAY);
if(calendar.get(Calendar.YEAR)==year){
System.out.printf("星期天:%tF%n",calendar);
}
calendar.set(Calendar.DAY_OF_WEEK,Calendar.SATURDAY);
if(calendar.get(Calendar.YEAR)==year){
System.out.printf("星期六:%tF%n",calendar);
}
}
}

⑦ java 一年的第一周從2月1號開始,獲取今年的第三周的星期一,以及去年第三周的星期一 對應的日期

public getWeekCount(Date date){ // 獲取日歷類實例 java.util.Calendar calendar = java.util.Calendar.getInstance(); // 設置時間 calendar.setTime(date); // 設置周一為每周第一天 calendar.setFirstDayOfWeek(java.util.Calendar.MONDAY); // 返回當年最大周數 return calendar.getActualMaximum(java.util.Calendar.WEEK_OF_YEAR);}

⑧ Java編程,輸入日期輸出星期。

你好,按照你的要求,代碼如下

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

public class test {
public static void main(String[] args) {

String[] day = new String[] { "日", "一", "二", "三", "四", "五", "六" };

DateFormat df = new SimpleDateFormat("yyyyMMdd");// 輸入的日期格式必須是這種
Scanner s = new Scanner(System.in);
while (true) {
System.out.println("輸入一個日期,格式是20120101");
try {
Date d = df.parse(s.nextLine());// 把字元串轉化成日期
System.out.println("星期" + day[d.getDay()]);
} catch (ParseException e) {
System.out.println("輸入不合法");
}
}
}
}

運行結果:

輸入一個日期,格式是20120101
20111123
星期三
輸入一個日期,格式是20120101
2011
輸入不合法
輸入一個日期,格式是20120101

閱讀全文

與java日期獲取星期相關的資料

熱點內容
男主中媚毒要不斷的要 瀏覽:342
大咖電影網宅男 瀏覽:436
麗卡所演的電影 瀏覽:303
android百度地圖導航開發 瀏覽:635
狂神之紫嫣之辱 瀏覽:199
程序員上班久了額頭疼 瀏覽:439
朋友聊天記錄誤刪怎麼恢復安卓 瀏覽:333
keil中取反如何編程 瀏覽:641
酷客影院打不開 瀏覽:530
韓國倫理劇中文字幕 瀏覽:753
免費流行影視 瀏覽:544
兩個雲伺服器資料能否互傳 瀏覽:791
不卡影院8區 瀏覽:628
冷血末世 小說 瀏覽:747
江寒為主角的小說 瀏覽:259
6080新視覺觀影理論 瀏覽:186
重生之紅色子弟 瀏覽:371
雲伺服器可以做代掛嗎 瀏覽:110
股票梅花樁主圖k線指標源碼 瀏覽:219