在Java中将常规日期转换为Julian日期,反之亦然

时间:2013-01-26 10:21:52

标签: java date converter julian

我写了一个简单的代码,将常规日期转换为Julian日期。

以下是需要相同转换的代码:

    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;
}

此代码将01.01.2013转换为113001

我想要做的是将Julian日期转换为没有时间细节的常规日期。 例如:Julian日期是113029 ==&gt;定期日期29.01.2013

请告诉我你如何做的想法。

感谢。

2 个答案:

答案 0 :(得分:4)

如果你想要113029 ==&gt; 29.01.2013尝试

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

输出

29.01.2013

答案 1 :(得分: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};
}