java 将儒略日期转换为常规日历日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3017954/
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-10-29 23:57:39  来源:igfitidea点击:

Convert a Julian Date to Regular Calendar Date

javajulian-date

提问by Mark Acosta

How do I convert a 7-digit julian date into a format like MM/dd/yyy?

如何将 7 位儒略日期转换为 MM/dd/yyy 格式?

回答by scott

Found a useful site: http://www.rgagnon.com/javadetails/java-0506.html

找到了一个有用的网站:http: //www.rgagnon.com/javadetails/java-0506.html

This should do the trick:

这应该可以解决问题:

 public static int[] fromJulian(double injulian) {

      int jalpha,ja,jb,jc,jd,je,year,month,day;
      double julian = julian + HALFSECOND / 86400.0;
      ja = (int) julian;
      if (ja>= JGREG) {    

       jalpha = (int) (((ja - 1867216) - 0.25) / 36524.25);
       ja = ja + 1 + jalpha - jalpha / 4;
       }
     jb = ja + 1524;
   jc = (int) (6680.0 + ((jb - 2439870) - 122.1) / 365.25);
   jd = 365 * jc + jc / 4;
   je = (int) ((jb - jd) / 30.6001);
   day = jb - jd - (int) (30.6001 * je);
   month = je - 1;
   if (month > 12) month = month - 12;
   year = jc - 4715;
   if (month > 2) year--;
   if (year <= 0) year--;

   return new int[] {year, month, day};
  }

回答by Nandan Kumar Singh

simple way is here and this will return approx 100% accurate information.

简单的方法在这里,这将返回大约 100% 准确的信息。

String getDobInfo(double doubleString){
        SweDate sweDate = new SweDate(doubleString);
        int year = sweDate.getYear();
        int month = sweDate.getMonth();
        int day = sweDate.getDay();
        // getting hour,minute and sec from julian date
        int hour = (int) Math.floor(sweDate.getHour());
        int min = (int) Math
                .round((sweDate.getHour() - Math.floor(hour)) * 60.0);
        int sec = (int) (((sweDate.getHour() - Math.floor(hour)) * 60.0 - Math
                .floor(min)) * 60.0);
        return "DOB:(DD:MM:YY) "+day+":"+month+":"+year+" TOB:(HH:MM:SS) "+hour+":"+min+":"+sec;
    }

download the Swiss Ephemeris library and enjoy coding!!!

下载瑞士星历库并享受编码!!!

回答by Tome

Starting with Java 8, this becomes a one-liner to get the LocalDate:

从 Java 8 开始,这变成了一个单线来获得LocalDate

LocalDate.MIN.with(JulianFields.JULIAN_DAY, julianDay)
.format(DateTimeFormatter.ofPattern("MM/dd/yyyy"));

Where julianDayis your 7-digit number.

julianDay你的 7 位数字在哪里。

回答by erickson

Do you really mean a Julian date, like astronomers use? Ordinal dates, which are specified as a year (four digits) and the day within that year (3 digits), are sometimes incorrectly called Julian dates.

你真的是指天文学家使用的儒略日期吗?指定为年份(四位数)和该年中的日期(3 位数)的序数日期有时被错误地称为儒略日期。

static String formatOrdinal(int year, int day) {
  Calendar cal = Calendar.getInstance();
  cal.clear();
  cal.set(Calendar.YEAR, year);
  cal.set(Calendar.DAY_OF_YEAR, day);
  Date date = cal.getTime();
  SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
  return formatter.format(date);
}

This will give you the date at 00:00 local time; you may want to set the timezone on the calendars to GMT instead, depending on the application.

这将为您提供当地时间 00:00 的日期;您可能希望将日历上的时区设置为 GMT,具体取决于应用程序。

回答by Aravind Yarram

I see there are enough answers already provided. But any calendar related question is only half answered without mentioning joda-time;-). Hereis how simple it is with this library

我看到已经提供了足够的答案。但是任何与日历相关的问题都只回答了一半而没有提到joda-time;-)。是这个库的简单之处

// setup date object for the Battle of Hastings in 1066
Chronology chrono = JulianChronology.getInstance();
DateTime dt = new DateTime(1066, 10, 14, 10, 0, 0, 0, chrono);