首页 > 其他 > 详细

利用SimpleDateFormat将String转换为格式化的日期

时间:2014-02-16 21:00:39      阅读:340      评论:0      收藏:0      [点我收藏+]
    
	// 将20120324解析为:2012-03-24
	private void parseTime1() {
		try {
			String time = "20120324";
			SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
			// SimpleDateFormat的parse(String time)方法将String转换为Date
			Date date = simpleDateFormat.parse(time);
			simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
			// SimpleDateFormat的format(Date date)方法将Date转换为String
			String formattedTime = simpleDateFormat.format(date);
			System.out.println("---->将" + time + "解析为:" + formattedTime);
		} catch (Exception e) {

		}

	}

	// 将20131227085009解析为:2013-12-27 08:50:09
	private void parseTime2() {
		try {
			String time = "20131227085009";
			SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
			// SimpleDateFormat的parse(String time)方法将String转换为Date
			Date date = simpleDateFormat.parse(time);
			simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			// SimpleDateFormat的format(Date date)方法将Date转换为String
			String formattedTime = simpleDateFormat.format(date);
			System.out.println("---->将" + time + "解析为:" + formattedTime);
		} catch (Exception e) {

		}

	}

利用SimpleDateFormat将String转换为格式化的日期

原文:http://blog.csdn.net/lfdfhl/article/details/19258769

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!