File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Date Formatting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Date Formatting" Watch "Date Formatting" New topic

Date Formatting

niranjan angadi

Joined: Jan 09, 2006
Posts: 3
Hi All,

I am getting Date column (DueDate) from database through hibernate. It will be of format "6/23/2005 12:02:30 PM". I need to format it to "yyyy-MM-dd" before displaying/setting it to DO object (as a date only).

I used SimpleDateFormat class to format date. But sdf.format(dueDate) returns string, i need date object back so as to set it to DO object. So i used sdf.parse(sdf.format(dueDate)) which returns date object, but now returned date will be not in my required format("yyyy-MM-dd"), it will be in "Tue Jun 12 00:00:00 IST 2007".

Could you help me out how to format the date in required format and get back as date object.

Or else, simply I have Date Time object and i want to truncate time part. Want to display only date (yyyy-MM-dd, 2007-06-12).
Joe Ess

Joined: Oct 29, 2001
Posts: 8843

You have to use two instances of SimpleDateFormat. One to parse, one to format.

"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
I agree. Here's the link:
subject: Date Formatting
Similar Threads
converting string yyyy/MM/dd to java.util.Date
Convert to timestamp yyyy-MM-dd hh:mm:ss a
Validate date and time in locat time.
date format issue. can you please tell
Date comparision in SQL from timestamp