• <bdo id='ODWch'></bdo><ul id='ODWch'></ul>
      <tfoot id='ODWch'></tfoot>

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

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

      2. 在 Java 中格式化时间戳

        时间:2023-07-27

          • <bdo id='CWw0b'></bdo><ul id='CWw0b'></ul>
              <tbody id='CWw0b'></tbody>

                <legend id='CWw0b'><style id='CWw0b'><dir id='CWw0b'><q id='CWw0b'></q></dir></style></legend>

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

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

                  本文介绍了在 Java 中格式化时间戳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我希望生成 yyyy-MM-dd HH:mm:ss 格式的当前时间戳.我编写了以下代码,但它总是给我这种格式 yyyy-MM-dd HH:mm:ss.x

                  I wish to produce a current timestamp in the format of yyyy-MM-dd HH:mm:ss. I have written up the following code, but it always gives me this format yyyy-MM-dd HH:mm:ss.x

                  你如何摆脱 .x 部分?

                  DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                  String currentTime = df.format(new Date());
                  Timestamp timestamp = Timestamp.valueOf(currentTime);
                  

                  我需要一个时间戳类型来写入 mysql.

                  I need to have a timestamp type for writing into mysql.

                  推荐答案

                  您可能正在查看 Timestamp 对象在您的数据库引擎或其通过使用 System.out.println 或其他方法在控制台中打印 Java 表示.请注意,真正存储的(在 Java 端或数据库引擎中)是一个数字,表示自纪元以来的时间(通常是 1970 年 1 月 1 日)和您想要/需要存储的日期.

                  Probably you're looking at the String representation the Timestamp object gives in your database engine or its Java representation by printing it in the console using System.out.println or by another method. Note that which is really stored (in both Java side or in your database engine) is a number that represents the time since epoch (usually January 1st 1970) and the date you want/need to store.

                  不应该使用您的Timestamp时注意它所代表的String格式.如果您应用相同的 SimpleDateFormat 来获得 timestamp 对象的 String 表示,则可以很容易地证明这一点:

                  You should not pay attention to the String format it is represented when you consume your Timestamp. This can be easily demostrated if you apply the same SimpleDateFormat to get a String representation of your timestamp object:

                  DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                  String currentTime = df.format(new Date());
                  Timestamp timestamp = Timestamp.valueOf(currentTime)
                  //will print without showing the .x part
                  String currentTimeFromTimestamp = df.format(currentTime);
                  

                  无论如何,如果您想要当前时间,只需直接从 new Date 的结果创建 Timestamp 即可:

                  Anyway, if you want the current time, just create the Timestamp directly from the result of new Date:

                  Timestamp timestamp = new Timestamp(new Date().getTime());
                  

                  这篇关于在 Java 中格式化时间戳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:我如何反序列化以杰克逊为单位的时间戳? 下一篇:设置由数据库生成的 JPA 时间戳列?

                  相关文章

                  最新文章

                • <tfoot id='Gequm'></tfoot>

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

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

                        <bdo id='Gequm'></bdo><ul id='Gequm'></ul>

                    1. <legend id='Gequm'><style id='Gequm'><dir id='Gequm'><q id='Gequm'></q></dir></style></legend>