| | |
Reverse String?
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Dec 2007
Posts: 11
Reputation:
Solved Threads: 0
Hey guys, back again I be. Having a new problem with my dates. I want to sort by date and that's fine but in order to sort correctly by date i would have to reverse the date and ignore the slashes or it wont recognise that 2006 is before 2007 for instance as it only reads the first bit. Is there something i can do to reverse this string for sorting.
Regards
Jez
Regards
Jez
Well, sort date objects rather than date strings and you wn't have this problem. Use SimpleDateFormat to create actual date objects from the date strings, then sort those.
Java Programmer and Sun Systems Administrator
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Parsing them into valid dates as masijade mentioned is really best, but if you have simple date strings like "12/01/2006", you could use the String split() function to put them in year-month-day form "20031201" for integer sorting If you have times in the string as well then it gets trickier and you would need to use regex parsing to create the sortable representation - at which point you would have just been better off using the date formatter in the first place...
Java Syntax (Toggle Plain Text)
String date = "12/10/2006"; String[] datePieces = date.split("/"); String rebuiltDate = datePieces[2]+datePieces[0]+datePieces[1]; System.out.println(rebuiltDate);
![]() |
Similar Threads
- To form a palindrome of a given string (C)
- How do you reverse string elements (C++)
- reverse a string using recursion (C)
- Reverse String - Assembly Language (Assembly)
- Reversing a string? (C++)
Other Threads in the Java Forum
- Previous Thread: Counting Die-Rolls, Random
- Next Thread: Icon movement
Views: 2595 | Replies: 2
| Thread Tools | Search this Thread |
Tag cloud for Java
android api apple applet application arguments array arrays automation bidirectional binary birt bluetooth calculator chat class classes client code columns component database designadrawingapplicationusingjavajslider detection draw eclipse editor error errors event exception expand file fractal game givemetehcodez graphics gui guidancer helpwithhomework html ide image inetaddress input integer intellij j2me java javamicroeditionuseofmotionsensor javaprojects jme jmf jni jpanel julia linux list loop map method methods mobile mobiledevelopmentcreatejar myaggfun netbeans newbie number object oracle os plazmic print problem program programming project recursion scanner screen server set signing size smart sms smsspam socket sort sql string subclass support swing test threads time transfer tree windows






