0

How to compare below two dates in java. my scenario is

  String dt1="2013/01/08";
  Date f=Calendar.getInstance().getTime();

  and how to add time(hh:mm:ss.ms) to the date string(i mean to dt1)eg. 2013/01/08 21:10:01.23
Manu
  • 3,179
  • 23
  • 57
  • 69
  • 1
    I'd start [here](http://stackoverflow.com/questions/4216745/java-string-to-date-conversion) for more details on Date objects and Strings. – Oren Jan 08 '13 at 14:53

2 Answers2

3
        String dt1="2013/01/08";
        Date f=Calendar.getInstance().getTime();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
        System.out.println(sdf.parse(dt1).after(f));
  • use SimpleDateFormat and format the String into java.util.Date
  • use java.util.Date.after() or java.util.Date.before() to compare the two dates
PermGenError
  • 45,977
  • 8
  • 87
  • 106
0

You can use compareTo method also

    String dt1="2013/01/08";
    Date f=Calendar.getInstance().getTime();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
    System.out.println(sdf.parse(dt1).compareTo(f);
Jayamohan
  • 12,734
  • 2
  • 27
  • 41