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

后端 未结 6 1263
梦毁少年i
梦毁少年i 2021-01-17 08:19

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

I\'ve realised how spoilt you are

6条回答
  •  耶瑟儿~
    2021-01-17 09:00

    Comparison of Date/Time features in .NET and 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 DateTime is the preferred date/time library for Java.
    3. See additional notes on 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 timezones when working with dates and times.

提交回复
热议问题