19

Which data type can I use in Java to hold the current date as well as time?. I want to store the datetime in a db as well as having a field in the java bean to hold that.

is it java.util.Date ?

Daniel Romero
  • 1,581
  • 1
  • 20
  • 33
akshay
  • 5,235
  • 14
  • 39
  • 49

8 Answers8

26

java.util.Date represents an instant in time, with no reference to a particular time zone or calendar system. It does hold both date and time though - it's basically a number of milliseconds since the Unix epoch.

Alternatively you can use java.util.Calendar which does know about both of those things.

Personally I would strongly recommend you use Joda Time which is a much richer date/time API. It allows you to express your data much more clearly, with types for "just dates", "just local times", "local date/time", "instant", "date/time with time zone" etc. Most of the types are also immutable, which is a huge benefit in terms of code clarity.

Gord Thompson
  • 116,920
  • 32
  • 215
  • 418
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
12
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

private String getDateTime() {
    DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
    Date date = new Date();
    return dateFormat.format(date);
}
Brad Mace
  • 27,194
  • 17
  • 102
  • 148
Rupok
  • 2,062
  • 16
  • 16
6

java.time

The java.time framework built into Java 8 and later supplants both the old date-time classes bundled with the earliest versions of Java and the Joda-Time library. The java.time classes have been back-ported to Java 6 & 7 and to Android.

The Instant class represents a moment on the timeline in UTC with a resolution of nanoseconds.

Instant instant = Instant.now();

Apply an offset-from-UTC (a number of hours and possible minutes and seconds) to get an OffsetDateTime.

ZoneOffset offset = ZoneOffset.of( "-04:00" );
OffsetDateTime odt = OffsetDateTime.ofInstant( instant , offset );

Better yet is applying a full time zone which is an offset plus a set of rules for handling anomalies such as Daylight Saving Time (DST).

ZoneId zoneId = ZoneId.of( "America/Montreal" );
ZonedDateTime zdt = ZonedDateTime.ofInstant( instant , zoneId );

Database

Hopefully the JDBC drivers will be updated to work directly with the java.time classes. Until then we must use the java.sql classes to move date-time values to/from the database. But limit your use of the java.sql classes to the chore of database transit. Do not use them for business logic. As part of the old date-time classes they are poorly designed, confusing, and troublesome.

Use new methods added to the old classes to convert to/from java.time. Look for to… and valueOf methods.

Use the java.sql.Timestamp class for date-time values.

java.sql.Timestamp ts = java.sql.Timestamp.valueOf( instant );

And going the other direction…

Instant instant = ts.toInstant();

For date-time data you virtually always want the TIMESTAMP WITH TIME ZONE data type rather than WITHOUT when designing your table columns in your database.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
3

+1 the recommendation for Joda-time. If you plan on doing anything more than a simple Hello World example, I suggest reading this:

Daylight saving time and time zone best practices

Community
  • 1
  • 1
nogridbag
  • 3,521
  • 4
  • 38
  • 51
1

I used this import:

import java.util.Date;

And declared my variable like this:

Date studentEnrollementDate;
Tim
  • 41,901
  • 18
  • 127
  • 145
dmiadan
  • 11
  • 3
1

Since Java 8, it seems like the java.time standard library is the way to go. From Joda time web page:

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.


Back to your question. Were you to use Java 8, I think you want LocalDateTime. Because it contains the date and time-of-the-day, but is unaware of time zone or any reference point in time such as the unix epoch.

Tarrasch
  • 10,199
  • 6
  • 41
  • 57
  • 1
    Right about using java.time but wrong about using `LocalDateTime`. Because it has no time zone or offset-from-UTC, it has no real meaning. It is not a moment on the timeline. Rarely needed in business apps. Instant, OffsetDateTime, and ZonedDateTime are all used to represent a moment on the timeline. – Basil Bourque Apr 12 '16 at 05:54
  • @BasilBourque Yes that's a good rephrasing. Though the question at hand wasn't really specific enough to say what is right or wrong. – Tarrasch Apr 12 '16 at 07:31
  • Agreed the Question is not clear, and rather anemic, lacking details. But it does say “current” date and time which almost certainly should be zoned. – Basil Bourque Apr 12 '16 at 15:59
1

Depends on the RDBMS or even the JDBC driver.

Most of the times you can use java.sql.Timestamp most of the times along with a prepared statement:

pstmt.setTimestamp( index, new Timestamp( yourJavaUtilDateInstance.getTime() );
OscarRyz
  • 196,001
  • 113
  • 385
  • 569
0

You can use Calendar.

     Calendar rightNow = Calendar.getInstance();

Calendar

Date4j alternative to Date, Calendar, and related Java classes

Srikanth Venkatesh
  • 2,812
  • 1
  • 20
  • 13