views:

62

answers:

4

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().

+3  A: 

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
+1. Java built in dates are bad. Take the 3 extra seconds to build around Joda time, and you will be much happier.
bwawok
I've heard of Jon Skeet's api, but is this android friendly?
Chris S
I believe it is completely android-friendly. (Jon Skeet has made a port of JodaTime to .NET (noda-time))
Bozho
I thought Joda time was his not Noda time, I'll give it a try
Chris S
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
which is the opposite?
Bozho
DateTime.MaxValue
Chris S
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
A: 

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
this doesn't answer his question.
Bozho
It's the right direction, but one of my main annoyances with the Sun/Oracle Java documentation is the lack of per-method examples, for example the Calendar.add() method
Chris S
@Bozho, as Chris has understood I was attempting to point him in the right direction for date manipulation with core Java.
Kevin D
well, fine. Just to note that the downvote is not mine.
Bozho
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
A: 

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();
    }
deepsat