Reputation: 2917
What can I use to replace this, new Date(2009, 12, 9)
?
Thanks for your help.
Upvotes: 29
Views: 49576
Reputation: 79580
The java.util
Date-Time API and their formatting API, SimpleDateFormat
are outdated and error-prone. It is recommended to stop using them completely and switch to the modern Date-Time API*.
Also, quoted below is a notice at the Home Page of 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.
Solution using java.time
, the modern API:
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.ZonedDateTime;
public class Main {
public static void main(String[] args) {
ZonedDateTime zdt = LocalDate.of(2009, 12, 9).atStartOfDay(ZoneId.of("Etc/GMT"));
System.out.println(zdt);
}
}
Output:
2009-12-09T00:00Z[Etc/GMT]
The Z
in the output is the timezone designator for zero-timezone offset. It stands for Zulu and specifies the Etc/UTC
timezone (which has the timezone offset of +00:00
hours).
For any reason, if you need to convert this object of ZonedDateTime
to an object of java.util.Date
, you can do so as follows:
Instant instant = zdt.toInstant();
Date date = Date.from(instant);
Like java.util.Date
, an Instant
represents an instantaneous point on the timeline in UTC, but unlike java.util.Date
, which does not represent a real Date-Time object (a java.util.Date
object simply represents the number of milliseconds since the standard base time known as "the epoch", namely January 1, 1970, 00:00:00 GMT
), it represents a real Date-Time object.
Learn more about java.time
, the modern Date-Time API* from Trail: Date Time.
* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.
Upvotes: 3
Reputation: 5173
GregorianCalendar(year + 1900, month, date)
https://docs.oracle.com/javase/1.5.0/docs/api/java/util/Date.html
Upvotes: 2
Reputation: 1503479
Note: this answer was written in 2009. Since then, java.time
has become the preferred date/time API in Java.
Ideally, use Joda Time instead. It's an infinitely superior API to the built-in one. You'd then want to choose between LocalDateTime
and DateTime
depending on your exact requirements (it's a complicated area - I'm not going to try to summarise in a sentence or two, but the docs do a good job).
If absolutely necessary, use a java.util.Calendar and convert that to a Date
when you need to.
Upvotes: 26
Reputation: 17639
If you look at the Javadoc it points you towards using Calendar.
As of JDK version 1.1, replaced by Calendar.set(year + 1900, month, date, hrs, min) or GregorianCalendar(year + 1900, month, date, hrs, min).
If you look at the Date
constructor params you'll see why it was deprecated:
Parameters:
year - the year minus 1900. month - the month between 0-11. date - the day of the month between 1-31. hrs - the hours between 0-23. min - the minutes between 0-59.
year
isn't what you expect and neither is month
.
To represent the date you have mentioned you need to call Date
like this (not recommended)
new Date(2009-1900, 12-1, 9)
The alternative using Calendar
is
Calendar cal = Calendar.getInstance();
cal.set(2009, 11, 9); //year is as expected, month is zero based, date is as expected
Date dt = cal.getTime();
Upvotes: 17
Reputation: 5314
Calendar cal = Calendar.getInstance();
cal.set(2009, Calendar.DECEMBER, 12);
Notice that i didn't put 12 for december because it's actually 11 (january is 0).
Then, you can add or remove seconds, minutes, hours, days, months or year easily with :
cal.add(Calendar.HOUR, 2);
cal.add(Calendar.MONTH, -5);
And finally, if you want a Date :
cal.getTime();
Upvotes: 25
Reputation: 915
Tim, in your comments you mentioned that you are doing this in a GXT context - i.e. in GWT client code. GWT does not support GregorianCalendar and you will most likely not be able to put JodaTime through the GWTCompiler (you may be able to, but do you really want to).
I think you are left really with the option to using JNSI if you want to do calendar operations in GWT. See the Date class in JavaScript.
Upvotes: 4
Reputation: 4439
Unfortunately date support in Java is completely awful. Officially, you'd probably have to do this with Calendar, but that shouldn't be necessary in my opinion. Like others have mentioned, Joda time is a lot better, but still not quite as easy to use as dates in Ruby on Rails.
I'm not aware of any Java package that gives you quite that amount of date support (Joda falls short a bit, but comes close), but in Groovy, using TimeCategory gives you very Ruby on Rails-like date support.
Upvotes: 2
Reputation: 199333
You can also use the SimpleDateFormat object:
import java.util.Date;
import java.text.SimpleDateFormat;
import java.text.ParseException;
public class DateTest {
public static void main( String [] args ) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy, MM, dd");
Date date = sdf.parse("2009, 12, 9");
System.out.println( date );
}
}
Upvotes: 8