excel - How to check the date is in US format in java -
the user enters date follows:
20/05/2015
now know in format date invalid. how check in java? in code date value excel sheet , how i'm processing it. date read excel sheet numerical. example-3124.0 i.e., no. of days passed since 1/1/1990. converting format mm/dd/yyy:
simpledateformat sdf = new simpledateformat("mm/dd/yyyy"); string s = sdf.format(cell.getdatecellvalue());
format method of simpledateformat takes in date parameter. hence cell.getdatecellvalue() gives date type value passed format method converts string value.
simpledateformat sdf = new simpledateformat("mm/dd/yyyy"); string s = sdf.format(cell.getdatecellvalue()); system.out.println(s); sdf.setlenient(false); date d= sdf.parse(s); system.out.println(sdf.format(d));
so if excel cell value 12/08/2015
system.out.println(s); // prints 12/08/2015 system.out.println(sdf.format(d)); //prints 12/28/2015
don't understand.
if date comes string can try regex:
string date ="20/11/2015"; private static final string date_pattern = "(0?[1-9]|1[012])/(0?[1-9]|[12][0-9]|3[01])/((19|20)\\d\\d)"; pattern = pattern.compile(date_pattern); matcher = pattern.matcher(date); if(matcher.matches()){ system.out.println("mateched"); }else { system.out.println("not matched"); }
if have more once, make pattern
static , compile once (in constructor or static block probably).
Comments
Post a Comment