Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java data type to hold only date

Tags:

java

Which data type in Java can hold just the date and doesn't require a time component? For example, just to store 12/07/2012. I'm working with persisting the data to/from a database that has a date-only data type, so I'm looking for the best equivalent data type in Java.

like image 334
monda Avatar asked Jun 18 '12 13:06

monda


People also ask

What is the data type to store date in Java?

The Date in Java is not only a data type, like int or float, but a class. This means it has its own methods available for use. A Date in Java also includes the time, the year, the name of the day of the week, and the time zone. One of its methods includes extracting the time from the Date object.

Which type is used to store only the date?

For storing only date you need DATE data type.

Which data type can store only date and time?

The TIMESTAMP data type consists of a date and time, with optional time zone.


1 Answers

from the JDK: java.sql.Date:

A thin wrapper around a millisecond value that allows JDBC to identify this as an SQL DATE value. A milliseconds value represents the number of milliseconds that have passed since January 1, 1970 00:00:00.000 GMT.

To conform with the definition of SQL DATE, the millisecond values wrapped by a java.sql.Date instance must be 'normalized' by setting the hours, minutes, seconds, and milliseconds to zero in the particular time zone with which the instance is associated.

or from JodaTime: DateMidnight or LocalDate (thanks @cdeszaq)

DateMidnight defines a date where the time component is fixed at midnight. The class uses a time zone, thus midnight is local unless a UTC time zone is used.

It is important to emphasise that this class represents the time of midnight on any given day. Note that midnight is defined as 00:00, which is at the very start of a day.

like image 79
Sean Patrick Floyd Avatar answered Oct 21 '22 02:10

Sean Patrick Floyd