• Post Reply Bookmark Topic Watch Topic
  • New Topic

String reverse  RSS feed

 
Ivan Kesic
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello

I have a string looking like this "2001-12-12". I need to transform it to look like this "12-12-2001" How do i do this?
StringBuilder reverse() just give me "21-21-1002" but that is not what i need. Is there any way to do this, or i need to make my own method?
 
Randall Fairman
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use String.split() to split on occurrence of '-', and you'll have the three separate pieces you need. Now put them back together in the order you want them.
 
Ramon Anger
Ranch Hand
Posts: 56
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ivan,

there is no direct way for this type of string conversion. You could to the following:
Convert your string into a Date and use SimpleDateFormat to get the string representation of this Date in the format you want.
See for instance http://download.oracle.com/javase/1.4.2/docs/api/java/text/SimpleDateFormat.html

BTW, if you have all the time your 2001-12-12 format it's easier to implement your own conversion method using the dashes as stop flags.

Regards,
Ramon
 
Ivan Kesic
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok ill convert it with dashes, thank you!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!