如何在 Java 日期和儒略日数之间进行转换?

发布于 2024-10-31 23:12:54 字数 3247 浏览 3 评论 0原文

如何将 Java Date 转换为表示儒略日的 double
如何将儒略日数转换为 Java Date


public static double dateToJulian(Date date) {

    GregorianCalendar calendar = new GregorianCalendar();
    calendar.setTime(date);

    int year;
    int month;
    float day;
    int a;
    int b;
    double d;
    double frac;

    frac = (calendar.get(Calendar.HOUR_OF_DAY) / 0.000024 + calendar.get(Calendar.MINUTE) / 0.001440);

    b = 0;

    year = calendar.get(Calendar.YEAR);
    month = calendar.get(Calendar.MONTH) + 1;

    DecimalFormat ceroPlaces = new DecimalFormat("0");
    day = calendar.get(Calendar.DAY_OF_MONTH);
    day = Float.parseFloat(ceroPlaces.format(day) + "." + ceroPlaces.format(Math.round(frac)));

    if (month < 3) {
        year--;
        month += 12;
    }

    if (FuncionSoporte.compararFechas(calendar.getTime(), calendar.getGregorianChange()) > 0) {
        a = year / 100;
        b = 2 - a + a / 4;
    }

    d = Math.floor(365.25 * year) + Math.floor(30.6001 * (month + 1)) + day + 1720994.5 + b;

    return (d); 
} 

public static Date julianToDate(double jd) {

    double z, f, a, b, c, d, e, m, aux;
    Date date = new Date();
    jd += 0.5;
    z = Math.floor(jd);
    f = jd - z;

    if (z >= 2299161.0) {
      a = Math.floor((z - 1867216.25) / 36524.25);
      a = z + 1 + a - Math.floor(a / 4);
    } else {
      a = z;
    }

    b = a + 1524;
    c = Math.floor((b - 122.1) / 365.25);
    d = Math.floor(365.25 * c);
    e = Math.floor((b - d) / 30.6001);
    aux = b - d - Math.floor(30.6001 * e) + f;

    Calendar calendar = new GregorianCalendar();
    calendar.setTime(date);
    calendar.set(Calendar.DAY_OF_MONTH, (int) aux);
    aux = ((aux - calendar.get(Calendar.DAY_OF_MONTH)) * 24);
    calendar.set(Calendar.HOUR_OF_DAY, (int) aux);
    calendar.set(Calendar.MINUTE, (int) ((aux - calendar.get(Calendar.HOUR_OF_DAY)) * 60));

    if (e < 13.5) {
      m = e - 1;
    } else {
      m = e - 13;
    }
    // Se le resta uno al mes por el manejo de JAVA, donde los meses empiezan en 0.
    calendar.set(Calendar.MONTH, (int) m - 1);
    if (m > 2.5) {
      calendar.set(Calendar.YEAR, (int) (c - 4716));
    } else {
      calendar.set(Calendar.YEAR, (int) (c - 4715));
    }
    return calendar.getTime();
  }

编辑:添加了 FuncionSoporte.java 代码。

public class FuncionSoporte {

    /**
     * Compare 2 dates. If the first is after the second result will be positive, if the second is after then negative, 0 if they are equal.
     */
    public static int compararFechas(Date d1, Date d2) {

        Calendar c1 = new GregorianCalendar();
        c1.setTime(d1);
        Calendar c2 = new GregorianCalendar();
        c2.setTime(d2);

        if (c1.get(Calendar.YEAR) == c2.get(Calendar.YEAR)) {
            if (c1.get(Calendar.MONTH) == c2.get(Calendar.MONTH)) {
                return c1.get(Calendar.DAY_OF_MONTH) - c2.get(Calendar.DAY_OF_MONTH);
            } else {
                return c1.get(Calendar.MONTH) - c2.get(Calendar.MONTH);
            }
        } else {
            return c1.get(Calendar.YEAR) - c2.get(Calendar.YEAR);
        }
    }
}

How can a Java Date be converted into a double that represents a Julian day?
How can a Julian day number be converted into a Java Date?


public static double dateToJulian(Date date) {

    GregorianCalendar calendar = new GregorianCalendar();
    calendar.setTime(date);

    int year;
    int month;
    float day;
    int a;
    int b;
    double d;
    double frac;

    frac = (calendar.get(Calendar.HOUR_OF_DAY) / 0.000024 + calendar.get(Calendar.MINUTE) / 0.001440);

    b = 0;

    year = calendar.get(Calendar.YEAR);
    month = calendar.get(Calendar.MONTH) + 1;

    DecimalFormat ceroPlaces = new DecimalFormat("0");
    day = calendar.get(Calendar.DAY_OF_MONTH);
    day = Float.parseFloat(ceroPlaces.format(day) + "." + ceroPlaces.format(Math.round(frac)));

    if (month < 3) {
        year--;
        month += 12;
    }

    if (FuncionSoporte.compararFechas(calendar.getTime(), calendar.getGregorianChange()) > 0) {
        a = year / 100;
        b = 2 - a + a / 4;
    }

    d = Math.floor(365.25 * year) + Math.floor(30.6001 * (month + 1)) + day + 1720994.5 + b;

    return (d); 
} 

public static Date julianToDate(double jd) {

    double z, f, a, b, c, d, e, m, aux;
    Date date = new Date();
    jd += 0.5;
    z = Math.floor(jd);
    f = jd - z;

    if (z >= 2299161.0) {
      a = Math.floor((z - 1867216.25) / 36524.25);
      a = z + 1 + a - Math.floor(a / 4);
    } else {
      a = z;
    }

    b = a + 1524;
    c = Math.floor((b - 122.1) / 365.25);
    d = Math.floor(365.25 * c);
    e = Math.floor((b - d) / 30.6001);
    aux = b - d - Math.floor(30.6001 * e) + f;

    Calendar calendar = new GregorianCalendar();
    calendar.setTime(date);
    calendar.set(Calendar.DAY_OF_MONTH, (int) aux);
    aux = ((aux - calendar.get(Calendar.DAY_OF_MONTH)) * 24);
    calendar.set(Calendar.HOUR_OF_DAY, (int) aux);
    calendar.set(Calendar.MINUTE, (int) ((aux - calendar.get(Calendar.HOUR_OF_DAY)) * 60));

    if (e < 13.5) {
      m = e - 1;
    } else {
      m = e - 13;
    }
    // Se le resta uno al mes por el manejo de JAVA, donde los meses empiezan en 0.
    calendar.set(Calendar.MONTH, (int) m - 1);
    if (m > 2.5) {
      calendar.set(Calendar.YEAR, (int) (c - 4716));
    } else {
      calendar.set(Calendar.YEAR, (int) (c - 4715));
    }
    return calendar.getTime();
  }

EDIT: Added FuncionSoporte.java code.

public class FuncionSoporte {

    /**
     * Compare 2 dates. If the first is after the second result will be positive, if the second is after then negative, 0 if they are equal.
     */
    public static int compararFechas(Date d1, Date d2) {

        Calendar c1 = new GregorianCalendar();
        c1.setTime(d1);
        Calendar c2 = new GregorianCalendar();
        c2.setTime(d2);

        if (c1.get(Calendar.YEAR) == c2.get(Calendar.YEAR)) {
            if (c1.get(Calendar.MONTH) == c2.get(Calendar.MONTH)) {
                return c1.get(Calendar.DAY_OF_MONTH) - c2.get(Calendar.DAY_OF_MONTH);
            } else {
                return c1.get(Calendar.MONTH) - c2.get(Calendar.MONTH);
            }
        } else {
            return c1.get(Calendar.YEAR) - c2.get(Calendar.YEAR);
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文