SIMPLEDATEFORMAT
此类的功能是完成日期的显示格式化的、在开发中、可以会将一种日期格式变为另外一种日期格式、比如:将日期:2015-07-19 10:11:30.345转换为日期:2015年07月19日10点11分30秒345毫秒
但是以上的两个日期中日期的数字是完全一样的、唯一不同的是日期的显示格式不同、所以要想实现这样的转换功能就必须依靠SimpleDateFormat类
需要注意的是、在创建SimpleDateFormat的时候一定要指定时间区域、不然会有如下警告
To get local formatting use getDateInstance(), getDateTimeInstance(), or getTimeInstance(), or use new SimpleDateFormat(String template, Locale locale) with for example Locale.US for ASCII dates.
以下通过SimpleDateFormat把String转Date的方法和把Date转String的方法
// String 转换为 Date public static Date strToDate(String strdate) { DateFormat format = new SimpleDateFormat("yyyy-MM-dd", new Locale("zh", "CN")); Date date = null; try { date = format.parse(strdate); } catch (ParseException e) { e.printStackTrace(); } return date; } // Date 转换为 String public static String dateToStr(Date date) { // 年月日****-**-** DateFormat format = new SimpleDateFormat("yyyy-MM-dd", new Locale("zh", "CN")); String str = format.format(date); System.out.println("str:" str); // 年月日**-*-* format = DateFormat.getDateInstance(DateFormat.SHORT); str = format.format(date); System.out.println(str); // 年月日****-*-* format = DateFormat.getDateInstance(DateFormat.MEDIUM); str = format.format(date); System.out.println(str); // ****年*月*日星期* format = DateFormat.getDateInstance(DateFormat.FULL); str = format.format(date); System.out.println(str); return str; }