2010-06-14 1359 views
2
Sample Julian Dates: 
2009218 
2009225 
2009243 

如何将它们转换为正常日期?朱利安日期转换

我尝试使用online converter将他们和我的GOT

12-13-7359为2009225!没有意义!

+1

你的例子不是朱利安日期。朱利安日期是公元前4713年1月1日以来的天数 - 例如2000-01-01为2.451.545。 (如果2009225是你想要的朱利安日期,在线转换器是正确的) – oezi 2010-06-14 11:29:07

+0

我这么认为,但我不知道那些日期是那种日期!甚至不能问。 – 2010-06-14 11:30:09

+0

目前还不清楚你想要转换的东西:Julian日历日期?或修改过的朱利安_Days_(天文数据),如你的链接?如果这些日期是从一年开始的,那么存在模棱两可的余地(2009年1月11日或11月1日?)。 – McDowell 2010-06-14 11:59:57

回答

7

使用Joda-Time库,做这样的事情:

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

使用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(); 

---- ----编辑感谢 亚历克斯提供最佳回答:

Date myDate = new SimpleDateFormat("yyyyD").parse("2009218") 
+0

这个月怎么样? – 2010-06-14 11:59:31

+0

MutableDateTime会计算出该月份,因为它知道年份和年份。 – 2010-06-14 12:05:02

+0

有没有办法可以使用Java API来做到这一点? – 2010-06-14 12:06:29

2

另一种格式是CYYDDDD我写了这个函数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; 
}