Julian Date Conversion

守給你的承諾、 提交于 2019-12-10 11:19:17

问题


Sample Julian Dates:
2009218
2009225
2009243

How do I convert them into a regular date?

I tried converting them using online converter and I got-

12-13-7359 for 2009225!! Makes no sense!


回答1:


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")



回答2:


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


来源:https://stackoverflow.com/questions/3036761/julian-date-conversion

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!