Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Suggestions to exact variable length dates out of a string?

 
Todd Patrick
Ranch Hand
Posts: 31
Android Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have the following string:

Data Released September 11, 2006 at 10:30 A.M., For the Week Ending September 05, 2006

What I need to do is extract the two dates from the string.

A constant is the following:

Data Released [date 1] at [time], For the Week Ending [date 2]

I've been trying to use a combination of the String Object methods split(), indexof() and substring().

What is throwing me off is the variable length of the two dates.

I'd appreciate any suggestions or examples on how I can exact the two variable length dates out of a string.

Thanks.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if the constant data IS constant, this might work

 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could do something with regular expressions (the java.util.regex package):

You can learn more about how regular expressions work from the Java Tutorial. They're very powerful, but contain some complexities that may require a bit of practice.
[ May 17, 2006: Message edited by: Jim Yingst ]
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use a regular expression to parse the String:

[ May 17, 2006: Message edited by: Garrett Rowe ]
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jim Yingst:
You could do something with regular expressions (the java.util.regex package)...


That's what I was going to say!

Note though, you must call find(), matches(), or lookingAt() before querying the state of a Matcher or else an IllegalStateException will be thrown.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oops, yeah, thanks for catching that. I've edited my post above to fix the error.
 
Todd Patrick
Ranch Hand
Posts: 31
Android Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Much appreciated!

Thank you. Your suggestions are a lot better and faster than what I came up with:



Just shows, I have a lot to learn still...

Thanks again,
[ May 18, 2006: Message edited by: Todd Patrick ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic