21

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 with the .NET datetime class, I also need the equivalent of AddDays(), AddMonths().

CJBS
  • 15,147
  • 6
  • 86
  • 135
Chris S
  • 64,770
  • 52
  • 221
  • 239
  • See also, [Java: What/where are the maximum and minimum values of a GregorianCalendar?](http://stackoverflow.com/q/2393016/642706). – Basil Bourque Apr 07 '16 at 22:39

6 Answers6

23

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.

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
  • +1. Java built in dates are bad. Take the 3 extra seconds to build around Joda time, and you will be much happier. – bwawok Oct 23 '10 at 21:47
  • I've heard of Jon Skeet's api, but is this android friendly? – Chris S Oct 23 '10 at 21:51
  • I believe it is completely android-friendly. (Jon Skeet has made a port of JodaTime to .NET (noda-time)) – Bozho Oct 23 '10 at 21:53
  • I thought Joda time was his not Noda time, I'll give it a try – Chris S Oct 23 '10 at 22:03
  • I could just use the epoch as the min value (I'm looking for something to indicate no date has been set), I'm not so sure about the opposite though – Chris S Oct 23 '10 at 22:14
  • For no date set you could use a date of "null". Means you have to null check it more, but null is a better description for no date set, then some random date (-764 B.C. or such ) – bwawok Oct 24 '10 at 02:20
  • From [Joda-Time's web-site](https://www.joda.org/joda-time/): **"Note that from Java SE 8 onwards, users are asked to migrate to java.time (JSR-310) - a core part of the JDK which replaces this project."** Also joda-time is limited to millisecond precision. – Ian Boyd Jul 15 '22 at 19:37
4

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.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:

  • EPOCH1970-01-01T00:00:00Z
  • MIN-1000000000-01-01T00:00Z
  • MAX1000000000-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 );
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
3

Comparison of Date/Time features in .NET and Java

.NET DateTime (C#) Joda DateTime (Java, deprecated) [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.

Joda-Time Deprecated

Joda-Time is deprecated. From their web-site archive:

Note that from Java SE 8 onwards, users are asked to migrate to java.time (JSR-310) - a core part of the JDK which replaces this project.

Meaning that java.util.Dateorg.joda.time.DateTimejava.time.ZonedDateTime

Giving us new mappings:

.NET DateTime (C#) java.time (formerly known as Joda-Time) Java Date
DateTime.MinValue LocalDateTime.MIN new Date(Long.MIN_VALUE)
DateTime.Today LocalDateTime.of(LocalDate.NOW, LocalTime.MIDNIGHT) (not available)
DateTime.Now LocalDateTime.now() new Date()
DateTime.UtcNow LocalDateTime.now(ZoneOffset.UTC) (not available)
DateTime.MaxValue LocalDateTime.MAX new Date(Long.MAX_VALUE)

The .NET DateTime contains:

  • date
  • time
  • and no particular information about what timezone it contains (except to day "Local", "UTC", or "Unspecified").

This means the java.time equivalent is LocalDateTime. If you want a datetime object that also carries with it an offset from UTC:

  • .NET: DateTimeOffset
  • java.time: ZonedDateTime
Ian Boyd
  • 246,734
  • 253
  • 869
  • 1,219
CJBS
  • 15,147
  • 6
  • 86
  • 135
-2

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

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

Kevin D
  • 3,564
  • 1
  • 21
  • 38
  • 2
    this doesn't answer his question. – Bozho Oct 23 '10 at 21:43
  • @Bozho, as Chris has understood I was attempting to point him in the right direction for date manipulation with core Java. – Kevin D Oct 24 '10 at 09:04
  • I didn't think it was Bozho, I've read enough of your answers that you don't strike me as that petty. I admit the answer could have had much more detail but I didn't really have time. – Kevin D Oct 24 '10 at 09:07
-2

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();
    }
zerodin
  • 857
  • 5
  • 9