Reputation: 4945
I need to remove time from a Date
Object. Here is my try,
Code:
System.out.println("date " + dbDate);
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
System.out.println("formatter.format(dbDate) " + formatter.format(dbDate));
System.out.println("final " + formatter.parse(formatter.format(dbDate)));
Output:
date 2011-12-03 23:59:59.0
formatter.format(dbDate) 2011-12-03
final Sat Dec 03 00:00:00 IST 2011
I want to the final date to display in 2011-12-03
. But after conversion toString()
of that Date
is in different format. I am missing something. Please help.
Update:
In my application, I have two different methods to get dbDate
. EXPIRY_DATE
column is type of DATE
.
First query uses dbDate = (java.util.Date) rs.getDate("EXPIRY_DATE");
.
For this dbDate
, System.out.println("date " + dbDate);
gives date 2011-12-03
Second query uses dbDate = rs.getTimestamp("EXPIRY_DATE");
For this dbDate
, System.out.println("date " + dbDate);
gives date 2011-12-03 23:59:59.0
.
This is my problem. As I thought toString()
was giving problem, I didn't mention the full problem.
Solution:
I did not have choices to avoid java.sql.Date
as my application methods have multiple usages.
I tried the below and worked,
dbDate = new java.sql.Date(dbDate.getTime());
Upvotes: 1
Views: 5584
Reputation: 340118
The Answer by BalusC is correct: You cannot eliminate a time-of-day from a class object defined to hold a date plus a time-of-day.
Also, you are using troublesome old classes (java.util.Date
and java.sql.Date
) that are now obsolete, supplanted by the java.time classes.
Instead, use a date-only class for a date-only value. The LocalDate
class represents a date-only value without time-of-day and without time zone. The java.sql.Date
pretends to do the same, but actually does carry a time of day due to very poor design decision of inheriting from java.util.Date
. Avoid java.sql.Date
, and use only java.time.LocalDate
instead.
You are starting with a java.util.Date
object apparently. That represents a point on the timeline in UTC with a resolution in milliseconds. So using that to determine a date requires a time zone. The LocalDate
class represents a date-only value without time-of-day and without time zone.
A time zone is crucial in determining a date. For any given moment, the date varies around the globe by zone. For example, a few minutes after midnight in Paris France is a new day while still “yesterday” in Montréal Québec.
If no time zone is specified, the JVM implicitly applies its current default time zone. That default may change at any moment, so your results may vary. Better to specify your desired/expected time zone explicitly as an argument.
Specify a proper time zone name in the format of continent/region
, such as America/Montreal
, Africa/Casablanca
, or Pacific/Auckland
. Never use the 3-4 letter abbreviation such as EST
or IST
as they are not true time zones, not standardized, and not even unique(!).
ZoneId z = ZoneId.of( "America/Montreal" ) ;
If you want to use the JVM’s current default time zone, ask for it and pass as an argument. If omitted, the JVM’s current default is applied implicitly. Better to be explicit, as the default may be changed at any moment during runtime by any code in any thread of any app within the JVM.
ZoneId z = ZoneId.systemDefault() ; // Get JVM’s current default time zone.
To get a date-only value from your java.util.Date
, first convert to its java.time replacement, Instant
. To convert back and forth, call new methods added to the old classes.
Instant instant = myJavaUtilDate.toInstant() ;
That value is in UTC by definition. Apply your desired time zone (ZoneId
) to generate a ZonedDateTime
.
ZonedDateTime zdt = instant.atZone( z ) ;
Finally, extract your desired LocalDate
object from ZonedDateTime
.
LocalDate ld = zdt.toLocalDate() ;
As of JDBC 4.2 and later, you can directly exchange java.time classes with your database. So no need to use the the java.sql
classes such as java.sql.Date
and java.sql.Timestamp
.
myPreparedStatement.setObject( … , ld ) ;
Retrieval.
LocalDate ld = myResultSet.getObject( … , LocalDate.class ) ;
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 Stack Overflow 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?
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.
Upvotes: 0
Reputation: 798
I have encountered similar problem for those who encounters the same problem as mine I write this entry:
The problem is the date value that is taken from database and passed to the web client is in format yyyy-mm-dd
but in the application for the first entry there is not database value so we create date object and passed the value to web client which gives us timestamp value. The value that will be passed to web client must be in date format so SimpleDateFormat
is not a good choice for me
So from this post ı understand the difference of java.sql.date
and java.util.date and then create first object as
Date date = new java.sql.Date(1430454600000L);
which gives yyyy-mm-dd
value for toString
method.
Upvotes: 0
Reputation: 1109635
I need to remove time from a Date Object
You can't. The java.util.Date
object contains both the date and time. Its toString()
is also in a fixed format. If you want to represent it without time to humans, then you need to convert it to a String
like as you already did. Or, if you intend to store it in the DB without the time (as the db
part in the variable name dbDate
suggests), then you need to convert it to java.sql.Date
.
preparedStatement.setDate(1, new java.sql.Date(dbDate.getTime()));
// ...
Update as per your update, the ResultSet#getDate()
returns an instance of java.sql.Date
, not java.util.Date
(but it is a subclass of java.util.Date
, that's why the unnecessary cast worked; please note that casting is not the same as converting, a real conversion would be new java.util.Date(dbDate.getTime())
). As you can read in the javadoc of the toString()
method of java.sql.Date
, it's indeed in yyyy-MM-dd
format.
So, your concrete problem is that you're confusing java.sql.Date
with java.util.Date
and that you're misgrasping the internal workings of java.util.Date
and been mislead by the toString()
method. Everything is working as intented.
Upvotes: 5
Reputation: 3025
When you last call formatter.parse()
you get back a Date
object; the concatenation then makes an implicit call to Date.toString()
: the format returned by this call is the default for the locale set in the JVM.
What you must understand is that the Date
object has no knowledge of the string representation, internally it's just an aggregate of inte
Upvotes: 0
Reputation: 27880
If what you want to do is remove the time part of the Date
object:
Calendar
to remove the time part of your Date
object. As pointed out in this question: Java Date cut off time information.If you only want to obtain a String
representation without the time part of the Date
object:
SimpleDateFormat.format()
. You can't make Date.toString()
return a different value, it will always use that pattern. Look at its source code.Upvotes: 1