2010-06-10 285 views

回答

5

找到一個有用的網站:http://www.rgagnon.com/javadetails/java-0506.html

這應該做的伎倆:

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

ja =(int)injulian;是一個錯誤,將其更改爲ja =(int)julian; – RealHowTo 2010-08-12 21:22:17

0

你真的是一個Julian日期,像天文學家使用?有序日期被指定爲一年(四位數字)和該年的一天(三位數字),有時被錯誤地稱爲儒略日期。

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

這會給你在當地時間00:00的日期;您可能希望將日曆上的時區設置爲GMT,具體取決於應用程序。

0

我看到已經提供了足夠的答案。但任何與日曆相關的問題只有一半的答案沒有提及joda-time ;-)。 Here是多麼簡單的與此庫

// setup date object for the Battle of Hastings in 1066 
Chronology chrono = JulianChronology.getInstance(); 
DateTime dt = new DateTime(1066, 10, 14, 10, 0, 0, 0, chrono); 
3

簡單的方法是在這裏,這將返回約100%準確的信息。

String getDobInfo(double doubleString){ 
     SweDate sweDate = new SweDate(doubleString); 
     int year = sweDate.getYear(); 
     int month = sweDate.getMonth(); 
     int day = sweDate.getDay(); 
     // getting hour,minute and sec from julian date 
     int hour = (int) Math.floor(sweDate.getHour()); 
     int min = (int) Math 
       .round((sweDate.getHour() - Math.floor(hour)) * 60.0); 
     int sec = (int) (((sweDate.getHour() - Math.floor(hour)) * 60.0 - Math 
       .floor(min)) * 60.0); 
     return "DOB:(DD:MM:YY) "+day+":"+month+":"+year+" TOB:(HH:MM:SS) "+hour+":"+min+":"+sec; 
    } 

下載Swiss Ephemeris library並享受編碼!

1

與Java 8開始,這將成爲一個班輪得到LocalDate

LocalDate.MIN.with(JulianFields.JULIAN_DAY, julianDay) 
.format(DateTimeFormatter.ofPattern("MM/dd/yyyy")); 

哪裏julianDay是您的7位數字。