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

2023-11-27

Java 怎样才能Date被转换成double代表儒略日?
如何将儒略日数转换为JavaDate?


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

None

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 Java 日期和儒略日数之间进行转换? [关闭] 的相关文章

随机推荐

  • 如何优雅地检查 Erlang 中的许多条件?

    因此 当用户发送注册帐户的请求时 他们会发送用户名 密码 电子邮件和其他信息 注册功能必须验证其所有数据 一个例子是 验证电子邮件未被使用 验证用户名未被使用 验证用户名是字母数字 验证所有字段的长度都超过 X 个字符 验证所有字段的长度都
  • 启动应用程序,在 C++ 中捕获 stdout 和 stderr

    如何启动应用程序并通过 stdout 或 stderr 捕获输出 我正在编写一个自动构建系统 我需要捕获输出进行分析 我想更新 svn 存储库并获取修订号 以便如果成功的话我可以移动 autobuild revNumber 中的文件 我还想
  • Ant - 仅复制文件而不复制目录

    我需要使用 Ant 脚本复制文件夹中除目录之外的所有文件 我使用下面的脚本来做到这一点
  • 当 单元格中的文本发生更改时,jQuery 事件侦听器?

    jQuery 有没有办法将监听器附加到 td 以便当单元格内的文本发生更改 由 JavaScript 而不是用户 时 会触发该事件 延长姆威的回答 这是一些代码 var td my table tr td eq 1 var tdHtml t
  • 是否有任何理由重写 Java 8 中枚举中的方法

    正如所指出的herelambda 提供了一种非常优雅的方式来指定各个枚举值的行为 在 Java 8 之前 我通常会将其实现为 enum Operator TIMES public int operate int n1 int n2 retu
  • SQLite - 对表进行排序

    我有一个 SQLlite 数据库 我想按字母顺序对表进行排序 我该怎么做 有没有一种方法可以仅使用 SQLite 对条目进行排序 或者我是否必须首先将表读入数组 对其进行排序 然后将其写入数据库 这是我的查询 从表中选择条目 其中 id 我
  • Kubernetes 资源版本太旧

    我正在开发一个为不同 k8s 资源创建监视的操作员 我时不时地可以在日志中看到以下异常 并且应用程序停止 是什么导致了这个问题 我该如何解决这个问题 io fabric8 kubernetes client KubernetesClient
  • 使用 os.walk 读取文件[重复]

    这个问题在这里已经有答案了 我正在尝试访问植根于主目录的子目录中的文件 为此 我正在使用os walk 我能够成功访问文件名并将其存储在列表中 但是 当我尝试使用打开这些文件时open filename r 我收到一条错误消息 告诉我不存在
  • 自动装配不适用于类 @Entity

    我有一堂课叫Menu 注释为 Entity我需要在名为的类中使用一个方法杰斯托 梅萨吉 Component Entity Table name menu public class Menu implements Serializable A
  • 通过 jQuery 添加的脚本标签在 FireBug 中不可见

    我正在添加
  • Spring data JPA 原生查询跳过锁定

    我想执行一个SKIP LOCKED使用 Spring Data JPA 对 Oracle 进行查询 所以我尝试了以下操作 Lock LockModeType PESSIMISTIC WRITE Query value SELECT FROM
  • 更新数据库 Android

    谁能告诉我如何更新android中的数据库 我创建了一个带有嵌入式数据库的应用程序 我更改了清单中数据库的版本并创建了更新方法 我想测试它以查看数据库是否正确更新 但是当我使用 adb 命令时 只有 r 允许我重新安装 但它会保留数据库 有
  • SignalR 2.0.2 创建持久连接

    我使用包管理器控制台将 SignalR 2 0 2 安装到我的 MVC 4 5 应用程序中 我做了连接配置的标准示例 namespace SignalRPersistent public class Startup public void
  • 如何在 C# 中使用 XMLREADER 从 XML 字符串读取特定元素

    我有 XML 字符串
  • 当我将 OS X 升级到 10.9 时,我的 applescript 不再工作

    以下代码尝试打开代理设置对话框 NSAppleScript a NSAppleScript alloc initWithSource tell application System Preferences nset current pane
  • jQuery - 垂直向上切换(即不是向下)

    我需要创建一个向上而不是向下动画的切换 换句话说 与 正常 切换相反 也许更简单的是 切换应该在菜单项 它是一个菜单 上方向上滑动以变得可见 而不是像普通的 SlideToggle 等那样向下滑动 我已经快到了 var opened fal
  • SYSMALLOC:断言失败 - 关于如何有效调试的任何想法?

    我的服务器守护进程在大多数机器上运行良好 但在我得到的一台机器上 malloc c 3074 sYSMALLOc Assertion old top mbinptr char av gt bins 1 1 2 builtin offseto
  • Java继承

    为什么最后打印的是 我是一个儿童班 public class Parent String parentString public Parent System out println Parent Constructor public Par
  • jQuery 无限函数执行

    我们想知道是否可以有一个使用 jQuery 的函数来检查多个元素 并根据通过单击分配给它们的类型来执行其他函数 基本上 这是一个将永远运行的函数 而用户不会刷新页面 这个想法不是依赖事件点击来执行功能 而是依赖分配给特定元素的类 例如 td
  • 如何在 Java 日期和儒略日数之间进行转换? [关闭]

    很难说出这里问的是什么 这个问题模棱两可 含糊不清 不完整 过于宽泛或言辞激烈 无法以目前的形式合理回答 如需帮助澄清此问题以便重新打开 访问帮助中心 Java 怎样才能Date被转换成double代表儒略日 如何将儒略日数转换为JavaD