File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Using Patterns for Date String Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Using Patterns for Date String" Watch "Using Patterns for Date String" New topic

Using Patterns for Date String

Ajay Singh
Ranch Hand

Joined: Jan 04, 2008
Posts: 105
Hi all

i need some suggestions in woking with pattern/matcher classes for dates.What i am doing is giving an input date as "24 Feb 2002".My output should be displayed as :-
//d+//s//w+//d{4},//d+//s//w+//d+ etc (that is all possible combination which if i give as the argument to Pattern.matcher() and using the matcher class,output is the date "24 Feb 2002" )ie i am doing a reverse process.

i was putting the above string in a character array and my idea was to assign d ,w one by one for digits and words respectively and then display the final output as //d+//s//w+//d{4} or whatever.Any better idea?

Thanks in advance
Dariusz Kordonski
Ranch Hand

Joined: Jul 11, 2008
Posts: 49
I don't understand what you're trying to do... Do you actually want to parse some string containing dates in a specific format and find them in this string? Or format dates from one formatting into another? If first is true, then the regex to use would be more like: "\\d+\\s\\w+\\s\\d{4}" (notice using backslashes instead of slashes). But you must be aware, that such regex may also match expressions like e.g. '12323434 _123_ 1342', which is not really a date...
If you're actually trying to reformat a date stored as string, take a look at java.text.DateFormat / java.text.SimpleDateFormat API.

Ok now I think I know what you're trying to do. Do you actually want to put in some expression and receive as output regexes that would match it? There is Pattern#quote method, but as far as I know, it only returns literal pattern (no character classes)... Retrieving pattern that uses character classes from a given input sequence doesn't seem like a trivial task.
[ July 30, 2008: Message edited by: Dariusz Kordonski ]
Ajay Singh
Ranch Hand

Joined: Jan 04, 2008
Posts: 105

i am giving the input as "24 Feb 2002" in a string.what my code should do is to produce the possible regular expression for that string like //d+//s//w+//d{4},//d+//s//w+//d+ etc for this case.Hope its clear now.
I agree. Here's the link:
subject: Using Patterns for Date String
It's not a secret anymore!