想要将星期几作为字符串,但是给错一天 [英] Want to get day of a week as a string, But giving wrong day
本文介绍了想要将星期几作为字符串,但是给错一天的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试通过使用以下代码将日期作为字符串。但是它返回错误的字符串。
I tried to get the day as a string by using the following code. But it returns wrong string. Can I fix it with this code.
private String getDayOfWeek(int value){
String day = "";
switch(value){
case 1:
day="Sunday";
break;
case 2:
day="Monday";
break;
case 3:
day="Tuesday";
break;
case 4:
day="Wednesday";
break;
case 5:
day="Thursday";
break;
case 6:
day="Friday";
break;
case 7:
day="Saturday";
break;
}
return day;
我将其实现为
Calendar c = Calendar.getInstance();
String dayOfWeek = getDayOfWeek(Calendar.DAY_OF_WEEK);
System.out.println(dayOfWeek);
推荐答案
您需要使用
String dayOfWeek = getDayOfWeek(c.get(Calendar.DAY_OF_WEEK));
您之前在做什么
String dayOfWeek = getDayOfWeek(Calendar.DAY_OF_WEEK);
正在使用随机常量(恰好是7 )日历
类用于表示日期中的 DAY_OF_WEEK
字段。
is calling your method with a random constant (that happens to be 7) the Calendar
class is using to represent the DAY_OF_WEEK
field in a date.
您真正要查找的是在您的 Calendar
实例中获取星期几的值,这就是 Calendar#get(int)
What you are actually looking for is getting the value of the day of the week in your Calendar
instance, which is what Calendar#get(int)
c.get(Calendar.DAY_OF_WEEK)
返回。
在相关注释,请尝试学习和使用注释中所述的实际调试器。
On a related note, try to learn and use an actual debugger as stated in the comments.
这篇关于想要将星期几作为字符串,但是给错一天的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文