Converting Julian date to Java date but still cannot get Month
Asked Answered
B

3

2

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?

Balloon answered 16/5, 2013 at 11:58 Comment(0)
G
3

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.

Genous answered 16/5, 2013 at 12:5 Comment(0)
C
0

As the javadocs state, months begin at zero: 0 = January, 1 = February, and so on.

Cryobiology answered 16/5, 2013 at 12:6 Comment(0)
K
0

tl;dr

LocalDate.parse ( 
    "13136", 
    DateTimeFormatter.ofPattern ( "uuDDD" ) 
).getMonthValue()

5

…for month of May 2013.

Ordinal, not Julian

Your use of the word “Julian” is technically incorrect, though common. Folks seem to confuse day-of-year (1-365 or 1-366) with practice of counting the number of days elapsed since January 1, 4713 BC used in some scientific fields.

The terms “ordinal date” or day-of-year are more clear.

ISO 8601

Your format for ordinal dates is not standard. Whenever possible, use the standard ISO 8601 formats:

  • YYYY-DDD
  • YYYYDDD

java.time

The modern way is with the java.time classes that supplant the troublesome old legacy date-time classes.

DateTimeFormatter

Note that the formatting pattern codes in DateTimeFormatter class are similar to the legacy class but not exactly the some.

String input = "13136";  //Julian date
DateTimeFormatter f = DateTimeFormatter.ofPattern ( "uuDDD" );

LocalDate

The LocalDate class represents a date-only value without time-of-day and without time zone.

LocalDate localDate = LocalDate.parse ( input, f );

Dump to console.

System.out.println ("localDate: " + localDate );

localDate: 2013-05-16

Month

You can ask about the month of that LocalDate. The Month enum pre-defines a dozen objects, one for each month of the year. And unlike the crazy legacy classes, these are sanely numbered 1-12 for January-December.

If you are passing the month number around your code, I suggest you instead pass around these enum objects. Doing so gives you type-safety, valid values, and self-documenting code.

Month month = localDate.getMonth();

You can get the localized name of that month if needed.

String output = month.getDisplayName( TextStyle.FULL , Locale.CANADA_FRENCH );  // Or Locale.US, Locale.ITALY, whatever.

If you truly do need the number of the month 1-12, ask in either way.

int monthNumber = month.getValue() ;
int monthNumber = localDate.getMonthValue() ;

About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

Kidd answered 20/3, 2017 at 2:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.