Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/375.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Calendar的setMonth方法是否工作错误?_Java_Calendar - Fatal编程技术网

Java Calendar的setMonth方法是否工作错误?

Java Calendar的setMonth方法是否工作错误?,java,calendar,Java,Calendar,我有下面这样的小代码。我希望结果应该是7,但它打印了6。如果我取消注释行tmp.get(Calendar.MONTH),它运行正常(打印7) 请告诉我原因。我在MacOS中使用JDK1.7.025 publicstaticvoidmain(字符串[]args){ Calendar tmp=Calendar.getInstance(); tmp.set(日历日/月,4); tmp.set(日历.月,日历.月); //tmp.get(日历月); tmp.set(Calendar.DAY\u OF_W

我有下面这样的小代码。我希望结果应该是
7
,但它打印了
6
。如果我取消注释行
tmp.get(Calendar.MONTH)
,它运行正常(打印
7

请告诉我原因。我在MacOS中使用JDK1.7.025

publicstaticvoidmain(字符串[]args){
Calendar tmp=Calendar.getInstance();
tmp.set(日历日/月,4);
tmp.set(日历.月,日历.月);
//tmp.get(日历月);
tmp.set(Calendar.DAY\u OF_WEEK,Calendar.MONDAY);
System.out.println(tmp.get(日历月));
}
屏幕截图:
注释代码:

取消注释代码:

,因为月份索引以索引0开头。在获取月份时添加+1。它是复制到java中的基于c的结构。它的索引为0到11

我认为月日是不正确的。对其进行注释并正确运行。(
tmp.set(Calendar.DAY\u OF_WEEK,Calendar.MONDAY);


默认情况下,时间为2015年,8月4日为
星期二
星期一
当您设置周中的天=星期一时,日历返回到上周一,时间为7月27日,您可以使用
System.out.println(tmp.get(日历月中的天))

我试着运行你的代码,它每次打印7(甚至试着取消你提到的那行的注释)。这对我来说似乎是正确的

编辑详细解释了该问题

我很高兴Java8带来了处理日期和时间的新API

EDIT2

我对所发生事情的理解:

只有明智的组合才是正确的

YEAR + MONTH + DATE (let's call it A)

日历有其内在的日期状态,他认为自己是。所以基本上在你开始之前

DAY_OF_WEEK
你用的是组合A,但之后你用的是组合B和这条线

tmp.set(Calendar.DAY_OF_MONTH, 4);
被忽视了。因此,在8月的第一周(2015-07-27)的周一。但是当你打电话的时候

tmp.get(Calendar.MONTH);
您根据组合A(截至2015-08-04)和

从2015-08-04开始设置为“正确”

不管怎样,你能用JDK 8试试吗

避免使用Java-8之前的日期和日历

这在某种程度上同样适用于日历,它也有一些相同的问题。(德国链接)是

  • 不是日历,而是日期+时间
  • 易变的
  • 一月是
    0
    th月,而不是
    1
    st月
  • 非直观API:您必须使用
    get(Calendar.HOUR)
    而不是
    getHour

如果您在设置星期几时使用Java,日历无法知道您期望的是什么。如果您希望得到同一周中另一天的日期,您也应该设置一年中的一周,以指示您希望得到的是哪一周。

但是8月是8日,减去1后,我们应该有7日。对吧?我早就知道了。因此,如果我将一周的日期更改为星期一,它将移动到最后一个星期一,因此应该是2003年8月3日星期一。最后一个结果是7。它将覆盖由2行代码设置的月份。看看源代码。2004年8月是星期二。所以上周一应该是8月3日谢谢!我在考虑JDK或OS版本。这就是我共享我的OS和JDK的原因。你能添加System.out.println(tmp.get(Calendar.MONTH)+“:“+tmp.getTime());你的代码?查看您的最终日期会很有趣。带有注释的结果get函数:(6--周一7月27日17:33:12 ICT 2015)和取消注释:(7--周一8月3日17:31:23 ICT 2015)查看
tmp.set(Calendar.DAY_OF_MONTH, 4);
tmp.get(Calendar.MONTH);
DAY_OF_WEEK
import org.joda.time.MutableDateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

public class Test2 {
    public static void main(String[] args) {
    MutableDateTime tmp = new MutableDateTime();
    tmp.setDayOfMonth(4);
    tmp.setMonthOfYear(8);
    tmp.setDayOfWeek(1);

    DateTimeFormatter fmt = DateTimeFormat.fullDate();
    System.out.println(fmt.print(tmp));
    }
}
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

public class Test {
    public static void main(String[] args) {
        DateTimeFormatter fmt = DateTimeFormat.fullDate();

        DateTime tmp = new DateTime().withDayOfMonth(4).withMonthOfYear(8);
        System.out.println(fmt.print(tmp));

        tmp = tmp.withDayOfWeek(1);
        System.out.println(fmt.print(tmp));
    }
}
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class Test {
    public static void main(String[] args) {
        DateTimeFormatter fmt = DateTimeFormatter.RFC_1123_DATE_TIME;

        LocalDateTime tmp
            = LocalDateTime.now().withDayOfMonth(4).withMonthOfYear(8);
        System.out.println(fmt.format(tmp));

        tmp = tmp.withDayOfWeek(1);
        System.out.println(fmt.format(tmp));
    }
}