Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to tell if the java.util.Date object contains the time portion?

Tags:

java

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.

like image 873
thinkanotherone Avatar asked Feb 17 '12 19:02

thinkanotherone


People also ask

Does Java Util date contain time?

Java provides the Date class available in java. util package, this class encapsulates the current date and time.

How do you find the time from a date object?

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.

How do you check if a date is within a date range in Java?

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.

What time-zone is Java Util date in?

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.


2 Answers

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.

like image 102
Bohemian Avatar answered Oct 14 '22 00:10

Bohemian


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.

like image 32
James Scriven Avatar answered Oct 13 '22 23:10

James Scriven