Convert Julian date to Java date but still can't get month

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

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

He is typing

 Thu May 16 00:00:00 BST 2013 

what is right.

Now I want to get a day, a month and a year from him

  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 doesn't it print as correct? What have I done wrong here?

+2
source share
3 answers

According to javadoc Calendar.MONTH :

The number of the field to receive and install, indicating the month. This is a calendar value. The first month of the year in Gregorian and Julian calendars is JANUARY, which is 0; the latter depends on the number of months in a year.

So, the months start from zero, so your 4 result is correct, for general use in your code it would be safe to add 1 to it if you do not use these values ​​again as the MONTH value in Calendar .

+3
source

Like javadocs state, months start from zero: 0 = January, 1 = February, etc.

0
source

TL; DR

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

5

... for May 2013.

Ordinal, not Julian

Your use of the word "Julian" is technically incorrect, although common. It seems that people are embarrassed by the number of days (1-365 or 1-366) with the practice of counting the number of days that have passed since January 1, 4713 BC is used in some scientific fields.

The terms "ordinal date" or day of the year are clearer.

ISO 8601

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

  • YYYY-DDD
  • YYYYDDD

java.time

The modern way is the java.time classes, which supersede the nasty old obsolete time classes.

DateTimeFormatter

Note that the formatting pattern codes in the DateTimeFormatter class are similar to a legacy class, but not quite a few.

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

LocalDate

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

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

Dump for the console.

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

localDate: 2013-05-16

Month

You can ask about the month of this LocalDate . The Month variable Month dozen objects, one for each month of the year. And unlike the crazy heritage classes, they are reasonably numbered 1-12 for January-December.

If you pass the month number around your code, I suggest that you pass these enumeration objects instead. This gives you the type of security, valid values, and self-documenting code.

 Month month = localDate.getMonth(); 

If necessary, you can get the localized name of this month.

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

If you really need the month number 1-12, ask anyway.

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

About java.time

The java.time framework is built into Java 8 and later. These classes supersede the nasty old legacy time classes such as java.util.Date , Calendar and SimpleDateFormat .

The Joda-Time project, now in maintenance mode , we recommend switching to the java.time classes.

To learn more, see the Oracle Tutorial . And search for qaru for many examples and explanations. JSR 310 specification .

Where to get java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is proof of possible future additions to java.time. Here you can find useful classes such as Interval , YearWeek , YearQuarter and more .

0
source

All Articles