• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how do you strip out certain groups of characters from a String

 
Robert Johnson
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
for exapmle...

String date = "11-Feb-2005";
String day;
String month;
String year;

how would you strip out '11' from date to assign it to 'day', and 'Feb' to assign it to 'month' and '2005' to assign it to 'year'.

in my program the variable 'date' will always be in the format of:
ist two digits are numbers followed by '-'
then three digits (letters) followed by '-'
then four digits that are numbers.

i think it has something got to do with charAt or something, im not sure how to do it.

any ideas?
 
Dave Wingate
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're probably going to want to use SimpleDateFormat.

Create a SimpleDateFormatObject with the format string in which you are interested. Use that object to parse the string into a Date. Once you have the date object you can use its methods to extract day, month, etc.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Parsing strings is fun and keeps you off the streets at night.

Since you have reliable delimiters you could use String.split (JDK1.4) or StringTokenizer or Scanner (JDK5).

Since you have reliable field lengths, you could use String.substring().

Since you have a date you can make DateFormat do all the work as suggested above, but then you have to get back the strings and maybe worry about leading zeros and such.

For recreation and education you might try all of those and see which you like best. Feel free to post some code if you try something that won't quite work. Let us know how it goes!
 
Matthew Taylor
Rancher
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think split is your best option.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64959
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or you could investigate regular expressions.
 
Robert Johnson
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yea the split thing worked and so did the StringTokenizer. im using both now. its nice to learn new things like that.

these dates is driving me insaine,
they input into oracle in one format, when you select them from oracle its a different format, java dates is another format, then i have to change them to another different java format to compare dates, then change them all back. jesus.

i have another question in here regarding the gregorianCalenger (in a new post).
maybe you can help me out with that.


thanks for your help
 
Matthew Taylor
Rancher
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wrote a helper class to help me out with date formats and put it in my utilities project. It's been helpful to have all date/string transformations in one place.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic