我正在尝试从儒略日期获取日、月和年。
String date = "13136";//Julian date
Date convertedDate = new SimpleDateFormat("yyDDD").parse(date);
System.out.println(convertedDate);
打印
Thu May 16 00:00:00 BST 2013
这是正确的。
现在我想从中获取日、月和年
Calendar cal = Calendar.getInstance();
cal.setTime(convertedDate);
System.out.println(cal.get(Calendar.MONTH));
It prints 4 .
它应该打印 5 而不是 4 。为什么打印不正确?我在这里做错了什么?
最佳答案
根据 Calendar.MONTH
的 javadoc:
Field number for get and set indicating the month. This is a calendar-specific value. The first month of the year in the Gregorian and Julian calendars is JANUARY which is 0; the last depends on the number of months in a year.
因此月份从零开始,因此您的输出 4
是正确的,对于代码中的一般用例,向其中添加 1
是安全的,除非您将此值用作再次在Calendar
中显示MONTH
值。
关于java - 将儒略日期转换为 Java 日期但仍然无法获取月份,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16586806/