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
Java equivalent of .NET DateTime.MinValue, DateTime.Today
提问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.
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()
获取当前日期/时间。
MinValue
can be Calendar.getInstance(0)
/ new Date(0)
. This would use the default chronology - i.e. since January 1st, 1970. Since MinValue
returns 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:
补充笔记:
- 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)
- Joda DateTimeis the preferred date/time library for Java.
- See additional noteson
new Date(Long.MIN_VALUE)
in Java - Getting start of day with Java's Date is a little more involved - see here.
- .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.
回答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();
Instant
has three constants:
Instant
有三个常数:
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 ZoneOffset
to 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 ZoneId
to 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, LocalTime
for time-only without date and without time zone. Usually better to stick with Instant
and OffsetDateTime
/ZonedDateTime
as the Local…
types do not represent actual moments on the timeline (no offset or time zone means they are undefined).
如果您只需要一个没有时间和时区的日期,请使用LocalDate
. 同样,LocalTime
对于没有日期和时区的仅时间。通常最好坚持使用Instant
和OffsetDateTime
/,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();
}
回答by Matt
Theres calendar http://download.oracle.com/javase/1.5.0/docs/api/java/util/Calendar.html
有日历 http://download.oracle.com/javase/1.5.0/docs/api/java/util/Calendar.html
And date http://download.oracle.com/javase/1.4.2/docs/api/java/util/Date.html
和日期 http://download.oracle.com/javase/1.4.2/docs/api/java/util/Date.html
Theres also simpledateformat for formatting. http://download.oracle.com/javase/1.4.2/docs/api/java/text/SimpleDateFormat.html
还有用于格式化的 simpledateformat。 http://download.oracle.com/javase/1.4.2/docs/api/java/text/SimpleDateFormat.html