format()fromSimpleDateFormat类可通过使用提供的模式字符串将Date对象转换为特定格式的String对象。
Date today = new Date(); SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MMM-yy"); //模式在这里指定 System.out.println(dateFormat.format(today)); //25-Feb-16
可以通过使用再次应用图案 applyPattern()
dateFormat.applyPattern("dd-MM-yyyy"); System.out.println(dateFormat.format(today)); //25-02-2016 dateFormat.applyPattern("dd-MM-yyyy HH:mm:ss E"); System.out.println(dateFormat.format(today)); //25-02-2016 06:14:33星期四
注意:此处mm(小写字母m)表示分钟,MM(大写字母M)表示月份。格式化年份时请格外注意:大写Y字母“ Y”(y)表示“一年中的星期”,小写字母“ y”()表示年份。