java根据年月日判断周几

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

java根据年⽉⽇判断周⼏java根据年⽉⽇判断周⼏,下⾯提供四中⽅法需要的可以参考下.
⽅法⼀:
public static String dateToWeek(String datetime) throws java.text.ParseException { SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
String[] weekDays = { "星期⽇", "星期⼀", "星期⼆", "星期三", "星期四", "星期五", "星期六" }; Calendar cal = Calendar.getInstance(); // 获得⼀个⽇历
Date datet = null;
datet = (Date) f.parse(datetime);
cal.setTime(datet);
int w = cal.get(Calendar.DAY_OF_WEEK) - 1; // 指⽰⼀个星期中的某天。

if (w < 0)
w = 0;
System.out.println(weekDays[w]);//星期⼆
return weekDays[w];
}
public static void main(String[] args) throws java.text.ParseException {
System.out.println(Ts.dateToWeek("2019-01-01"));
}
⽅法⼆:
//基姆拉尔森计算公式根据⽇期判断星期⼏
public static void CalculateWeekDay(int y, int m, int d) {
if(m < 1 || m >12){
System.out.println("你输⼊的⽉份不再范围内,请重新输⼊!");
}
if (m == 1 || m == 2) {
m += 12;
y--;
}
int iWeek = (d + 2 * m + 3 * (m + 1) / 5 + y + y / 4 - y / 100 + y / 400) % 7;
switch (iWeek) {
case 0:
System.out.printf("星期⼀\n");
break;
case 1:
System.out.printf("星期⼆\n");
break;
case 2:
System.out.printf("星期三\n");
break;
case 3:
System.out.printf("星期四\n");
break;
case 4:
System.out.printf("星期五\n");
break;
case 5:
System.out.printf("星期六\n");
break;
case 6:
System.out.printf("星期⽇\n");
break;
}
}
public static void main(String args[]) {
CalculateWeekDay(2019, 1, 8);
}
⽅法三:
public static int dayForWeek(String pTime) throws Exception {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Calendar c = Calendar.getInstance();
c.setTime(format.parse(pTime));
int dayForWeek = 0;
if(c.get(Calendar.DAY_OF_WEEK) == 1){
dayForWeek = 7;
}else{
dayForWeek = c.get(Calendar.DAY_OF_WEEK) - 1;
}
System.out.println(dayForWeek);
return dayForWeek;
}
⽅法四:
public static String getWeek(Date date){
String[] weeks = {"星期⽇","星期⼀","星期⼆","星期三","星期四","星期五","星期六"}; Calendar cal = Calendar.getInstance();
cal.setTime(date);
int week_index = cal.get(Calendar.DAY_OF_WEEK) - 1;
if(week_index<0){
week_index = 0;
}
System.out.println(weeks[week_index]);
return weeks[week_index];
}
public static void main(String[] args) throws Exception {
Ts.getWeek(new Date());
}
每天积累⼀点点,总⽤天会有发光的,加油。

相关文档
最新文档