Java 儒略日期转换

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3036761/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 15:51:45  来源:igfitidea点击:

Julian Date Conversion

javajulian-date

提问by Milli Szabo

Sample Julian Dates:
2009218
2009225
2009243

How do I convert them into a regular date?

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

I tried converting them using online converterand I got-

我尝试使用在线转换器转换它们,我得到了-

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

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

采纳答案by Mike Sickler

Use the Joda-Timelibrary and do something like this:

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

Using the Java API:

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

回答by gishac

Another format is CYYDDDD I wrote this function in Java

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