0

I have a localized date format. I want to retrieve just the year format in Java.

So if I am given mmddyyyy I would like to extract yyyy. if I am given mmddyy, i would like to extract yy.

I cannot find a way to get that info using SimpleDateFormat, Date, Calendar etc. classes.

dmanners
  • 2,062
  • 1
  • 18
  • 24
  • What would be the year in the date string "01/02/03"? See [this](http://stackoverflow.com/q/15010210/155813) and [that](http://stackoverflow.com/q/4216191/155813). – mg007 May 18 '13 at 17:23

1 Answers1

0

It's important to note that the concept of a "year format" only really applies to SimpleDateFormat. (In the default JDK, anyway.) More specifically, SimpleDateFormat is the only DateFormat implementation provided by the JDK that uses the concept of a "format string" that you can pull out a year format from; the other implementations use more opaque mappings from a Date to a String. For this reason, what you're asking for is only well-defined on the SimpleDateFormat class (again, among the DateFormat implementations available in the stock JDK).

If you're working with a SimpleDateFormat, though, you can just pull the year format out with regular expressions:

SimpleDateFormat df=(something);
final Pattern YEAR_PATTERN=Pattern.compile("^(?:[^y']+|'(?:[^']|'')*')*(y+)");
Matcher m=YEAR_PATTERN.matcher(df.toPattern());
String yearFormat=m.find() ? m.group(1) : null;
// If yearFormat!=null, then it contains the FIRST year format. Otherwise, there is no year format in this SimpleDateFormat.

The regular expression looks so strange because it has to ignore any y's that happen in "fancy" quoted parts of the date format string, like "'Today''s date is 'yyyy-MM-dd". Per the comment in the code above, note that this only pulls out the first year format. If you need to pull out multiple formats, you'll just need to use the Matcher a little differently:

SimpleDateFormat df=(something);
final Pattern YEAR_PATTERN=Pattern.compile("\\G(?:[^y']+|'(?:[^']|'')*')*(y+)");
Matcher m=YEAR_PATTERN.matcher(df.toPattern());
int count=0;
while(m.find()) {
    String yearFormat=m.group(1);
    // Here, yearFormat contains the count-th year format
    count = count+1;
}
sigpwned
  • 6,957
  • 5
  • 28
  • 48
  • I should always get a SimpleDateFormat. So this is the code I wrote – user2397334 May 18 '13 at 18:02
  • SimpleDateFormat df= new SimpleDateFormat("mmddyyyy"); final Pattern YEAR_PATTERN=Pattern.compile("y+"); MatchResult m=YEAR_PATTERN.matcher(df.toPattern()).toMatchResult(); String yearFormat; if(m!=null) yearFormat = m.group(); else yearFormat = "empty"; System.out.println("year pattern is =====>"+yearFormat); – user2397334 May 18 '13 at 18:07
  • I get Exception in thread "main" java.lang.IllegalStateException: No match found – user2397334 May 18 '13 at 18:09
  • Sorry about that. Try the updated code. Devil's in the details, as usual. :P – sigpwned May 18 '13 at 18:15
  • Thank you for such a prompt reply – user2397334 May 18 '13 at 18:20