将Julian日期转换为Java日期,但仍不能获得Month [英] Converting Julian date to Java date but still cannot get Month

查看:175
本文介绍了将Julian日期转换为Java日期,但仍不能获得Month的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 字符串日期=13136; / / Julian日期
日期convertedDate = new SimpleDateFormat(yyDDD)。parse(date);
System.out.println(convertedDate);

打印

  Thu May 16 00:00:00 BST 2013 

这是正确的。 p>

现在我想从中获得日,月和年

 日历cal = Calendar.getInstance(); 
cal.setTime(convertedDate);
System.out.println(cal.get(Calendar.MONTH));


打印4。

应该打印5而不是4。为什么打印不正确?我在这里做错了什么? Calendar.MONTH :


get和set表示月份的字段编号。这是
日历特定的值。格里高利$ b和朱利安日历中的第一个月是JANUARY,它是0;最后一年取决于
一年中的月数。

所以月份从零开始,所以您的输出 4 是正确的,对于你的代码中的一般用例,除非你使用 1 作为<$ code> MONTH 值再次显示在日历中。


I am trying to get day ,month and year from a Julian date.

    String date = "13136";//Julian date
    Date convertedDate = new SimpleDateFormat("yyDDD").parse(date);
    System.out.println(convertedDate);

It prints

Thu May 16 00:00:00 BST 2013

which is correct.

Now I want to get Day , Month and Year from it

  Calendar cal = Calendar.getInstance();
  cal.setTime(convertedDate);
  System.out.println(cal.get(Calendar.MONTH));


It prints 4 .

It should print 5 instead of 4 . Why is it not printing as correct ? What I have done wrong here?

解决方案

As per the javadoc of Calendar.MONTH:

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.

So months starts from zero so your output 4 is correct, for general usecase in your code it would be safe to add 1 to it unless you use this values as MONTH value in Calendar again.

这篇关于将Julian日期转换为Java日期,但仍不能获得Month的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆