使用DateFormat格式化日期,dateformat格式化,要想把一个日期字符串转换
分享于 点击 17345 次 点评:57
使用DateFormat格式化日期,dateformat格式化,要想把一个日期字符串转换
要想把一个日期字符串转换成一个时间,例如把“2006-2-6”转换成日期,可以使用下面的代码:
String dates = "2006-2-6"; //定义日期字符串 DateFormat df2 = new SimpleDateFormat("yyyy-MM-dd"); //定义日期字符串的格式 Date d2; //声明日期对象 try { d2 = df2.parse(dates); //把日期字符串转换成日期 System.out.println(df2.format(d2)); }catch (ParseException ex) { }
注意:DateFormat和SimpleDateFormat在 java.text包中,使用的时候需要引入;在转换的时候需要进行异常处理,因为在转换的时候可能会产生异常。
用户点评