Java 時間和字符換的處理

	/**
	 * 
	 * @param timeStr  時間字符串
	 * @param diff     與起始值差距,單位爲毫秒
	 * @throws ParseException 
	 */
	public String dealDateString(final String timeStr,final long diff) throws ParseException{
		
		//轉換字符串格式
		final DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
			//轉換爲日期時間格式
			Date date = df.parse(timeStr);
			//獲取以1970年爲開始的尺度
			long times = date.getTime();
			//將日期格式轉換爲字符串格式
			final String result = df.format(new Date(times-diff));
			//返回字符串
			return result;
		} 

另外,上面須要用到SimpleDateFormat,Date這兩個包,所以須要在程序中導入:java

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;函數

若是咱們但願將某日期格式的數據轉換爲指定的字符串數據顯示,好比2014-11-05 11:20:20 爲2014年11月5日 11點20分20秒orm

	public String DateToString(final Date date){
		
		DateFormat df = new SimpleDateFormat("yyyy年MM月dd日 hh點:mm分:ss秒");
		return df.format(date);

	}

  

固然,目前的Date不少函數都已經被淘汰掉了!JDK1.6中已經有關於這些的詳細說明。blog

相關文章
相關標籤/搜索