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().
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().
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
.
Most date manipulation should be done using the Calendar object now.
http://download.oracle.com/javase/6/docs/api/java/util/Calendar.html
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