0

I have string date time "2014-10-13 18:22:54.71", I want to convert that to Date and

I'll use that algorithm

public static boolean IsthatMorethanTwo(String DateString) {
    Calendar thatDay = Calendar.getInstance();
    thatDay.setTime(new Date(DateString));
    Calendar today = Calendar.getInstance();
    long diff = today.getTimeInMillis() - thatDay.getTimeInMillis();
    long days = diff / (24 * 60 * 60 * 1000);
    return days >= 2;
}

How can I do this ?

massaimara98
  • 7,401
  • 1
  • 18
  • 18

1 Answers1

2

You can use SimpleDateFormat class.

Like this:

String dateString = "2014-10-13 18:22:54.71";    
SimpleDateFormat format = 
        new SimpleDateFormat("yyyy-mm-dd HH:mm:ss.S");

try {
    Date parsed = format.parse(dateString);
}
catch(ParseException pe) {
    System.out.println("ERROR: Cannot parse \"" + dateString + "\"");
}
Pablo Santa Cruz
  • 176,835
  • 32
  • 241
  • 292