java - 将儒略日期转换为 Java 日期但仍然无法获取月份

标签 java date format converters julian-date

我正在尝试从儒略日期获取日、月和年。

    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/

相关文章:

java - 更改 Spring PropertyPlaceholderConfigurer 以从另一个源读取

ms-access - 如何格式化报告中的数字?

FileInputFormat,其中文件名是 KEY,文本内容是 VALUE

php - 通过 php 在 Maria db 上以 swe7 格式插入数据

Java将光标更改为图像

java - 通过JNA从Java程序访问dll文件的问题

java - 为什么我们在寻找素数时可以使用 sqrt(n) 而不是 n/2 作为上限?

python - 如何在python3上打印当前日期?

java - 需要帮助我的代码有错误

ruby-on-rails - Rails_admin, Date 添加更多接受格式