java String字符串与Date的相互转换

package time;


import java.util.Date;
import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.commons.lang.time.DateUtils;


public class Time {
public static void main(String[] args){
try {
        Date dateStart = DateUtils.parseDate("2017/11/15 19::00", new String[]{"yyyy/MM/dd hh:mm:ss"});//字符串转Date
        String startDate = DateFormatUtils.format(dateStart, "yyyy-MM-dd HH:mm:ss");//Date转字符串

      Date dateStart1 = DateUtils.parseDate("2018-10-12T11:15:42Z", new String[]{"yyyy-MM-dd'T'hh:mm:ss'Z'"});//字符串转Date
        System.out.println(startDate1);
} catch (Exception e) {
e.printStackTrace();
}
}
}

 

以下是java将"30/Sep/2018:10:47:23 +0800" 时间格式转为yyyyMMddHH的demo

	public static void main(String[] args){
		try {
			String timeLocal ="30/Sep/2018:10:47:23 +0800";
			System.out.println(changeTimeLocal(timeLocal));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 将"30/Sep/2018:10:47:23 +0800" 时间格式转为yyyyMMddHH
	 * @param timeLocal
	 * @return
	 */
	private static String changeTimeLocal(String timeLocal){
		try {
			if(StringUtils.isNotEmpty(timeLocal)){
				String timeLocalStrAll[] = timeLocal.split(" ");
				SimpleDateFormat df = new SimpleDateFormat("dd/MMM/yyyy:HH:mm:ss", Locale.US);
				if(StringUtils.isNotEmpty(timeLocalStrAll[0])){
					Date date = df.parse(timeLocalStrAll[0]);
					SimpleDateFormat Tmp_df = new SimpleDateFormat("yyyyMMddHH", Locale.US);
					return Tmp_df.format(date);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

 

你可能感兴趣的:(java String字符串与Date的相互转换)