欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Java日期时间的设置问题

程序员文章站 2022-05-18 17:19:34
...

Calender的月份是从0开始的,但日期和年份是从1开始的

例1

import java.util.Calendar;

public class Test {
    public static void main(String[] args) {
            Calendar c1 = Calendar.getInstance();
            c1.set(2017, 1, 1);
            System.out.println(c1.get(Calendar.YEAR)
                    +"-"+c1.get(Calendar.MONTH)
                    +"-"+c1.get(Calendar.DATE));
            c1.set(2017, 1, 0);
            System.out.println(c1.get(Calendar.YEAR)
                    +"-"+c1.get(Calendar.MONTH)
                    +"-"+c1.get(Calendar.DATE));
    }
}

运行结果:

2017-1-1
2017-0-31

可见,将日期设为0以后,月份变成了上个月,但月份可以为0
把月份改为2试试:

例2

import java.util.Calendar;

public class Test {
    public static void main(String[] args) {
            Calendar c1 = Calendar.getInstance();
            c1.set(2017, 2, 1);
            System.out.println(c1.get(Calendar.YEAR)
                    +"-"+c1.get(Calendar.MONTH)
                    +"-"+c1.get(Calendar.DATE));
            c1.set(2017, 2, 0);
            System.out.println(c1.get(Calendar.YEAR)
                    +"-"+c1.get(Calendar.MONTH)
                    +"-"+c1.get(Calendar.DATE));
    }
}

运行结果:

2017-2-1
2017-1-28

可以看到上个月的最后一天是28号,所以Calendar.MONTH为1的时候是2月的天数
既然日期设为0表示上个月的最后一天,那是不是可以设为负数呢?

例3

import java.util.Calendar;

public class Test {
    public static void main(String[] args) {
            Calendar c1 = Calendar.getInstance();
            c1.set(2017, 2, 1);
            System.out.println(c1.get(Calendar.YEAR)
                    +"-"+c1.get(Calendar.MONTH)
                    +"-"+c1.get(Calendar.DATE));
            c1.set(2017, 2, -10);
            System.out.println(c1.get(Calendar.YEAR)
                    +"-"+c1.get(Calendar.MONTH)
                    +"-"+c1.get(Calendar.DATE));
    }
}

运行结果:

2017-2-1
2017-1-18

月份也可以是负数,规则与日期一样,但月份从0开始,不能用0设置上一年的12月。

例4

import java.util.Calendar;

public class Test {
    public static void main(String[] args) {
            Calendar c1 = Calendar.getInstance();
            c1.set(2017, 0, 1);
            System.out.println(c1.get(Calendar.YEAR)
                    +"-"+c1.get(Calendar.MONTH)
                    +"-"+c1.get(Calendar.DATE));
            c1.set(2017, -2, 10);
            System.out.println(c1.get(Calendar.YEAR)
                    +"-"+c1.get(Calendar.MONTH)
                    +"-"+c1.get(Calendar.DATE));
    }
}

运行结果:

2017-0-1
2016-10-10

将年份设为非正数时,会自动变为绝对值+1。

例5

import java.util.Calendar;

public class Test {
    public static void main(String[] args) {
            Calendar c1 = Calendar.getInstance();
            c1.set(-2017, 1, 1);
            System.out.println(c1.get(Calendar.YEAR)
                    +"-"+c1.get(Calendar.MONTH)
                    +"-"+c1.get(Calendar.DATE));
            c1.set(-2017, -2, -1);
            System.out.println(c1.get(Calendar.YEAR)
                    +"-"+c1.get(Calendar.MONTH)
                    +"-"+c1.get(Calendar.DATE));
    }
}

运行结果:

2018-1-1
2017-9-30