SimpleDateFormat
date --> text
Date now = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); String strDate = sdf.format(now); System.out.println(strDate);
Date now = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日"); String strDate = sdf.format(now); System.out.println(strDate);
Date now = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss"); String strDate = sdf.format(now); System.out.println(strDate);
text --> date
//由字符串解析成Date对象
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); try { Date date = sdf.parse("2018-10-10"); System.out.println(date); } catch (ParseException e) { System.out.println("时间解析出错啦:" + e.getMessage()); }
Method
String format(Date date)
把Date格式化为字符串Formats a Date into a date/time string.
Date parse(String source)
把字符串解析为Date
Parses text from the beginning of the given string to produce a date.
原文地址:http://lsieun.blog.51cto.com/9210464/1845368