2
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!

Milli Szabo
  • 33
  • 1
  • 1
  • 3
  • 2
    your examples aren't julian dates. an julian date is the number of days since January 1, 4713 BC - for example 2.451.545 for 2000-01-01. (if 2009225 is a julian date like you want it to be, the online converter is right) – oezi Jun 14 '10 at 11:29
  • I thought so but I have no idea what kind of dates are those! Can't even ask. – Milli Szabo Jun 14 '10 at 11:30
  • it isn't clear to me what you're trying to convert from: Julian Calendar dates? Or Modified Julian _Days_ (astronomical data), as in your link? If those dates start with a year, there is room for ambiguity (is 2009111 January 11 or November 1?). – McDowell Jun 14 '10 at 11:59
  • I wish I could ask the client and answer your question. – Milli Szabo Jun 14 '10 at 12:02
  • The only thing I can think of is that they are of the format "YYYYDDD" - the DDDth day of year YYYY. However, if you want to make sure it's done right *you have to ask the source of the dates*, or at the very least, have a sufficient amount of examples (both the encoded date and the corresponding actual date) to be able to determine the meaning. – Michael Madsen Jun 14 '10 at 12:22

2 Answers2

7

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
  • 33,662
  • 21
  • 64
  • 90
3

Another format is CYYDDDD I wrote this function in Java

public static int convertToJulian(Date date){
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    int year = calendar.get(Calendar.YEAR);
    String syear = String.format("%04d",year).substring(2);
    int century = Integer.parseInt(String.valueOf(((year / 100)+1)).substring(1));
    int julian = Integer.parseInt(String.format("%d%s%03d",century,syear,calendar.get(Calendar.DAY_OF_YEAR)));
    return julian;
}
kleopatra
  • 51,061
  • 28
  • 99
  • 211
gishac
  • 31
  • 1