Java日期格式转换-yyyy-MM-dd HH:mm:ss转换为yyyy-MM-dd

package com.utcsoft.pcapps.selfservice.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class Test {

	/**
	 * yyyy-MM-dd HH:mm:ss转换为yyyy-MM-dd
	 * @author 刘鹏
	 * @throws ParseException 
	 */
	public String transferFormat(String inTime) throws ParseException{
		SimpleDateFormat s1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
		SimpleDateFormat s2 = new SimpleDateFormat("yyyy-MM-dd");
		Date tempDate =null;
		String outTime = null;
		tempDate = s1.parse(inTime);
		outTime = s2.format(s2.parse(s1.format(tempDate)));
		return outTime;
	}
	
	public static void main(String[] args) throws ParseException {
		Test test = new Test();
		String inTime = "2014-08-13 09:20:40";
		String outTime = test.transferFormat(inTime);
		System.out.println(outTime);//打印结果为2014-08-13
		
	}
}

你可能感兴趣的:(Java日期)