programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# How to get Day, Month from Julian days?

Greenhorn
Posts: 10
Julian day is a three-digit day between 1 and 366. I want to convert this number to Java Date Object to find the Day and Month in the year. Can you please help me find it out? Thaks in advance.

Ranch Hand
Posts: 410
I think you will have to implement your own Julian Calendar class, as the JDK only provides GregorianCalendar (and the base class Calendar, which you might want to extend).

Bartender
Posts: 10336
Presuming you are doing this for calculations before the Gregorian changeover, you can just use the GregorianCalendar class supplied. Does anywhere still use the Julian Calendar? If they do, this seems to implement it.
[ July 25, 2005: Message edited by: Paul Sturrock ]

Wanderer
Sheriff
Posts: 18671
Actually the term "Julian day" means someting very different from "Julian calendar". Julian day refers to counting the number of days since some standard reference point - most commonly since January 1, 4713 BC (a date which was convenient for conversions with another ancient calendar system, irrelevant now)). However a simple variant of this concept is to count the days since Jan 1 of the current year - that is evidently what Venkat is talking about. This can be done with a a Julian calendar or a Gregorian calendar - and since no one in modern times uses the Julian calendar, let's forget about it now.

[Venkat]: I want to convert this number to Java Date Object to find the Day and Month in the year.

Well, I think you will need more than just the one number, or there's going to be a problem. Consider:

Julian day 1 = Jan 1
Julian day 31 = Jan 31
Julian day 32 = Feb 1
Julian day 59 = Feb 28
Julian day 60 = Feb 29 or March 1?

Clearly, to go past 59 you need to know whether the current year is a leap year or not. If you know the year, great - but if you don't you've got a problem.

The GregorianCalendar class does have methods which will make this whole process fairly simple, if you know the year as well as month and day. Note that one of the things which you can get() and set() is the DAY_OF_YEAR, which is exactly what you're looking for. I recommend studying the Calendar and GregorianCalendar classes. You may find examples in the Java Developers' Almanac 1.4 to be useful. Or the Java Cookbook is an excellent resource (though not available free online like the almanac).

 Won't you be my neighbor? - Fred Rogers. tiny ad: Rocket Oven Kickstarter - from the trailboss https://coderanch.com/t/695773/Rocket-Oven-Kickstarter-trailboss