<bdo id='8ITre'></bdo><ul id='8ITre'></ul>
    1. <tfoot id='8ITre'></tfoot>

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

    2. <legend id='8ITre'><style id='8ITre'><dir id='8ITre'><q id='8ITre'></q></dir></style></legend>
    3. <small id='8ITre'></small><noframes id='8ITre'>

        我应该在 Java 8 中使用哪个日期类?

        时间:2023-07-25

        <tfoot id='c57Zt'></tfoot>
        • <small id='c57Zt'></small><noframes id='c57Zt'>

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

                  本文介绍了我应该在 Java 8 中使用哪个日期类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  Java 8 中有一整套日期类:

                  There is a whole set of date's classes in Java 8:

                  • java.time.LocalDateTime;
                  • java.time.ZonedDateTime;
                  • java.time.即时;
                  • java.time.偏移日期时间;
                  • java.sql.时间戳;
                  • java.util.日期.
                  • java.time.LocalDateTime;
                  • java.time.ZonedDateTime;
                  • java.time.Instant;
                  • java.time.OffsetDateTime;
                  • java.sql.Timestamp;
                  • java.util.Date.

                  我已经通过了他们的 JavaDocs 并注意到所有这些类都包含我需要的所有方法.因此,目前,我可以随机选择它们.但我想有 6 个单独的类并且每个类都专用于特定目的是有某种原因的.

                  I already passed over their JavaDocs and paid attention that all these classes contain all the methods I need. Thus, for the moment, I can select them randomly. But I guess that there is some reason why there are 6 separate classes and each of them is dedicated to the specific purpose.

                  技术信息和要求:

                  1. 输入是 String,它被转换为这些日期格式之一.
                  2. 我不需要显示时区,但是当我比较两个日期时,能够正确比较纽约和巴黎的时间很重要.
                  3. 精确的级别是秒,不需要使用毫秒.
                  4. 所需的操作:
                    • 查找最大/最小日期;
                    • 按日期对对象进行排序;
                    • 计算日期和时间段(两个日期之间的差异);
                    • 将对象插入 MongoDB 并按日期从数据库中检索它们(例如,特定日期之后的所有对象).
                  1. The input is in String, which is converted to one of these date formats.
                  2. I don't need to display the time zones but when I compare two dates it's important to be capable to compare correctly the time in New York and in Paris.
                  3. The precise level is seconds, there is no need to use milliseconds.
                  4. The required operations:
                    • find max/min date;
                    • sort objects by date;
                    • calculate date & time period (difference between two dates);
                    • insert objects to MongoDB and retrieve them from a db by date (e.g. all objects after specific date).

                  我的问题:

                  1. 我应该牢记哪些方面才能从性能和性能的这四个选项中选择最佳格式可维护性的观点?
                  2. 我有什么理由不应该避免这些日期课程吗?

                  推荐答案

                  每个 Date 类都有特定用途:

                  Each one of the Date classes are for specific purposes:

                  • 如果您想在 SQL/JDBC 上下文中使用 Date,请使用 java.sql.Timestamp.

                  • If you want to use your Date in an SQL/JDBC context, use the java.sql.Timestamp.

                  java.util.Date 是旧的 Java API,它不是线程安全的,你很难处理时间分区,而且最重要的是,它的设计很糟糕:一简单的统一是月份从 1 开始,而日子从 0 开始.

                  java.util.Date is the old Java API, it is not thread safe, you can difficultly handle time zoning, and on the top of all, it is poorly designed: one simple uniformity is that months start from 1 while days start from 0.

                  java.time.LocalDateTime 是一个不可变的日期时间对象,表示日期时间,通常被视为年-月-日-小时-分钟-秒,您需要没错.

                  java.time.LocalDateTime is an immutable date-time object that represents a date-time, often viewed as year-month-day-hour-minute-second, which you need exactly.

                  java.time.ZonedDateTime 类存储所有日期和时间字段,因此您可以使用它来处理以下值:1990 年 1 月 27 日 15:40.30.123123123 +02:00,欧洲/巴黎时区.

                  java.time.ZonedDateTime class stores all date and time fields, so you can use it to deal with values like: 27th January 1990 at 15:40.30.123123123 +02:00 in the Europe/Paris time-zone.

                  为了完成您的任务,ZonedDateTime 类处理从 LocalDateTime 的本地时间线到 Instant 的即时时间线的转换(它模拟时间线上的单个瞬时点).由 ZoneOffset 表示的两条时间线之间的差异是与 UTC/格林威治的偏移量.

                  To do your task, the ZonedDateTime class handles conversion from the local time-line of LocalDateTime to the instant time-line of Instant(which models a single instantaneous point on the time-line). The difference between the two time-lines, represented by a ZoneOffset, is the offset from UTC/Greenwich.

                  计算持续时间和周期:有 java.time.Duration 是基于时间的时间量,例如20.5 秒"和 java.time.Period,这是一个基于日期的时间量(例如:26 年 2 个月 2 天).

                  To calculate duration and period: there is the java.time.Duration which is a time-based amount of time, such as '20.5 seconds', and java.time.Period, which is a date-based amount of time (like: 26 years, 2 months and 2 days).

                  要获得最大和最小日期,您可以使用 Java 8 lambdas,例如:

                  To get max and min dates, you can use the Java 8 lambdas in something like:

                  Date maxDate = list.stream().map(yourInstance -> yourInstance.date).max(Date::compareTo).get();
                  Date minDate = list.stream().map(yourInstance -> yourInstance.date).min(Date::compareTo).get();
                  

                  这篇关于我应该在 Java 8 中使用哪个日期类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:log4j 与 System.out.println - 记录器的优势? 下一篇:休眠强制时间戳以保持/加载为 UTC

                  相关文章

                  最新文章

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

                  1. <tfoot id='j3XCf'></tfoot>

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