<i id='gp4S4'><tr id='gp4S4'><dt id='gp4S4'><q id='gp4S4'><span id='gp4S4'><b id='gp4S4'><form id='gp4S4'><ins id='gp4S4'></ins><ul id='gp4S4'></ul><sub id='gp4S4'></sub></form><legend id='gp4S4'></legend><bdo id='gp4S4'><pre id='gp4S4'><center id='gp4S4'></center></pre></bdo></b><th id='gp4S4'></th></span></q></dt></tr></i><div id='gp4S4'><tfoot id='gp4S4'></tfoot><dl id='gp4S4'><fieldset id='gp4S4'></fieldset></dl></div>

  1. <tfoot id='gp4S4'></tfoot>
    <legend id='gp4S4'><style id='gp4S4'><dir id='gp4S4'><q id='gp4S4'></q></dir></style></legend>
      <bdo id='gp4S4'></bdo><ul id='gp4S4'></ul>

    1. <small id='gp4S4'></small><noframes id='gp4S4'>

    2. Java将double转换为日期格式

      时间:2023-07-27
      <i id='pTt2c'><tr id='pTt2c'><dt id='pTt2c'><q id='pTt2c'><span id='pTt2c'><b id='pTt2c'><form id='pTt2c'><ins id='pTt2c'></ins><ul id='pTt2c'></ul><sub id='pTt2c'></sub></form><legend id='pTt2c'></legend><bdo id='pTt2c'><pre id='pTt2c'><center id='pTt2c'></center></pre></bdo></b><th id='pTt2c'></th></span></q></dt></tr></i><div id='pTt2c'><tfoot id='pTt2c'></tfoot><dl id='pTt2c'><fieldset id='pTt2c'></fieldset></dl></div>
      • <bdo id='pTt2c'></bdo><ul id='pTt2c'></ul>

            <legend id='pTt2c'><style id='pTt2c'><dir id='pTt2c'><q id='pTt2c'></q></dir></style></legend>
              <tbody id='pTt2c'></tbody>

            <small id='pTt2c'></small><noframes id='pTt2c'>

            • <tfoot id='pTt2c'></tfoot>
              1. 本文介绍了Java将double转换为日期格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我在 ios+firebase 中做了一个小应用程序,现在我正在尝试连接 android.在 ios 中,我将日期保存为双精度(例如:-242528463.775282),但随后我尝试在 java 中检索相同的双精度,它给了我另一个日期.

                I have made a little app in ios+firebase, now I am trying to connect android. In ios I save date as double (for example: -242528463.775282), but then I trying to retrieve same double in java it giving me another date.

                在 IOS - 01.07.2009在 Java 中 - 29.12.1969

                in IOS - 01.07.2009 in Java - 29.12.1969

                  double myDouble = date;
                  long myLong = (long) (myDouble);
                  System.out.println(myLong);
                
                  Date itemDate = new Date(itemLong);
                  String myDateStr = new SimpleDateFormat("dd-MM-yyyy").format(itemDate);             
                  editTextDate.setText(myDateStr);
                

                是否可以在不转换为 long 的情况下将 double 转换为 date?

                Is it possible to convert double to date without converting to long?

                推荐答案

                由于你的 double 代表你从现在开始日期的秒数,而 Date 构造函数在Java 预计自 01-01-1970 以来的毫秒数,您必须乘以您的数字以获得毫秒数 (* 1000),然后从 01-01 以来的当前毫秒数中减去该数-1970(System.currentTimeMillis()):

                Since your double represents the number of seconds of you date from now, and the Date constructor in Java is expecting a number of milliseconds since 01-01-1970, you have to multiply your number to get a number of milliseconds (* 1000) and substract that from the current number of milliseconds since 01-01-1970 (System.currentTimeMillis()):

                double myDouble = -242528463.775282;
                long myLong = System.currentTimeMillis() + ((long) (myDouble * 1000));
                System.out.println(myLong);
                
                Date itemDate = new Date(myLong);
                String myDateStr = new SimpleDateFormat("dd-MM-yyyy").format(itemDate);
                System.out.println(myDateStr);
                

                但是,您存储日期的方式的问题是,如果您今天和明天调用此代码,它将不会返回相同的日期,因为当前时间正在改变.您应该使用 timeIntervalSince1970 而不是 timeIntervalSinceNow.

                But, the problem with the way you store your dates is that if you are calling this code today and tomorrow it will not return the same date, as the current time is changing. You should use timeIntervalSince1970 instead of timeIntervalSinceNow.

                这篇关于Java将double转换为日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:从 double 到 int 的可能有损转换 下一篇:转换列表&lt;字符串&gt;Java中的字符串[]

                相关文章

                最新文章

              2. <tfoot id='AHS6L'></tfoot>

                • <bdo id='AHS6L'></bdo><ul id='AHS6L'></ul>

                <i id='AHS6L'><tr id='AHS6L'><dt id='AHS6L'><q id='AHS6L'><span id='AHS6L'><b id='AHS6L'><form id='AHS6L'><ins id='AHS6L'></ins><ul id='AHS6L'></ul><sub id='AHS6L'></sub></form><legend id='AHS6L'></legend><bdo id='AHS6L'><pre id='AHS6L'><center id='AHS6L'></center></pre></bdo></b><th id='AHS6L'></th></span></q></dt></tr></i><div id='AHS6L'><tfoot id='AHS6L'></tfoot><dl id='AHS6L'><fieldset id='AHS6L'></fieldset></dl></div>
                <legend id='AHS6L'><style id='AHS6L'><dir id='AHS6L'><q id='AHS6L'></q></dir></style></legend>

                  1. <small id='AHS6L'></small><noframes id='AHS6L'>