Java 等效于 .NET DateTime.MinValue、DateTime.Today

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4006186/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 04:20:11  来源:igfitidea点击:

Java equivalent of .NET DateTime.MinValue, DateTime.Today

javadatetimedate

提问by Chris S

Is there a Java equivalent of DateTime.MinValue and DateTime.Today in the Java Date class? Or a way of achieving something similar?

Java Date 类中是否有 DateTime.MinValue 和 DateTime.Today 的 Java 等价物?或者一种实现类似目标的方法?

I've realised how spoilt you are with the .NET datetime class, I also need the equivalent of AddDays(), AddMonths().

我已经意识到您对 .NET datetime 类的宠坏了,我还需要等效的 AddDays()、AddMonths()。

回答by Bozho

The de-facto Java datetime API is joda-time.

事实上的 Java 日期时间API 是joda-time

With it, you can get the current date/time by just constructing new DateTime().

有了它,您只需构建new DateTime().

Similarly, Without it, you can use Calendar.getInstance()or new Date()to obtain the current date/time.

同样,没有它,您可以使用Calendar.getInstance()new Date()获取当前日期/时间。

MinValuecan be Calendar.getInstance(0)/ new Date(0). This would use the default chronology - i.e. since January 1st, 1970. Since MinValuereturns Januar 1st, year 1, you can do that be simply specifying this date, using the appropriate constructor of DateTime.

MinValue可以是Calendar.getInstance(0)/ new Date(0)。这将使用默认年表 - 即自 1970 年 1 月 1 日以来。由于MinValue返回1 月 1 日,第 1 年,您可以使用DateTime.

回答by CJBS

Comparison of Date/Time features in .NET and Java

.NET 和 Java 中日期/时间特性的比较

+--------------------+----------------------------------------+----------------------------+
| .NET DateTime (C#) | Joda  DateTime (Java) [See Note #2]    | Java Date                  |
+--------------------+----------------------------------------+----------------------------+
|                    |                                        |                            |
| DateTime.MinValue  | new DateTime(Long.MIN_VALUE)           | new Date(Long.MIN_VALUE)   |
|                    |                                        |    [See Note #3]           |
|                    |                                        |                            |
| DateTime.Today     | new DateTime().withTimeAtStartOfDay()  | Messy [See Note #4]        |
|                    |                                        |                            |
| DateTime.Now       | new DateTime()                         | new Date()                 |
|                    |                                        |                            |
| DateTime.MaxValue  | new DateTime(Long.MAX_VALUE)           | new Date(Long.MAX_VALUE)   |
|                    |                                        |                            |
+--------------------+----------------------------------------+----------------------------+

Additional notes:

补充笔记:

  1. Dealing with dates and times is messy. This table is intended to be a starting point for code migrations. The comparisons compare concepts, not exact values (e.g. .NET's minimum date/time is not the same value as Java's)
  2. Joda DateTimeis the preferred date/time library for Java.
  3. See additional noteson new Date(Long.MIN_VALUE)in Java
  4. Getting start of day with Java's Date is a little more involved - see here.
  5. .NET DateTimes default to local date/time, whereas in Java they default to UTC. Make sure to consider any impact of timezoneswhen working with dates and times.
  1. 处理日期和时间很麻烦。此表旨在作为代码迁移的起点。比较比较的是概念,而不是精确值(例如 .NET 的最小日期/时间与 Java 的值不同)
  2. Joda DateTime是 Java 的首选日期/时间库。
  3. 补充说明new Date(Long.MIN_VALUE)Java中
  4. 使用 Java 的 Date 开始新的一天有点复杂 -请参阅此处
  5. .NET DateTimes 默认为本地日期/时间,而在 Java 中它们默认为 UTC。在处理日期和时间,请务必考虑时区的任何影响

回答by Basil Bourque

The other Answers may be correct but use outmoded classes.

其他答案可能是正确的,但使用过时的课程。

java.time

时间

The old date-time classes (java.util.Date/.Calendar etc.) were supplanted by Joda-Time, which in turn has been supplanted by the java.timeframework built into Java 8 and later. The java.time classes are inspired by Joda-Time, defined by JSR 310, extended by the ThreeTen-Extra project, back-ported to Java 6 & 7 by the ThreeTen-Backportproject, and adapted to Android in the ThreeTenABPproject. See Tutorial.

旧的日期时间类(java.util.Date/.Calendar 等)被Joda-Time取代,而Joda-Time又被Java 8 及更高版本中内置的java.time框架取代。的java.time类由启发约达时间,由下式定义JSR 310,由ThreeTen-额外项目,由后移植到Java 6和7延伸ThreeTen-反向移植项目,并在适合于的Android ThreeTenABP项目。请参阅教程

To get the current moment on the timeline in UTCwith a resolution of nanoseconds, use Instant.

要以纳秒的分辨率获取UTC时间轴上的当前时刻,请使用.Instant

Instant now = Instant.now();

Instanthas three constants:

Instant有三个常数:

  • EPOCH1970-01-01T00:00:00Z
  • MIN-1000000000-01-01T00:00Z
  • MAX1000000000-12-31T23:59:59.999999999Z
  • EPOCH—— 1970-01-01T00:00:00Z
  • MIN—— -1000000000-01-01T00:00Z
  • MAX—— 1000000000-12-31T23:59:59.999999999Z

To get the current moment for an offset-from-UTC, apply a ZoneOffsetto get an OffsetDateTime.

要获取从 UTC 偏移的当前时刻,请应用 aZoneOffset以获取OffsetDateTime

OffsetDateTime now = OffsetDateTime.now( ZoneOffset.of( "-04:00" ) );

Better to apply a full time zone(offset plus rules for anomalies such as Daylight Saving Time) if known. Apply a ZoneIdto get a ZonedDateTime.

如果已知,最好应用完整时区(偏移量加上夏令时等异常规则)。申请 aZoneId以获得ZonedDateTime.

ZonedDateTime now = ZonedDateTime.now( ZoneId.of( "America/Montreal" ) );

You can perform arithmetic.

您可以进行算术运算。

ZonedDateTime dayLater = now.plusDays( 1 );
ZonedDateTime monthLater = now.plusMonths( 1 );

You can get the first moment of a day.

您可以获得一天中的第一个时刻。

ZoneId zoneId = ZoneId.of( "America/Montreal" );
ZonedDateTime tomorrowStart = now.toLocalDate().atStartOfDay( zoneId );  // Usually time-of-day of `00:00:00.0` but not always.

If you need only a date without time-of-day and without time zone, use LocalDate. Similarly, LocalTimefor time-only without date and without time zone. Usually better to stick with Instantand OffsetDateTime/ZonedDateTimeas the Local…types do not represent actual moments on the timeline (no offset or time zone means they are undefined).

如果您只需要一个没有时间和时区的日期,请使用LocalDate. 同样,LocalTime对于没有日期和时区的仅时间。通常最好坚持使用InstantOffsetDateTime/,ZonedDateTime因为Local…类型不代表时间线上的实际时刻(没有偏移或时区意味着它们是未定义的)。

LocalDate localDate = LocalDate.now( zoneId );
LocalTime localTime = LocalTime.now( zoneId );

回答by Kevin D

Most date manipulation should be done using the Calendar object now.

大多数日期操作现在应该使用 Calendar 对象完成。

http://download.oracle.com/javase/6/docs/api/java/util/Calendar.html

http://download.oracle.com/javase/6/docs/api/java/util/Calendar.html

回答by zerodin

to get the current date:

获取当前日期:

Calendar calendar = Calendar.getInstance(); SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");

日历日历 = Calendar.getInstance(); SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");

    try {

        System.out.println("Today: " + dateFormat.format(calendar.getTime()));

    } catch (Exception e) {
        e.printStackTrace();
    }