Java日期工具类编写
将字符串转换为对应日期
Date date = simpleDateFormat.parse(string);
将日期转换为字符串
String string = simpleDateFormat.format(date);
注意,因为可能定义的格式和实际字符串提供的格式不符合,所以会抛出异常。
将年月日的汉字日期转为 - - 分隔符的日期
public static void main(String[] args) throws ParseException {
//统一日期格式
String StrVal = "2018年05月22日";
Date d1 = new SimpleDateFormat("yyyy年MM月dd日").parse(StrVal);
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
String time = format.format(d1);
System.out.println(time);
}