Bill F
Bill F

Reputation: 2087

Comparing a NotesDateTime in JAVA

Notes really does not like blank(null) dates and has caused me no end of grief. So in native Notes applications I have assigned all date fields a value of GMT 01/10/1970 0:0:0 AM

then at time to display the date if it equals the GMT time I display "-" as a place holder in multi-value fields. I'm want to create a little JAVA method that compares a date passed to the GMT date and if the same return true else return false. I think I'm OK with the code to the point that I have below, but now I'm not sure how to compare dt (converted to GMT ) to GMT the simple = does not appear to work so I suspect I have some kind of comparison conflict. Any pointer in the right direction appreciated. I googled this but could not find a direct answer.

public static Boolean isBlankDate(Date dt){
        Boolean rtn = false;
        Session s = ExtLibUtil.getCurrentSession();
        try{
            Date GMT = s.createDateTime("01/01/1970 0:0:0 AM GMT").toJavaDate();
        }catch(NotesException e){
            System.out.println("Date Create Error " + e.toString());
            if (dt = GMT){
                rtn = true;
            }else{
                rtn = false;
            }
            return false;
        }
        //compare dt to GMT and if equal return true else false
        return rtn;
    }

Upvotes: 2

Views: 615

Answers (4)

Knut Herrmann
Knut Herrmann

Reputation: 30960

You want to check if a native Notes data field contains 01/01/1970 0:0:0 AM GMT what you interpret as blank (null) data value. I assume you read the data value from document, convert the value to Java Date with .toJavaDate() and then want to test if it is 01/01/1970 0:0:0 AM GMT with your method isBlankDate(Date dt).

If that is what you want to achieve then you can reduce your method to just

public static boolean isBlankDate(Date dt) {
    return dt.getTime() == 0;
}

.getTime() returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by the Date object.

It returns 0 if Date is exactly 01/01/1970 0:0:0 AM GMT. So, the only thing you need to do is to test

dt.getTime() == 0

It returns true, if Date dt is 01/01/1970 0:0:0 AM GMT and returns false if not.

Upvotes: 2

The NTF
The NTF

Reputation: 111

"Notes really does not like blank(null) dates and has caused me no end of grief."

Actually that's not true. If you call both .setAnyTime() and .setAnyDate() you can still save as a DateTime but without any specific value.

Upvotes: 1

Paul Stephen Withers
Paul Stephen Withers

Reputation: 15729

If you're looking at comparing dates, isBefore / isAfter in java.util.Date class is useful. java.sql.Date allows you to create a Date as opposed to a DateTime.

java.util.Calendar is what I usually use for date manipulation / comparison in my own code. The Java APIs state that java.util.Date is deprecated in favour of it. You can pass in a java.util.Date using setTime. java.util.Calendar also has locales and timezones. It also has before and after methods.

Joda is a heavily used Java date/time API, but it doesn't come included in the Domino server.

Upvotes: 1

Frank van der Linden
Frank van der Linden

Reputation: 1271

In Java you can compare Dates by firstDate.before(otherDate) or firstDate.after(otherDate) it will return a boolean

Upvotes: 2

Related Questions