compare Date yyyy-mm-dd only





8
Date Submitted Mon. Aug. 7th, 2006 4:48 PM
Revision 1 of 1
Scripter bertheymans
Tags Date | Java | Time
Comments 0 comments
If you only want to compare yyyy-mm-dd you can use the java.sql.Date class, the valueOf method does the trick. It's perfectly safe as the sql Date class is a subclass of the Date class.

Check out the docs here for more detail.

enjoy

import java.sql.Date;

class DateTest {
        public static void main(String[] args) {
                Date today = Date.valueOf("2006-08-08"); // use date of today here
                Date alsoToday = new Date(System.currentTimeMillis()); // has time information and will probably not be equal to "today"
               
               
                System.out.println(today.equals(alsoToday)); // false
               
                System.out.println(today.equals(Date.valueOf(alsoToday.toString()))); // true
        }
}

Bert Heymans

blog.heymans.org

Comments

There are currently no comments for this snippet.

Voting