Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/17.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日历月索引_Java_Date_Calendar_Settings - Fatal编程技术网

Java日历月索引

Java日历月索引,java,date,calendar,settings,Java,Date,Calendar,Settings,设置了2019-12-27,但打印结果为2020-01-27。代码中有错误吗 public void testJavaDate(){ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); Date checkInDate; checkInDate = new Calendar.Builder() .setDate(2019, 12, 27) .build().g

设置了2019-12-27,但打印结果为2020-01-27。代码中有错误吗

public void testJavaDate(){
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    Date checkInDate;
    checkInDate = new Calendar.Builder()
            .setDate(2019, 12, 27)
            .build().getTime();
    System.out.println("checkIn sdf: "+sdf.format(checkInDate));
}
月份以0为基础: 从文件:

/**
 * Sets the date field parameters to the values given by {@code year},
 * {@code month}, and {@code dayOfMonth}. This method is equivalent to
 * a call to:
 * <pre>
 *   setFields(Calendar.YEAR, year,
 *             Calendar.MONTH, month,
 *             Calendar.DAY_OF_MONTH, dayOfMonth);</pre>
 *
 * @param year       the {@link Calendar#YEAR YEAR} value
 * @param month      the {@link Calendar#MONTH MONTH} value
 *                   (the month numbering is <em>0-based</em>).
 * @param dayOfMonth the {@link Calendar#DAY_OF_MONTH DAY_OF_MONTH} value
 * @return this {@code Calendar.Builder}
 */
public Builder setDate(int year, int month, int dayOfMonth) {
    return setFields(YEAR, year, MONTH, month, DAY_OF_MONTH, dayOfMonth);
}
/**
*将日期字段参数设置为{@code year}给定的值,
*{@code month}和{@code dayOfMonth}。此方法相当于
*呼吁:
* 
*设置字段(日历年、年份、,
*日历,月,月,
*日历。月中日,月中日);
*
*@param year{@link Calendar#year}值
*@param month{@link Calendar#month}值
*(月份编号以0为基础)。
*@param dayOfMonth{@link Calendar#DAY(月)DAY(月)DAY(月)值
*@返回此{@code Calendar.Builder}
*/
公共生成器设置日期(整数年、整数月、整数月日){
返回设置字段(年、年、月、月、月的日、月的日);
}

在legacy
java.util.Calendar
类中,月份是0索引的,所以12月是11。但是不要使用它,而是使用
java.time
包。您使用的是多年前被java.time类取代的可怕的旧类。与传统类不同,现代类使用合理的编号,例如1-12表示1月12日,1-7表示周一周日<代码>LocalDate.of(2019,12,27).toString()➙ <代码>2019-12-27。为了更清楚,请使用
Month
enum
LocalDate.of(2019年12月27日)。toString()
发布前搜索堆栈溢出。您可以假设任何基本的日期-时间问题都已经被问到并得到了回答。最好引导人们使用java.time类,它通过采用java.time类取代了这些糟糕的旧日期-时间类。鼓励2018年使用
日历
是一个糟糕的建议。