Skip to content Skip to sidebar Skip to footer

Android - Time Comparison "hh:mm:ss A" Format

'I try to compare two time in 'hh:mm:ss a' format. But it not really work in what I think. I googled but couldn't find a proper answer. Sorry cause I'm new to programming.' I try t

Solution 1:

Instead of comparing the formatted string, compare the value in milliseconds. Take a look at this to convert the string back to date:

SimpleDateFormatformatter=newSimpleDateFormat("dd-MMM-yyyy");
StringdateInString="7-Jun-2013";

try {

    Datedate= formatter.parse(dateInString);
    System.out.println(date);
    System.out.println(formatter.format(date));

} catch (ParseException e) {
    e.printStackTrace();
}

Then once you have two dates you can compare them like so:

booleanbefore= someDate.before(anotherDate);

or

booleanafter= someDate.after(anotherDate);

or even

 someDate.getTime() < anotherDate.getTime();

Side note: when I store dates, I like to just store the millisecond value and the time zone. That way you don't need to worry about things like this.

Solution 2:

Inside the SQLite database you are storing the dates as Strings, not as a Date because in SQLite doesn't exist a Date type (https://www.sqlite.org/datatype3.html).

You have two options: change the column type to a INTEGER type and store de date as a number (then you can compare milliseconds) or get the entity as it is, parse a Date type with the String, create a SimpleDateFormat with the given format and then make the comparison.

Post a Comment for "Android - Time Comparison "hh:mm:ss A" Format"