0

编辑:谢谢-我现在明白了很多。当你第一次开始时,它非常混乱!谢谢您的帮助。我将按原样保持这个问题(冒着更多反对票的危险),因为它可能会帮助其他人。每个人似乎都推荐其他一些用于日期时间的库


我正在努力使用 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
4

2 回答 2

4

首先,DAY_OF_WEEK基于1

public final static int SUNDAY = 1;

其次,2012-03-23(是的,3 月而不是 2 月)设定cal.set(2012, 2, 23)星期五

您的代码行为正确。

编辑:对于那些太蹩脚而无法正确阅读问题的人,调用cal.set(2012,2,23)将日期设置为2012-03-23,因为month参数是从零开始的(即 Jan = 0、Feb = 1、Mar = 2 等)

于 2012-05-14T00:51:12.133 回答
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
于 2012-05-14T00:52:29.540 回答