aspose file tools*
The moose likes Java in General and the fly likes Remove time part from java.util.Date() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Remove time part from java.util.Date()" Watch "Remove time part from java.util.Date()" New topic
Author

Remove time part from java.util.Date()

Mahesh Kedari
Ranch Hand

Joined: Nov 28, 2009
Posts: 111
Hi,
I want to get only the date part of java.util.Date() resetting time to 00.

Let me make it more clear. When I am initializing the date object as

java.util.Date date = new java.util.Date();

I am getting the date object with value as "Thu May 27 12:13:49 IST 2010"
I just want to set the time part of this object to "00:00:00"
I dont want to get the output as String using DateFormat class.
The output should be a java.util.Date object only.

In java.util.Date, setHours, setMinutes and setSeconds functions are deprecated.
What alternative I can use instead of those function?

Can anyone please help me for this?

# Subject edited


Regards,
Mahesh Kedari - Fidus Technologies Ltd.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

Mahesh Kedari wrote:In java.util.Date, setHours, setMinutes and setSeconds functions are deprecated.
What alternative I can use instead of those function?

The answer is in the Date class API. Can you find it ?


[My Blog]
All roads lead to JavaRanch
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1760
    
    7

It might be helpful to realize that java.util.Date is essentially nothing more than a wrapper for a long value that represents the milliseconds that have passed since January 1st 1970.
Formatting that long value as a human readable String is no longer its responsibility. If you look at the documentation of some of its deprecated methods that used to offer this kind of behaviour (toGMTString() / toLocaleString()), you'll find that DateFormat should indeed be used for this purpose. What's your reason for not wanting to use the DateFormat class?

Edit: Oh, nevermind, I misunderstood your question.

Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14108
    
  16

You can use the Calendar class, which is much more flexible than the Date class:


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Remove time part from java.util.Date()