AnujAroshA
AnujAroshA

Reputation: 4811

Convert and compare two date formats in JAVA

In my application I am having two different types of date formats. One gives Thu Apr 19 12:10:26 GMT+05:30 2012 as out put and other one gives 1334654035000 as out put.

Can I change the first format to the second type (which has only numbers) ? The reason is,I have to compare two different dates.

Upvotes: 4

Views: 5010

Answers (4)

user1920778
user1920778

Reputation: 7

parse the date and and compare the date

                 if(!sToDate[Integer.valueOf(sChecked[i])].equalsIgnoreCase("")&&!sFromDate[Integer.valueOf(sChecked[i])].equalsIgnoreCase(""))

if(sToDate[Integer.parseInt(sChecked[i])].compareTo(sFromDate[Integer.parseInt(sChecked[i])])<0)

Upvotes: 0

Phani
Phani

Reputation: 5427

Yes. You could do it. If we assume the dates are under the same time zone, convert the dates using SimpleDateFormat class to Calendar and then invoke the methods on Calendar object to compare dates.

Upvotes: 0

Woody
Woody

Reputation: 5130

using the parse method of java.text.SimpleDateFormat should do what you want

Upvotes: 0

JB Nizet
JB Nizet

Reputation: 691805

If you want to compare two dates formatted differently,

  • parse the first string with the appropriate date format to transform the string into a Date
  • parse the second string with the appropriate date format to transform the string into a Date
  • compare the Date objects

Upvotes: 3

Related Questions