简体   繁体   English

java.util.Calendar无法正确设置Month

[英]java.util.Calendar not setting Month correctly

The issue is pretty simple. 问题很简单。 What I want is to create a Calendar and set it with this attributes: Day: 17 Month: 10 (october) Year: 1989 我想要创建一个日历并使用以下属性进行设置:日:17月:10(十月)年:1989

But the calendar is not setting the MONTH properly. 但是日历不能正确设置“ MONTH”。

This is the code: 这是代码:

Calendar fecha = new GregorianCalendar();
fecha.set(Calendar.DAY_OF_YEAR, 17);
fecha.set(Calendar.MONTH, Calendar.OCTOBER);
System.out.println(new SimpleDateFormat("dd/MM/yyyy").format(fecha.getTime()));

And what I'm getting: 17/01/2013 ------------>The month is not October 而我得到的是:2013年1月17日------------>这个月不是十月

What's wrong with this? 这怎么了 Thanks in advance! 提前致谢!

如果要设置月份和年份,那么在设置日期时,请使用DAY_OF_MONTH而不是DAY_OF_YEAR

fecha.set(Calendar.DAY_OF_YEAR, 17);

belongs to JANUARY 属于JANUARY

You need to set Calendar.DATE 您需要设置Calendar.DATE

Replace: 更换:

Calendar fecha = new GregorianCalendar();
fecha.set(Calendar.DAY_OF_MONTH, 17);
fecha.set(Calendar.MONTH, Calendar.OCTOBER);
System.out.println(new SimpleDateFormat("dd/MM/yyyy").format(fecha.getTime()));

您需要使用DAY_OF_MONTH而不是DAY_OF_YEAR

Try this, It's work Fine, also Display Month in Words. 试试这个,它工作正常,还可以显示单词月份。

  `Calendar fecha = new GregorianCalendar();
    fecha.set(Calendar.DAY_OF_MONTH, 17);
    fecha.set(Calendar.MONTH, Calendar.OCTOBER);
    System.out.println(new SimpleDateFormat("dd/MMMM/yyyy").format(fecha.getTime()));`

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM