Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Remove time part from java.util.Date()

 
Mahesh Kedari
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15495
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use the Calendar class, which is much more flexible than the Date class:

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic