views:

72

answers:

1
Sample Julian Dates:
2009218
2009225
2009243

How do I convert them into a regular date?

I tried converting them using online converter and I got-

12-13-7359 for 2009225!! Makes no sense!

+2  A: 

Use the Joda-Time library and do something like this:

String dateStr = "2009218";
MutableDateTime mdt = new MutableDateTime();
mdt.setYear(Integer.parseInt(dateStr.subString(0,3)));
mdt.setDayOfYear(Integer.parseInt(dateStr.subString(4)));
Date parsedDate  = mdt.toDate();

Using the Java API:

String dateStr = "2009218";
Calendar cal  = new GregorianCalendar();
cal.set(Calendar.YEAR,Integer.parseInt(dateStr.subString(0,3)));
cal.set(Calendar.DAY_OF_YEAR,Integer.parseInt(dateStr.subString(4)));
Date parsedDate  = cal.getTime();

---- EDIT ---- Thanks for Alex for providing the best answer:

Date myDate = new SimpleDateFormat("yyyyD").parse("2009218")
Mike Sickler
How about the month?
Milli Szabo
MutableDateTime will figure out the month, since it knows the year and day of year.
Mike Sickler
Is there no way I can use the Java API to do that?
Milli Szabo
Sure- I've updated my answer.
Mike Sickler
Thanks a zillion!
Milli Szabo
An upvote and/or marking my answer as correct would be appreciated :)
Mike Sickler
Sorry - my bad. :)) Note: You might want to edit a little further since set doesn't have (int, long) methods. Might want to change them to integer.
Milli Szabo
Use the built in date parsers!
Alex Feinman
Elaboration on that would be really appreciated. Thanks.
Milli Szabo
Alex is right. That's the best answer, you could do `new SimpleDateFormat("yyyyD").parse("2009218")` to get your Date object
Mike Sickler