Julian Date Conversion

Milli Szabo picture Milli Szabo · Jun 14, 2010 · Viewed 22.6k times · Source
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!

Answer

Mike Sickler picture Mike Sickler · Jun 14, 2010

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")