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
Most date manipulation should be done using the Calendar object now.
http://download.oracle.com/javase/6/docs/api/java/util/Calendar.html
The de-facto Java datetime API is joda-time.
With it, you can get the current date/time by just constructing new DateTime()
.
Similarly, Without it, you can use Calendar.getInstance()
or new Date()
to obtain the current date/time.
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
.
+--------------------+----------------------------------------+----------------------------+
| .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:
new Date(Long.MIN_VALUE)
in JavaThe other Answers may be correct but use outmoded classes.
The old date-time classes (java.util.Date/.Calendar etc.) were supplanted by Joda-Time, which in turn has been supplanted by the java.time framework 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-Backport project, and adapted to Android in the ThreeTenABP project. See Tutorial.
To get the current moment on the timeline in UTC with a resolution of nanoseconds, use Instant.
Instant now = Instant.now();
Instant
has three constants:
1970-01-01T00:00:00Z
-1000000000-01-01T00:00Z
1000000000-12-31T23:59:59.999999999Z
To get the current moment for an offset-from-UTC, apply a ZoneOffset to get an 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.
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 localDate = LocalDate.now( zoneId );
LocalTime localTime = LocalTime.now( zoneId );
to get the current date:
Calendar calendar = Calendar.getInstance(); SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
try {
System.out.println("Today: " + dateFormat.format(calendar.getTime()));
} catch (Exception e) {
e.printStackTrace();
}
Theres calendar 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
Theres also simpledateformat for formatting. http://download.oracle.com/javase/1.4.2/docs/api/java/text/SimpleDateFormat.html