简体   繁体   中英

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

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.

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.

使用Apache commons-lang-3:

DateUtils.truncate(fecha, Calendar.DAY_OF_MONTH).equals(fecha)

No need for parsing, formatting or 3rd parties libraries. You can easily do:

Calendar c = ...
c.setTime(date);
//this doesn't care about nanos
if(c.get(Calendar.HOUR_OF_DAY) + c.get(Calendar.MINUTE) + c.get(Calendar.SECOND) > 0) {
....
}

PD: I do this with 1000-25.000 objects and doesn't seem to jeopardize the performance, but of course will all depend on where and why you want to do this

You could possibly use Google's DateTime library for validation.

It has parseDateTime and parseDate methods that will throw NumberFormatException if they're not formatted correctly.

Then, you could do...

try {
   DateTime someVar = DateTime.parseDate("your_date_as_a_string");
} catch (java.lang.NumberFormatException nfe) {
  // This isn't a date...
}

try {
   DateTime someVar = DateTime.parseDateTime("your_date_as_a_string");
} catch (java.lang.NumberFormatException nfe) {
  // This isn't a date/time...
}

I understand that you're actually dealing with java.sql.Date and java.sql.Timestamp instances as obtained from a JDBC ResultSet while they are declared against their superclass java.util.Date , right?

In that case, just use instanceof .

if (date instanceof java.sql.Date) {
    // It was originally a DATE.
} else if (date instanceof java.sql.Timestamp) {
    // It was originally a TIMESTAMP (DATETIME).
}

Though I agree with the solutions posted here regarding modding the raw time value to see if it has a time portion, as Dmitri said, the flaw is that if the Date value has a time portion of exactly midnight, there's no way to tell.

So, thinking a bit laterally here, you could presumably create your own class that extends Date which has a member variable boolean called "isDateTime". Then, when you initially load in your date or datetime value, set that boolean to true or false depending on whether it's a datetime or not.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM