Real'sHowTo AddThis Feed Button
Custom Search

Compare 2 datesTag(s): Date and Time


import java.util.*;
import java.util.*;
import java.text.*;

public class TestDate {
  public static void main(String args[]){
    String DATE_FORMAT = "yyyy-MM-dd";
    java.text.SimpleDateFormat sdf =
          new java.text.SimpleDateFormat(DATE_FORMAT);
    Calendar c1 = Calendar.getInstance();
    Calendar c2 = Calendar.getInstance();
    // remember months are zero-based : 0 jan 1 feb ...
    c1.set(1999, 11 , 31);
    c2.set(1999, 0 , 30);

    System.out.print(sdf.format(c1.getTime()));

    if (c1.before(c2)) {
       System.out.print(" is before ");
       }
    if (c1.after(c2)) {
       System.out.print(" is after ");
      }
    if (c1.equals(c2)) {
       System.out.print(" same as ");
      }
    System.out.println(sdf.format(c2.getTime()));
    }
}

blog comments powered by Disqus


If you find this article useful, consider making a small donation
to show your support for this Web site and its content.

Written and compiled by Réal Gagnon ©1998-2014
[ home ]