12

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.

8 Answers 8

6

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.

Sign up to request clarification or add additional context in comments.

3 Comments

+1 for the reference to JodaTime. There's also the LocalDate class in JodaTime that represents just the date component. It's not a perfectly clean fit, but it has worked for my needs in the past.
The midnight-related classes and methods have been deprecated and should not be used. See the withTimeAtStartOfDay method. Also, the makers of Joda-Time have advised us to migrate to the java.time framework.
thanks, java.sql.Date was exactly what I needed. I was using java.utils.Date and the json serializer was messing things up.
5

The other answers are out-dated.

The old date-time classes are notoriously troublesome and should be avoided.

java.time

The java.time framework is built into Java 8 and later. See Oracle Tutorial. Much of the java.time functionality is back-ported to Jave 6 & 7 and further adapted to Android.

The LocalDate class represents a date-only value without time-of-day and without time zone.

LocalDate localDate = LocalDate.of( 2012 , 12 , 7 );

Textual representation

The toString method generates a String using standard ISO 8601 format.

localDate.toString() → 2012-12-07

For other formats use DateTimeFormatter.

SQL

JDBC drivers compliant with JDBC 4.2 can use the getObject and setObject methods to directly fetch/pass the java.time types such as LocalDate to SQL type DATE.

If your driver cannot do so, fall back to using the java.sql types. New methods have been added to the old classes to facilitate conversion.

java.sql.Date sqlDate = java.sql.Date.valueOf( localDate );

And the other direction.

LocalDate localDate = sqlDate.toLocalDate();

Search Stack Overflow

Search Stack Overflow for many more discussions and examples of using java.time. For example, my answer to this similar Question.

Comments

2

Date class. It holds a UNIX timestamp. In most databases, you can specify a field to hold a timestamp as well. You can use DateFormat to format the timestamp however you want.

Comments

2

If you are using Java 8, you can use java.time.LocalDate.

Comments

1

Use Date class that is available.

Comments

1

It shows how hold only date(without time)

formatter = new SimpleDateFormat("dd/MM/yyyy");

Date date= new Date();

Date todayWithOutTime =formatter.parse(formatter.format(date));

Comments

1

Unfortunatelly, java.util.Date is something, that in SQL have name Timestamp. There's no pure type for Date only, javax.sql.Date extends java.util.Date and using this type for date manipulation in Java gives you no advantage.

I'm dealing with this using apache commons-lang class DateUtils:

Date myDate = DateUtils.truncate(timestamp, Calendar.DAY_OF_MONTH);

This will remove the time part and leave only date part.

Comments

1

Use Date class. This is the better way.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.