SimpleDateFormat

(java.text.SimpleDateFormat)
 
// ex: 2010/01/01 or 2010/1/1
String pattern = "[0-9]{4}[/][0-9]{1,2}[/][0-9]{1,2}";
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
dateFormat.setLenient(false);
……
Sring inDate = "2010/01/35";
if (Pattern.matches(pattern, inDate)) {
     outDate = dateFormat.parse(inDate);
}
 
Lenient 的預設值為 true, 表示會自動進位, 自動計算正確的日期.
ex: 2010/13/20=>2011/1/20
 
如果 Lenient 設為false, 不合法的日期就會出現 ParseException
 
 
 
 
 
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s