how to reset the time in Date?

Hi there,

I wonder how can I reset the Time from A Date object?

When doing this:

Date d =new Date();

the result is given as:

Sat Feb 03 16:01:38 EST 2007

I would like get do this:

Sat Feb 03 00:00:00 EST 2007

thanks

[305 byte] By [xianwinwina] at [2007-11-26 20:20:50]
# 1
Use GregorianCalendar and its APIs. Date class is a 90% deprecated API.
hiwaa at 2007-7-10 0:45:42 > top of Java-index,Java Essentials,Java Programming...
# 2

To clarify what hiwa said: Don't avoid Date--it still has plenty of uses. BUT Date sould only be used to represent an instant in time (without any regard to human divisions of time like months, years, hours) and to compare to other Dates.

So you can start with a Date and then use a Calendar to work with the human overlay of days, hours etc. However, if you just want to start with "now," then I think GregorianCalendar has a constructor and/or factory method that will give you "now" so you can skip the Date altogether.

jverda at 2007-7-10 0:45:42 > top of Java-index,Java Essentials,Java Programming...
# 3

basically, I don't use date that much. but I got this problem where I have to compare to instances of dates. I need to compare them only in terms of the date (not the time) so I though if i could reset the time I will be able to do this:

dateA.before(DateB)

and get true only if the date is different (if its the same day and dateA is one sec before DateB - it will return true)

xianwinwina at 2007-7-10 0:45:42 > top of Java-index,Java Essentials,Java Programming...
# 4

> basically, I don't use date that much. but I got this

> problem where I have to compare to instances of

> dates. I need to compare them only in terms of the

> date (not the time) so I though if i could reset the

> time I will be able to do this:

>

> dateA.before(DateB)

>

> and get true only if the date is different (if its

> the same day and dateA is one sec before DateB - it

> will return true)

As you discovered, that method tells if one Date object represents an instant in time that comes before the instant in time represented by the other Date object.

As suggested: Put them into a Calendar, set the hours, minutes, seconds, milliseconds to zero, and then compare the two calendars (or the Calendars' Dates or longs if Calendar doesn't give a before() or somesuch method--I haven't looked).

jverda at 2007-7-10 0:45:42 > top of Java-index,Java Essentials,Java Programming...
# 5
yes, will do so!thanks for the tip.
xianwinwina at 2007-7-10 0:45:42 > top of Java-index,Java Essentials,Java Programming...