views:

178

answers:

3

How do I convert a 7-digit julian date into a format like MM/dd/yyy?

+1  A: 

Here is example code - assuming you mean the Julian date astronomers use.

http://www.rgagnon.com/javadetails/java-0506.html

jim mcnamara
+1  A: 

Found a useful site: [http://www.rgagnon.com/javadetails/java-0506.html][1] This should do the trick -

 public static int[] fromJulian(double injulian) {

      int jalpha,ja,jb,jc,jd,je,year,month,day;
      double julian = julian + HALFSECOND / 86400.0;
      ja = (int) julian;
      if (ja>= JGREG) {    

       jalpha = (int) (((ja - 1867216) - 0.25) / 36524.25);
       ja = ja + 1 + jalpha - jalpha / 4;
       }
     jb = ja + 1524;
   jc = (int) (6680.0 + ((jb - 2439870) - 122.1) / 365.25);
   jd = 365 * jc + jc / 4;
   je = (int) ((jb - jd) / 30.6001);
   day = jb - jd - (int) (30.6001 * je);
   month = je - 1;
   if (month > 12) month = month - 12;
   year = jc - 4715;
   if (month > 2) year--;
   if (year <= 0) year--;

   return new int[] {year, month, day};
  }
scott
ja = (int) injulian; is an error, change it to ja = (int) julian;
RealHowTo
A: 

Do you really mean a Julian date, like astronomers use? Ordinal dates, which are specified as a year (four digits) and the day within that year (3 digits), are sometimes incorrectly called Julian dates.

static String formatOrdinal(int year, int day) {
  Calendar cal = Calendar.getInstance();
  cal.clear();
  cal.set(Calendar.YEAR, year);
  cal.set(Calendar.DAY_OF_YEAR, day);
  Date date = cal.getTime();
  SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
  return formatter.format(date);
}

This will give you the date at 00:00 local time; you may want to set the timezone on the calendars to GMT instead, depending on the application.

erickson