我想將日期轉換爲用戶需要的格式。我想要所有格式的日期。 但格式化日期不對,請幫忙。將日期轉換爲不同的格式
package DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateFormat
{
DateFormat() throws ParseException
{
String dateFormats[] =
{
"YYYY/MM/DD",
"DD/MM/YYYY",
"DD-MM-YYYY",
};
for (int i = 0; i < dateFormats.length; i++)
{
String newDate = new SimpleDateFormat(dateFormats[i]).format(new Date());
System.out.println(newDate);
}
}
public static void main(String[] args) throws ParseException
{
new DateFormat();
}
}
輸出
2016/04/98
98/04/2016
98-04-2016
謝謝。
。有關支持的格式,請參閱[文檔](http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html)。 – Rao