Java的SimpleDateFormat类
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java的SimpleDateFormat类相关的知识,希望对你有一定的参考价值。
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.
以上是关于Java的SimpleDateFormat类的主要内容,如果未能解决你的问题,请参考以下文章