Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6846401
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:37:02+00:00 2026-05-27T00:37:02+00:00

I need to remove time from a Date Object. Here is my try, Code:

  • 0

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());
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T00:37:03+00:00Added an answer on May 27, 2026 at 12:37 am

    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.

    Related:

    • Handling MySQL datetimes and timestamps in Java
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello I need to remove dots and hyphens from both variables time and date.
Typically I need to remove time info from GETDATE() . I need this because
I need to remove time portion of date time or probably have the date
I need to remove duplicate rows from a fairly large SQL Server table (i.e.
I need to remove multiple items from a Dictionary. A simple way to do
I need to remove the following format from the end of a string in
I need to remove the following script from my page(ASP.NET 3.5), leaving all other
I need to remove these ' & ' characters from each property of a
I need to remove the maximize and minimize buttons from a JFrame . Please
I need to retrieve data from a specific time period. The query works fine

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.