朱利安日期转换

时间:2010-06-14 11:20:42

标签: java julian-date

Sample Julian Dates:
2009218
2009225
2009243

如何将它们转换为常规日期?

我尝试使用online converter转换它们,我得到了 -

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

2 个答案:

答案 0 :(得分: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();

----编辑---- 感谢Alex提供最佳答案:

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

答案 1 :(得分:3)

另一种格式是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;
}