Friends I am having a String that contains date-record
String date=10-Oct-2012 @12-Oct-2012 @$12-Oct-2012 @12-Oct-2012 @$12-Sept-2012 13:50@12-Oct-2012 13:50@$12-Feb-2012 13:50@12-Oct-2012 13:50@$
List<Date> myList=new ArrayList<Date>() ;
I need to compare the dates 10-Oct-2012,12-OCt-2012,12-Sept-2012,12-Feb-2012 ie every odd date such that I can arrange them in a chronological order.I am confused on this implementation, please provide me with guidance/hint to solve the problem.
In this case the solution after chronological order would be 12-Feb-2012 @12-Oct-2012 @12-Sept-2012 @12-Oct-2012 @$10-Oct-2012 @12-Oct-2012 @$12-Oct-2012 @12-Oct-2012
Friends,to solve the problem I have created a Hashmap where I am planning to save the first date as key and the entire String as value.
String[] tokens=date.split("\\$");
demo[0]=demo[0].replaceAll("-", ".");
if(tokens.length>0)
{
for(int iTmp=tokens.length-1;iTmp>=0;iTmp--)
{
String []demo = tokens[iTmp].split("\\@");
demo[0] = demo[0].replace("Jan", "1")
.replace("Feb", "2").replace("March","3").replace("April","4").replace("May","5").replace("Jun","6").replace("July","7").replace("Aug","8")
.replace("Sept","9").replace("Oct","10").replace("Nov","11").replace("Dec","12");
demo[0]=demo[0]+" 00:05:00";
Date date1 = null;
SimpleDateFormat formatter = new SimpleDateFormat("dd.MM.yy");
try {
date1 = (Date)formatter.parse(demo[0]);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
myList.add(date1);
System.out.println("ADDED DATE IS"+date1);
//System.out.println("KEY VALUE PAIRS "+key+" "+tokens[iTmp]);
}
}
System.out.println("READING LISTs");
for(int iTmp=0;iTmp<myList.size();iTmp++)
{
System.out.println(myList.get(iTmp));
}
Collections.sort(myList);
System.out.println("After Sorting");
for(int iTmp=0;iTmp<myList.size();iTmp++)
{
System.out.println(myList.get(iTmp));
System.out.println();
}
It sounds pretty simple to me:
Calendar,Date, Joda Time’sLocalDate)(Using Joda Time is the preferred option here IMO, as neither
CalendarnorDatereally represent “just a date”; you’d have to put all values into the same time zone etc.)I would definitely not recommend trying to compare them as strings. As usual, convert your data into the most appropriate type for the information it’s trying to represent as early as possible – and convert it into serializing representations (e.g. for storage, propagation to a web service etc) as late as possible.