0

I create a date and then format is like this:

Example 1:

SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss   dd/MM/yyyy");
                    String currentDate = sdf.format(new Date());

What I would like to do is check if this date is before another date (also formatted the same way). How would I go about doing this?

Example 2:

Also, how would I check whether one of these is before another:

long setForLong = System.currentTimeMillis() + (totalTime*1000);
String display = (String) DateFormat.format("HH:mm:ss   dd/MM/yyyy", setForLong);

EDIT:

I think more detail is needed. I create a date in two different ways for two different uses. The first use just formats the current date into a string so it is readable for the user. In the second case, I am using a date in the future with System.currentTimeMillis and adding on a long. Both result in a string.

Both methods format the date in exactly the same way, and I set the strings into a TextView. Later, I need to compare these dates. I do not have the original data/date/etc, only these strings. Becasue they are formatted in the same way, I though it would be easy to compare them.

I have tried the if(String1.compareTo(String2) >0 ) method, but that does not work if the day is changed.

2 Answers2

0

You should use Calendar for convenient comparing dates.

Calendar c1 = Calendar.getInstance();
c1.setTime(Date someDate);
Calendar c2 = Calendar.getInstance();
c2.setTime(Date anotherDate);
if(c1.before(c2)){
    // do something
}

And you can format it at any time

SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss   dd/MM/yyyy");
String currentDate = sdf.format(c1.getTime());
Viacheslav
  • 5,443
  • 1
  • 29
  • 36
  • Thanks, but I need to be able to format the date so it is readable to the user. –  Aug 26 '13 at 19:20
  • I have edited the question with another example. This method works well for the first example –  Aug 26 '13 at 19:33
  • I have also added detail/context to try to help understanding. Thanks for taking interest :-) –  Aug 26 '13 at 20:15
  • @RiThBo try to separate data and data representation in your app. I mean long or String it's just a way you present data for user and really no matters when you need to operate with these data e.g. compare dates. So you can represent it to user any way but "behind the scene" you should have sole format to store data and operate with it. Of course it's just my IMO. Sorry for poor explanation but my English isn't so well to explain such things with full details. – Viacheslav Aug 27 '13 at 06:16
  • It's a little difficult for me to do that as the strings are getting passed around everywhere. Maybe I will add a separate piece of data for the date which I need to operate on. –  Aug 27 '13 at 06:36
0

If you only have two String objects that are dates available to you. You will need to process them in something, either in your own comparator class or in another object. In this case, since these are already formatted into dates, you can just create Date objects and compare using the methods previously posted. Something like this:

String string = "05:30:33   15/02/1985";
Date date1 = new SimpleDateFormat("HH:mm:ss   dd/MM/yyyy", Locale.ENGLISH).parse(string);

String string2 = "15:30:33   01/02/1985";
Date date2 = new SimpleDateFormat("HH:mm:ss   dd/MM/yyyy", Locale.ENGLISH).parse(string2);

if(date1.getTime()>date2.getTime()) {
    //date1 greater than date2
}
else if(date1.getTime()<date2.getTime()) {
    //date1 less than date2
}
else {
    //date1 equal to date2
}  
Nick
  • 1,854
  • 1
  • 13
  • 16
  • Thanks for the answer. Will this work in different time zones? –  Aug 27 '13 at 05:46
  • I think it will if I use Locale.getDefault(). –  Aug 27 '13 at 09:46
  • 1
    Yes that will give you the current `Locale` of the user. – Nick Aug 27 '13 at 16:39
  • This works fine on my Nexus 4 running 4.3 but not for my old Motorola Xoom. The strings are unparseble. Any ideas? Specifcally the HH that do not work. –  Aug 27 '13 at 19:59
  • What Android version is your Xoom runnning? – Nick Aug 27 '13 at 20:06
  • CyanogenMod 10.1 The nightly before the final milestone. (Android 4.2.2) –  Aug 27 '13 at 20:42
  • What happens when you try to parse them? Do you just get `null` dates? – Nick Aug 27 '13 at 21:03
  • Yes because it can't parse them properly. –  Aug 28 '13 at 05:40
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/36376/discussion-between-rithbo-and-user2453771) –  Aug 28 '13 at 06:09