"java: why is the date constructor deprecated, and what do i use instead?" Code Answer



localdate.of( 1985 , 1 , 1 )


localdate.of( 1985 , month.january , 1 )


the java.util.date, java.util.calendar, and java.text.simpledateformat classes were rushed too quickly when java first launched and evolved. the classes were not well designed or implemented. improvements were attempted, thus the deprecations you’ve found. unfortunately the attempts at improvement largely failed. you should avoid these classes altogether. they are supplanted in java 8 by new classes.

problems in your code

a java.util.date has both a date and a time portion. you ignored the time portion in your code. so the date class will take the beginning of the day as defined by your jvm’s default time zone and apply that time to the date object. so the results of your code will vary depending on which machine it runs or which time zone is set. probably not what you want.

if you want just the date, without the time portion, such as for a birth date, you may not want to use a date object. you may want to store just a string of the date, in iso 8601 format of yyyy-mm-dd. or use a localdate object from joda-time (see below).


first thing to learn in java: avoid the notoriously troublesome java.util.date & java.util.calendar classes bundled with java.

as correctly noted in the answer by user3277382, use either joda-time or the new java.time.* package in java 8.

example code in joda-time 2.3

datetimezone timezonenorway = datetimezone.forid( "europe/oslo" );
datetime birthdatetime_innorway = new datetime( 1985, 1, 1, 3, 2, 1, timezonenorway );

datetimezone timezonenewyork = datetimezone.forid( "america/new_york" );
datetime birthdatetime_innewyork = birthdatetime_innorway.todatetime( timezonenewyork ); 

datetime birthdatetime_utcgmt = birthdatetime_innorway.todatetime( datetimezone.utc );

localdate birthdate = new localdate( 1985, 1, 1 );

dump to console…

system.out.println( "birthdatetime_innorway: " + birthdatetime_innorway );
system.out.println( "birthdatetime_innewyork: " + birthdatetime_innewyork );
system.out.println( "birthdatetime_utcgmt: " + birthdatetime_utcgmt );
system.out.println( "birthdate: " + birthdate );

when run…

birthdatetime_innorway: 1985-01-01t03:02:01.000+01:00
birthdatetime_innewyork: 1984-12-31t21:02:01.000-05:00
birthdatetime_utcgmt: 1985-01-01t02:02:01.000z
birthdate: 1985-01-01


in this case the code for java.time is nearly identical to that of joda-time.

we get a time zone (zoneid), and construct a date-time object assigned to that time zone (zoneddatetime). then using the immutable objects pattern, we create new date-times based on the old object’s same instant (count of nanoseconds since epoch) but assigned other time zone. lastly we get a localdate which has no time-of-day nor time zone though notice the time zone applies when determining that date (a new day dawns earlier in oslo than in new york for example).

zoneid zoneid_norway = zoneid.of( "europe/oslo" );
zoneddatetime zdt_norway = zoneddatetime.of( 1985 , 1 , 1 , 3 , 2 , 1 , 0 , zoneid_norway );

zoneid zoneid_newyork = zonedid.of( "america/new_york" );
zoneddatetime zdt_newyork = zdt_norway.withzonesameinstant( zoneid_newyork );

zoneddatetime zdt_utc = zdt_norway.withzonesameinstant( zoneoffset.utc );  // or, next line is similar.
instant instant = zdt_norway.toinstant();  // instant is always in utc.

localdate localdate_norway = zdt_norway.tolocaldate();

about java.time

the java.time framework is built into java 8 and later. these classes supplant the troublesome old legacy date-time classes such as java.util.date, calendar, & simpledateformat.

the joda-time project, now in maintenance mode, advises migration to the java.time classes.

to learn more, see the oracle tutorial. and search for many examples and explanations. specification is jsr 310.

you may exchange java.time objects directly with your database. use a jdbc driver compliant with jdbc 4.2 or later. no need for strings, no need for java.sql.* classes.

where to obtain the java.time classes?

  • java se 8, java se 9, java se 10, and later
    • built-in.
    • part of the standard java api with a bundled implementation.
    • java 9 adds some minor features and fixes.
  • java se 6 and java se 7
    • much of the java.time functionality is back-ported to java 6 & 7 in threeten-backport.
  • android
    • later versions of android bundle implementations of the java.time classes.
    • for earlier android (<26), the threetenabp project adapts threeten-backport (mentioned above). see how to use threetenabp….

the threeten-extra project extends java.time with additional classes. this project is a proving ground for possible future additions to java.time. you may find some useful classes here such as interval, yearweek, yearquarter, and more.

By alexherm on March 28 2022

Answers related to “java: why is the date constructor deprecated, and what do i use instead?”

Only authorized users can answer the Search term. Please sign in first, or register a free account.