编辑:谢谢 - 我现在对此了解得更多。当你第一次开始时它非常混乱!谢谢您的帮助。我将继续保持这个问题(在更多的downvotes的危险),因为它可能会帮助其他人。每个人似乎都建议使用其他一些日期时间
我正在努力使用Java日历功能 - 它似乎正在返回错误数据,根据日历,底部数字应该是星期四,但是星期六会回来!
Calendar cal = new GregorianCalendar();
cal.set(2012,2,23); // 0 = January
String weekdays[]={"sunday","monday", "tuesday", "wednesday","thursday","friday","saturday",};
Integer Weekdaycurrent1=cal.get(Calendar.DAY_OF_WEEK);
System.out.println("today is a "+weekdays[Weekdaycurrent1]); //Prints today is Saturday, when it should be a thursday
答案 0 :(得分:4)
对于初学者,DAY_OF_WEEK
基于1
:
public final static int SUNDAY = 1;
其次,由2012-03-23
设定的cal.set(2012, 2, 23)
(是,Mar,而非2月)是 Friday
您的代码表现正常。
编辑:对于那些太蹩脚而无法正确阅读问题的人,调用cal.set(2012,2,23)
会将日期设置为2012-03-23
,因为month
参数从零开始(即Jan = 0,Feb = 1,Mar = 2等)
答案 1 :(得分:2)
你的错误假设。阅读javadocs:
http://docs.oracle.com/javase/1.4.2/docs/api/constant-values.html#java.util
public static final int SUNDAY 1
public static final int MONDAY 2
public static final int TUESDAY 3
public static final int WEDNESDAY 4
public static final int THURSDAY 5
public static final int FRIDAY 6
public static final int SATURDAY 7