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