Julian Date Conversion

不羁岁月 提交于 2019-12-06 11:06:14

Use the Joda-Time library and do something like this:

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

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

---- EDIT ---- Thanks for Alex for providing the best answer:

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

Another format is CYYDDDD I wrote this function in 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;
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!