格式化时间工具类

package com.dietime1943.core.utils;

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

/**
 * 格式化各类时间
 * @author dietime1943
 * @blog http://dietime1943.iteye.com/
 *
 */
public class TimeFormatUtil {
	
	public static String forMatTime(String tempTime) throws Exception{
		
		String normalTime = "";
		
		//2小时前
		if (tempTime.endsWith("小时前")) {
		
			Date currtDate = new Date();
			SimpleDateFormat norsf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
//			String tempTime = "2小时前";
			long time = Long.parseLong(tempTime.substring(0, tempTime.indexOf("小时前")));
			time = currtDate.getTime() - time*60*60*1000;
			
			
			normalTime = norsf.format(new Date(time)).replaceAll("1970", "2012");
		
//			System.err.println(normalTime);
			
			// 5分钟前
		}else if (tempTime.endsWith("分钟前")) {
			Date currtDate = new Date();
			SimpleDateFormat norsf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
//			String tempTime = "30分钟前";
			long time = Long.parseLong(tempTime.substring(0, tempTime.indexOf("分钟前")));
			time = currtDate.getTime() - time*60*1000;
			
			
			normalTime = norsf.format(new Date(time)).replaceAll("1970", "2012");
		
//			System.err.println(normalTime);
			
			
			// 22秒前
		}else if (tempTime.endsWith("秒前")) {
			Date currtDate = new Date();
			SimpleDateFormat norsf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
//			String tempTime = "30秒前";
			long time = Long.parseLong(tempTime.substring(0, tempTime.indexOf("秒前")));
			time = currtDate.getTime() - time*1000;
			
			
			normalTime = norsf.format(new Date(time)).replaceAll("1970", "2012");
		
//			System.err.println(normalTime);
			
			// 昨天14:55
		}else if (tempTime.contains("昨天")) {
		
			SimpleDateFormat norsf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
			SimpleDateFormat norsf1 = new SimpleDateFormat("yyyy-MM-dd");
//			String tempTime = "昨天21:52";
			SimpleDateFormat sf = new SimpleDateFormat("昨天HH:mm");
			Date date = sf.parse(tempTime);
			
			long date1 = date.getTime();
			Date date2 = new Date(date1);
//			System.out.println(date2);
			normalTime = norsf.format(date2).replaceAll("1970-01-01", norsf1.format(new Date(new Date().getTime()-24*60*60*1000)));
			
			
			// 前天 14:55
		}else if (tempTime.contains("前天")) {
			

			SimpleDateFormat norsf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
			SimpleDateFormat norsf1 = new SimpleDateFormat("yyyy-MM-dd");
//			String tempTime = "前天21:52";
			SimpleDateFormat sf = new SimpleDateFormat("前天HH:mm");
			Date date = sf.parse(tempTime);
			
			long date1 = date.getTime();
			Date date2 = new Date(date1);
//			System.out.println(date2);
			normalTime = norsf.format(date2).replaceAll("1970-01-01", norsf1.format(new Date(new Date().getTime()-2*24*60*60*1000)));
			
			
			//正常时间 04/0400:14
		}else {
			SimpleDateFormat norsf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
//			String tempTime = "04/0400:14";
			SimpleDateFormat sf = new SimpleDateFormat("MM/ddHH:mm");
			Date date = sf.parse(tempTime);
			
			long date1 = date.getTime();
			Date date2 = new Date(date1);
			
			normalTime = norsf.format(date2).replaceAll("1970", "2012");
		}
		
		
		return normalTime;
	}
	
	
	// test
	public static void main(String[] args) throws Exception {

		System.out.println("30秒前----------"+forMatTime("30秒前"));
		System.err.println("30分钟前---------"+forMatTime("30分钟前"));
		System.out.println("2小时前----------"+forMatTime("2小时前"));
		System.err.println("昨天14:55--------"+forMatTime("昨天14:55"));
		System.out.println("前天14:55--------"+forMatTime("前天14:55"));
		System.err.println("04/0400:14------"+forMatTime("04/0400:14"));

	}
}
 

 

运行结果:

30秒前----------2012-04-09 17:49
30分钟前---------2012-04-09 17:20
2小时前----------2012-04-09 15:50
昨天14:55--------2012-04-08 14:55
前天14:55--------2012-04-07 14:55
04/0400:14------2012-04-04 00:14
 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值