-5

I have following 2 time stamps,

Wed, 24 Feb 2016 09:02:12
Tue, 23 Feb 2016 09:01:59

how to compare them,

I am aware about we have inbuilt method for comparing date but do we have something for the given scenario.

Edit: Here i thought to compare the time first for example 09:02:12 and 09:01:59, which i can achieve by calculating hour , minutes and seconds into a single unit and can check which one is greater , but this will work fine if the date is same , my logic will not work if the date is different.

I have refereed this question as well

How to compare dates in Java?

but I am still not sure that this will be helpfull for my scenario.

Community
  • 1
  • 1
user2256009
  • 169
  • 1
  • 2
  • 9

1 Answers1

3
public static void main(String[] args)
    {
        String date1Str = "Wed, 24 Feb 2016 09:02:12";
        String date2Str = "Tue, 23 Feb 2016 09:01:59";

        SimpleDateFormat format = new SimpleDateFormat("E, DD MMM YYYY HH:mm:ss"); //Create formatter based on your required formats

        try
        {
            Date date1 = format.parse(date1Str); //convert string to date object
            Date date2 = format.parse(date2Str);
            System.out.println(date1.after(date2)); //compare the date
        }
        catch (ParseException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

for different formats https://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html

jan_kiran
  • 301
  • 3
  • 16