将Julian日期转换为Instant [英] Convert a Julian Date to an Instant

查看:123
本文介绍了将Julian日期转换为Instant的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个我希望从Julian日期转换为 java.time.Instant (如果这有意义),或者某些可以更容易理解的Java时间。我对Julian日期的理解来自阅读维基百科页面。有许多不同的变体,我试图阅读的日期使用不同的时代比其中任何一个。

I'm running into a situation where I would like to convert from a Julian date to an java.time.Instant (if that makes sense), or some Java time that can be more easily understood. My understanding of what a Julian date is comes from reading the Wikipedia page. There are bunch of different variants, and the date I am trying to read uses a different epoch than any of these.

例如,假设时代是日历(新风格)法案1750 ,朱利安日期 95906.27600694445 在这种情况下,我认为是 CE 2015年4月15日06:37:26.9 UT ,我如何从中得到一个瞬间?我需要稍后调整时区。

For example, let's say the epoch is the beginning of the Calendar (New Style) Act 1750, and the Julian date is 95906.27600694445 which in this case I believe is CE 2015 April 15 06:37:26.9 UT, how do I get an instant from this? I will need to adjust for the timezone later.

我注意到有一个名为 JulianFields ,但我不知道在哪里/如何用它。此外,我在包中看到的大多数方法都使用 int long ,而不是<$ c c $ c> double 。

I noticed there is a class called JulianFields, but I don't know where/how to use it. Also, most of the methods I see in the package make use of int or long, not really anything for double.

那么,是否有一种简单的方法可以将使用不同时期的Julian日期转换为Java 8 即时(如果我的想法错误,还是其他时间)。

So, is there a simple way to convert from a Julian date using a different epoch to a Java 8 Instant (or some other time if my thinking is wrong).

推荐答案

以下是使用新Java 8类的解决方案:

Here is a solution using the new Java 8 classes:

public class JulianDay {
    private static final double NANOS_PER_DAY = 24.0 * 60.0 * 60.0 * 1000000000.0;

    // Calculate Instants for some epochs as defined in Wikipedia.
    public static final Instant REDUCED_JD =
            ZonedDateTime.of(1858, 11, 16, 12, 0, 0, 0, ZoneOffset.UTC).toInstant();
    public static final Instant MODIFIED_JD =
            ZonedDateTime.of(1858, 11, 17, 0, 0, 0, 0, ZoneOffset.UTC).toInstant();
    public static final Instant JULIAN_DATE =
            REDUCED_JD.minus(2400000, ChronoUnit.DAYS);

    private final Instant epoch;

    public JulianDay(Instant epoch) {
        super();
        this.epoch = epoch;
    }

    public Instant toInstant(double day) {
        long l = (long) day;
        return epoch
                .plus(l, ChronoUnit.DAYS)
                .plusNanos(Math.round((day - l) * NANOS_PER_DAY));
    }

    public static void main(String[] args) {
        // Use the example values from Wikipedia for 2015-09-07 13:21 UTC.
        System.out.println(new JulianDay(REDUCED_JD).toInstant(57273.05625));
        // Output: 2015-09-07T13:21:00.000000126Z
        System.out.println(new JulianDay(MODIFIED_JD).toInstant(57272.55625));
        // Output: 2015-09-07T13:21:00.000000126Z
        System.out.println(new JulianDay(JULIAN_DATE).toInstant(2457273.05625));
        // Output: 2015-09-07T13:20:59.999991953Z
    }
}

关于您询问的 JulianFields ,您可以定义这样的自定义格式化程序:

Regarding the JulianFields you asked about, you can define a custom formatter like this:

DateTimeFormatter formatter = new DateTimeFormatterBuilder()
    .appendValue(JulianFields.MODIFIED_JULIAN_DAY)
    .toFormatter().withZone(ZoneOffset.UTC);

不幸的是它不支持几天的分数:

Unfortunately it doesn't support fractions of days:

System.out.println(formatter.format(Instant.now())); // Output: 57249
System.out.println(LocalDate.from(formatter.parse("57249"))); // Output: 2015-08-15

这篇关于将Julian日期转换为Instant的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆