2013-01-22 22 views
0

我正在尋找一種解析字符串的方法,該字符串可以用我提供的日期組件替換某些模式。裝飾日期格式

你知道一個標準的方法嗎?

一個用法是:

parseForDate("fileName%YYYY%MM.csv", new Date()); // returns: filename201301.csv 

問候

+1

我會建議你使用[MessageFormat中(HTTP://文檔.oracle.com/javase/7/docs/api/java/text/MessageFormat.html)與已經提到的[SimpleDateFormat](http ://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html) – ThanksForAllTheFish

回答

2

考慮使用format

String filename = String.format("fileName%1$tY%1$tm.csv", new Date()); 
+0

謝謝,這正是我一直在尋找的。我只是覺得有一個標準的方法來做到這一點。 – BenoitParis

1
String filename = "fileName%" + new SimpleDateFormat("yyyy%MM").format(new Date())+".csv"; // returns: filename201301.csv