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这两个包,因此需要在程序中导入:

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

如果我们希望将某日期格式的数据转换为指定的字符串数据显示,比如2014-11-05 11:20:20 为2014年11月5日 11点20分20秒

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

	}

  

当然,目前的Date很多函数都已经被淘汰掉了!JDK1.6中已经有关于这些的详细说明。

 

posted @ 2014-11-05 21:40  CBDoctor  阅读(460)  评论(0编辑  收藏  举报