I am using Java 7 to develop a conference scheduling application.
I have seen that there are several data types to handle dates.
Example:
private Date creationDate;
private DateTime creationDate;
private Calendar creationDate;
My question is: What is the difference between type Calendar
, Date
and DateTime
?
Type objects
Date
in Java are used to represent a specific instant in time. It also allows converting and parsing dates however the methods that provided this are now deprecated .Taking its place is the class
Calendar
for date arithmetic and conversions, this class also provides the ability to work withLocales
and represent dates in different languages or specific calendar types.Converting objects
Date
toCalendar
is simple:Once you have an instance of
Calendar
you can get information about the date in the following way:One thing to keep in mind is that the months are considered integers from 0 to 11. In the documentation you can find all the available fields that you can consult.
If you have an instance of
Calendar
and want to convert to date:For parsing and formatting dates, it is recommended to use the class
DateFormat
or a subclass such asSimpleDateFormat
, for example:My suggestion is that you store your dates as objects
Date
because they are easier objects to generate and if you need any conversions or operations like adding 5 months to a date useCalendar
then return to theDate
.Lastly
DateTime
is not a data type in Java SE, it exists as part of a library called Joda Time , and it offers a variety of additional methods for operating on dates.From the documentation of
java.util.Date
(translated):While
java.util.Calendar
(translated, emphasis mine):The biggest problem here in the design of the class
Date
is that it is mutable, when it should have been immutable and the operation on dates such as increasing (or decreasing) days, hours, seconds, years, etc, and the generation ofDate
s should be throughCalendar
. At least this is the way I usually work to avoid problems handling theDate
. In Java 8 this was solved by implementing a new internal framework for handling dates and times called Java 8 Date and Time and the classes can be found under the packagejava.time
(your question is based on Java 7, so I only mention this part of Java 8, I will not delve further into this point).To use
Calendar
, I recommend you always initialize it as follows:Since there is more than one implementation of
Calendar
, with the best known (and used)GregorianCalendar
being , but there are alsoBuddhistCalendar
andJapaneseImperialCalendar
(at least from Open JDK code). The methodCalendar#getInstance
delegates the creation of the calendar to the methodcreateCalendar
which for Java 8 is implemented as follows (reviewing the HotSpot source code):I always recommend working with
Calendar
because it is considered good practice to work with abstract classes and interfaces whenever possible over working with the implementation directly. This is covered here: https://stackoverflow.com/q/383947/1065197I don't know of any class
DateTime
declared in JDK 7. Perhaps you are referring to the Joda Timeorg.joda.time.DateTime
library class (curious name in Spanish), which emerged as a solution to avoid working with the fact that it is mutable. is immutable and operations you perform on a will actually create a new instance of instead of modifying the current instance.Date
DateTime
DateTime
DateTime
If you want to know which class you should use in your projects, I would give you the following recommendations:
Date
(make sure they are from the packagejava.util
).Date
how to add or remove time, there are 2 options:Calendar
, set its time from an instance ofDate
viaCalendar#setTime
, perform the necessary operations, and get theDate
ofCalendar
viaCalendar#getTime
.DateTime
de Joda.SimpleDateFormat#format(Date)
(eye, receiveDate
and notCalendar
) and avoid using theDate#getXyz
. You could also useCalendar#get(Calendar.<campo>)
but the code would be very verbose (I don't know a translation of this).Why not directly declare the use of
DateTime
in the fields of your entities? Well, because there are frameworks that do not support direct conversion unless you implement particular converters for it. Some examples: Hibernate 1 , JSF, Spring, etc.1 Hibernate 5 now offers support for the Java 8 Date Time library. I guess the frameworks will slowly go that way too at some point.