java输入一个日期返回星期几
不要网上搜来的答案。。我还是不知道该用什么方法呀-------------------------------------------------------------...
不要网上搜来的答案。。我还是不知道该用什么方法呀
------------------------------------------------------------------------------------
public class Weekday {
public int year;
public int month;
public int day;
public Weekday(int year,int month,int day){
this.year = year;
this.month = month;
this.day = day;
}
public Weekday(){}
public void showWeekday()throws Throwable {
Calendar temp = null;
temp.set(this.year, this.month, this.day);
int x = temp.get(Calendar.DAY_OF_WEEK)-1;
switch(x){
case 0:
System.out.println("星期日");
break;
case 1:
System.out.println("星期一");
break;
case 2:
System.out.println("星期二");
break;
case 3:
System.out.println("星期三");
break;
case 4:
System.out.println("星期四");
break;
case 5:
System.out.println("星期五");
break;
case 6:
System.out.println("星期六");
break;
default:
System.out.println("出错啦");
}
}
}
-------------------------------------------------------------------------------
public class TestWeekday {
public static void main(String args[]) throws Throwable{
Scanner sc = new Scanner(System.in);
Weekday wkd = new Weekday();
wkd.year = sc.nextInt();
wkd.month = sc.nextInt();
wkd.day = sc.nextInt();
wkd.showWeekday();
}
}
------------------------------------------------------------------------------
求大神教导 展开
------------------------------------------------------------------------------------
public class Weekday {
public int year;
public int month;
public int day;
public Weekday(int year,int month,int day){
this.year = year;
this.month = month;
this.day = day;
}
public Weekday(){}
public void showWeekday()throws Throwable {
Calendar temp = null;
temp.set(this.year, this.month, this.day);
int x = temp.get(Calendar.DAY_OF_WEEK)-1;
switch(x){
case 0:
System.out.println("星期日");
break;
case 1:
System.out.println("星期一");
break;
case 2:
System.out.println("星期二");
break;
case 3:
System.out.println("星期三");
break;
case 4:
System.out.println("星期四");
break;
case 5:
System.out.println("星期五");
break;
case 6:
System.out.println("星期六");
break;
default:
System.out.println("出错啦");
}
}
}
-------------------------------------------------------------------------------
public class TestWeekday {
public static void main(String args[]) throws Throwable{
Scanner sc = new Scanner(System.in);
Weekday wkd = new Weekday();
wkd.year = sc.nextInt();
wkd.month = sc.nextInt();
wkd.day = sc.nextInt();
wkd.showWeekday();
}
}
------------------------------------------------------------------------------
求大神教导 展开
2个回答
展开全部
public void showWeekday() throws Throwable {
Calendar temp = Calendar.getInstance();// 需要初始化
temp.set(this.year, this.month - 1, this.day);// 月份从0开始
int x = temp.get(Calendar.DAY_OF_WEEK);
switch (x) {
case Calendar.SUNDAY:
System.out.println("星期日");
break;
case Calendar.MONDAY:
System.out.println("星期一");
break;
case Calendar.TUESDAY:
System.out.println("星期二");
break;
case Calendar.WEDNESDAY:
System.out.println("星期三");
break;
case Calendar.THURSDAY:
System.out.println("星期四");
break;
case Calendar.FRIDAY:
System.out.println("星期五");
break;
case Calendar.SATURDAY:
System.out.println("星期六");
break;
default:
System.out.println("出错啦");
}
}
展开全部
import java.util.Calendar;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// 测试用的年月
int year = 2014;
int month = 5;
int day = 17;
// 取得月末的日期,即这个月有多少天
Calendar temp = Calendar.getInstance();
temp.set(year, month-1,day);
int x = temp.get(Calendar.DAY_OF_WEEK);
System.out.println(getWeek(x));
}
// 转换成星期几
private static String getWeek(int key) {
switch (key) {
case 1:
return "星期日";
case 2:
return "星期一";
case 3:
return "星期二";
case 4:
return "星期三";
case 5:
return "星期四";
case 6:
return "星期五";
case 7:
return "星期六";
}
return "";
}
}
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// 测试用的年月
int year = 2014;
int month = 5;
int day = 17;
// 取得月末的日期,即这个月有多少天
Calendar temp = Calendar.getInstance();
temp.set(year, month-1,day);
int x = temp.get(Calendar.DAY_OF_WEEK);
System.out.println(getWeek(x));
}
// 转换成星期几
private static String getWeek(int key) {
switch (key) {
case 1:
return "星期日";
case 2:
return "星期一";
case 3:
return "星期二";
case 4:
return "星期三";
case 5:
return "星期四";
case 6:
return "星期五";
case 7:
return "星期六";
}
return "";
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询