We are storing date and datetime as Date object.
But, late on, we need to be able tell whether the Date object is a date or datetime.
05/18/05 00:00:00 and 05/18:05 both have the hour / minute portion. I can't really tell them apart.
Java provides the Date class available in java. util package, this class encapsulates the current date and time.
The getTime() method of Java Date class returns the number of milliseconds since January 1, 1970, 00:00:00 GTM which is represented by Date object. Parameters: The function does not accept any parameter. Return Value: It returns the number of milliseconds since January 1, 1970, 00:00:00 GTM.
We can use the simple isBefore , isAfter and isEqual to check if a date is within a certain date range; for example, the below program check if a LocalDate is within the January of 2020. startDate : 2020-01-01 endDate : 2020-01-31 testDate : 2020-01-01 testDate is within the date range.
util. Date objects do not contain any timezone information by themselves - you cannot set the timezone on a Date object. The only thing that a Date object contains is a number of milliseconds since the "epoch" - 1 January 1970, 00:00:00 UTC.
Unless your datetimes are guaranteed to not be at midnight, ie they never have HH:mm:ss
of 00:00:00
, there is no solid way to tell "dates" and datetimes that happen to be at midnight apart.
If your datetimes are never at midnight, then:
if (new SimpleDateFormat("HH:mm:ss").format(object).equals("00:00:00"))
would do to determine if the Date
object is a "date".
I think your design is flawed and you should find another solution. I also think that the name of the Date
class is flawed and causes this kind of confusion.
A java.util.Date is, in fact, a point in time, not a date so there is no way to use it as a date only. You will need to come up with a different approach.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With