6

I've written a simple code that converts a regular date to Julian date.

Here's the code for those who need the same conversion:

    public int convertToJulian(String unformattedDate)
    {
    /*Unformatted Date: ddmmyyyy*/
    int resultJulian = 0;
    if(unformattedDate.length() > 0)
    {
     /*Days of month*/
     int[] monthValues = {31,28,31,30,31,30,31,31,30,31,30,31};

     String dayS, monthS, yearS;
     dayS = unformattedDate.substring(0,2);
     monthS = unformattedDate.substring(3, 5);
     yearS = unformattedDate.substring(6, 10);

     /*Convert to Integer*/
     int day = Integer.valueOf(dayS);
     int month = Integer.valueOf(monthS);
     int year = Integer.valueOf(yearS); 

         //Leap year check
         if(year % 4 == 0)
         {
          monthValues[1] = 29;    
         }
         //Start building Julian date
         String julianDate = "1";
         //last two digit of year: 2012 ==> 12
         julianDate += yearS.substring(2,4);

         int julianDays = 0;
         for (int i=0; i < month-1; i++)
         {
          julianDays += monthValues[i];
         }
         julianDays += day;

             if(String.valueOf(julianDays).length() < 2)
             {
              julianDate += "00";
             }
             if(String.valueOf(julianDays).length() < 3)
             {
              julianDate += "0";
             }

        julianDate += String.valueOf(julianDays);
    resultJulian =  Integer.valueOf(julianDate);    
 }
 return resultJulian;
}

This code converts 01.01.2013 to 113001

What I want to do is to convert a Julian date to regular date without time details. For example: Julian date is 113029 ==> Regular date 29.01.2013

Please give me your ideas on how to do it.

Thanks.

tanzer
  • 303
  • 1
  • 5
  • 11

2 Answers2

4

If you want 113029 ==> 29.01.2013 try

    String j = "113029";
    Date date = new SimpleDateFormat("Myydd").parse(j);
    String g = new SimpleDateFormat("dd.MM.yyyy").format(date);
    System.out.println(g);

output

29.01.2013
Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
  • Is there a simple code like this for conversion of Gregorian date to Julian date? – Bugs Happen Jul 10 '15 at 11:02
  • 1
    I'm not sure this is a valid solution for most dates. Any day after April 8th would be 3 digits for the day, and thus dd should not parse correctly? Is there some undocumented behavior in SimpleDateFormat that handles this case? – solvingJ May 09 '16 at 20:37
  • Could you explain pattern "Myydd" here? Converted "000001" to "19991201" which is incorrect. – niaomingjian Jun 22 '18 at 06:42
3
public static int[] julianToGregorian(double injulian) {
    int JGREG= 15 + 31*(10+12*1582);     
    int jalpha,ja,jb,jc,jd,je,year,month,day;
    double julian = injulian + 0.5 / 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};
}
BackSlash
  • 21,927
  • 22
  • 96
  • 136
  • Thanks for response. I tried that function but I think it doesn't work. I sent 112304 as julian date parameter which is 30.10.2012 in regular date. It returned -4406 6 22 in an integer array. – tanzer Jan 26 '13 at 11:02
  • It is right, check http://aa.usno.navy.mil/data/docs/JulianDate.php , put your julian number in the "Julian Date" field, and the result will be `JD 112304.000000 is BCE 4406 June 22 12:00:00.0 UT Thursday` – BackSlash Jan 26 '13 at 11:15
  • In JD Edwards database the date in Julian is kept as 112304 where 1 is constant, 12 is year and 304 is the day of year. 112304 is 30.10.2012 but the converter that you said includes date time details and adds it which I don't want. The function you've sent is working you're right but in a way that I don't need. Thanks. – tanzer Jan 26 '13 at 11:38
  • 4
    @tanzer those JD Edwards dates are definitely NOT "standard Julian" dates. See this question: http://stackoverflow.com/questions/1171208/what-is-the-precise-definition-of-jdes-julian-date-format – Jose_GD May 06 '14 at 14:51