Hi, How to sort ascending & descending order following date & time? June 13, 2003,10:35:46 AM PDT June 13, 2003,8:44:06 AM PDT May 29, 2003,2:37:57 PM PDT Can anybody give me good example? Thanks, Angela [ December 06, 2003: Message edited by: Marilyn de Queiroz ]
hi Angela i know someone would come up with how to use Comparable or Comparator things to achieve this sort but a different way i figured out in sorting date/time is as follows, u format ur input in string form, yyyymmdd(AM/PM)hhmmss and just compare strings. it would work. e.g. 1. 20030323AM05:34:45 2. 20030323PM04:34:45 when u use compareTo() of string it will return the correct result right? regards maulin
Thanks. I am using similar following things you described but doesn't work. File dst = new File(file); DateFormat data1 = DateFormat.getDateInstance(DateFormat.LONG); DateFormat data2 = DateFormat.getTimeInstance(DateFormat.LONG); String date = data1.format(new java.util.Date(dst.lastModified())) + "," + data2.format(new java.util.Date(dst.lastModified())); and I used compare method to compare date string. Note: String date give following results: June 12, 2003,11:34:52 AM PDT June 13, 2003,10:35:46 AM PDT June 12, 2003,2:36:02 PM PDT June 13, 2003,10:43:42 AM PDT May 29, 2003,3:08:34 PM PDT Please let me know Thanks again, Angela
hi Angela can u please S.O.P. the output of , String date = data1.format(new java.util.Date(dst.lastModified())) + "," + data2.format(new java.util.Date(dst.lastModified())); see what is the string that u get in 'date' variable and print all of them to make sure it is exactly the format what i refer to... please note that i use "mm" as "number" in the date format meaning it can be [01..12] rather than [Jan..Dec] text based months.. regards maulin
Originally posted by Maulin Vasavada: hi Angela see what is the string that u get in 'date' variable and print all of them to make sure it is exactly the format what i refer to... please note that i use "mm" as "number" in the date format meaning it can be [01..12] rather than [Jan..Dec] text based months.. regards maulin
I get following print out of 'date' variable: June 12, 2003,11:34:52 AM PDT June 13, 2003,10:35:46 AM PDT June 12, 2003,2:36:02 PM PDT June 13, 2003,10:43:42 AM PDT May 29, 2003,3:08:34 PM PDT How can I get the format you refer to ? Thanks, Angela
Originally posted by Maulin Vasavada: hi Angela please note that i use "mm" as "number" in the date format meaning it can be [01..12] rather than [Jan..Dec] text based months.. regards maulin
what did you use to "mm" as "number" rather than text. Did you use DateFormat class??? Thanks, Angela
hi angela sorry about not being clear. i didn't use any code so far i just mentioned the mechanism that works. i see in the output you have that it is not as per what i mentioned. i suggested to have format, yyyymmdd(AM/PM)hhmmss and i also provided the examples of that format like, 20030324AM092434 20030324PM092434 somehow get ur date in that format. i'm sorry that i can't come up with the example right now to have that format but i 'd try once i reach home (i'm at office). regards maulin
hi Angela okay..i couldn't wait till reaching home.. here u go..
u get it? to know why i have yyyymmddahhmmss as formatter please refer to the java.text.SimpleDateFormat API where they define meaning of everything.. regards maulin
I tried following to get the format you suggested: SimpleDateFormat sdf = new SimpleDateFormat("yyyymmddhhmmssa"); It gives the format you suggested in String date. I could able to sort it out correctly. After sorting, Now I can't display like: 20030324AM092434 to the users. Do you know how to convert this string(20030324AM092434) into following format 20003/03/24, 09:24:34 AM PST Thanks, Angela
I've just seen the topic here, so I may be coming at it from a different angle, but it seems to me that the best way to sort dates (so long as you want "natural" ordering, and nothing I see in you post suggests otherwise) is to simply sort on the .getTime() method of the dates. Since Date implements Comperable, you can simply put the dates into a java.util.Date[] dateArray and call java.util.Arrays.sort(dateArray). For a descending sort, either write a Comperator that inverts the sort criterea or, more simply, dread the array from back to front.
To answer your other question, use two SimpleDateFormats. One to convert your string to a date and then one to convert the date back to a differntly-formatted string.
I just used following code: File dst = new File(fileName); SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy, HH:mm:ss a z"); String date = sdf.format(new java.util.Date(dst.lastModified())); Then using comparator I did sorting. It's working fine! Thanks for help, Angela
Post by:autobot
The two armies met. But instead of battle, they decided to eat some pie and contemplate this tiny ad:
a bit of art, as a gift, the permaculture playing cards