Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Beginning Java and the fly likes Suggestions to exact variable length dates out of a string? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Suggestions to exact variable length dates out of a string?" Watch "Suggestions to exact variable length dates out of a string?" New topic
Author

Suggestions to exact variable length dates out of a string?

Todd Patrick
Ranch Hand

Joined: Jan 30, 2004
Posts: 31

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.


--Todd
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
if the constant data IS constant, this might work

Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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 ]

"I'm not back." - Bill Harding, Twister
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
You can use a regular expression to parse the String:

[ May 17, 2006: Message edited by: Garrett Rowe ]

Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
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

Joined: Jan 30, 2000
Posts: 18671
Oops, yeah, thanks for catching that. I've edited my post above to fix the error.
Todd Patrick
Ranch Hand

Joined: Jan 30, 2004
Posts: 31

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 ]
 
jQuery in Action, 2nd edition
 
subject: Suggestions to exact variable length dates out of a string?
 
Similar Threads
Getting Elements from My Array
Date difference
What is the Data Type for GMT Date and Time?
NEED HELP
Create an Array of Weeks