JAVA 利用SimpleDateFormat将String转换为格式化的日期
生活随笔
收集整理的這篇文章主要介紹了
JAVA 利用SimpleDateFormat将String转换为格式化的日期
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
1.
/*** 使用用戶格式提取字符串日期** @param strDate 日期字符串* @param pattern 日期格式* @return*/public static Date parse(String strDate, String pattern) {SimpleDateFormat df = new SimpleDateFormat(pattern);try {return df.parse(strDate);} catch (ParseException e) {return null;}}2.
/**** 將20120324* 解析為:2012-03-24**/private void parseTime1() {try {String time = "20120324";SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");// SimpleDateFormat的parse(String time)方法將String轉(zhuǎn)換為DateDate date = simpleDateFormat.parse(time);simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");// SimpleDateFormat的format(Date date)方法將Date轉(zhuǎn)換為StringString formattedTime = simpleDateFormat.format(date);System.out.println("---->將" + time + "解析為:" + formattedTime);} catch (Exception e) {}}3.
/***** 將20131227085009* 解析為:2013-12-27 08:50:09***/private void parseTime2() {try {String time = "20131227085009";SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");// SimpleDateFormat的parse(String time)方法將String轉(zhuǎn)換為DateDate date = simpleDateFormat.parse(time);simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// SimpleDateFormat的format(Date date)方法將Date轉(zhuǎn)換為StringString formattedTime = simpleDateFormat.format(date);System.out.println("---->將" + time + "解析為:" + formattedTime);} catch (Exception e) {}}?
轉(zhuǎn)載于:https://www.cnblogs.com/rinack/p/6484950.html
總結(jié)
以上是生活随笔為你收集整理的JAVA 利用SimpleDateFormat将String转换为格式化的日期的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: php-Arrays函数-array_f
- 下一篇: 阿里云的访问控制