米其林的微笑

I am here,as always。。。。。

导航

Time格式转换

Posted on 2011-03-19 00:19  米其林的微笑  阅读(294)  评论(0编辑  收藏  举报
1.将"Thu Apr 30 01:33:41 +0000 2009"转换为"yyyy年MM月dd日 HH时mm分ss秒"
public static String getDisplayDateStr(String dateStr){
Date dd = new Date(dateStr);
SimpleDateFormat myFmt
=new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
String str
= "";
try{
str
= myFmt.format(dd);
}
catch(Exception e){
e.printStackTrace();
}
dout(str);
return str;
}
2.输入"Thu Apr 30 01:33:41 +0000 2009" 返回Date
public static Date str2Date(String dateStr){
return new Date(dateStr);
}

3.传入一个Date,判断是多久前,返回示例"4小时前"

public static String getHowLongStr(Date date){
String rs
= "";
Long i
= date.getTime();
Date now
= new Date();
Long j
= now.getTime();
dout(j
- i);
long day = 1000 * 60 * 60 * 24;
long hour = 1000 * 60 * 60;
long min = 1000 * 60;
long sec = 1000;
if (((j-i)/day)>0)
rs
= ((j-i)/day)+"天前";
else if (((j-i)/hour)>0)
rs
= ((j-i)/hour)+"小时前";
else if (((j-i)/min)>0)
rs
= ((j-i)/min)+"分钟前";
else if (((j-i)/sec)>0)
rs
= ((j-i)/sec)+"秒前";
return rs;
}

4.number型的直接换换成时间Date类型

long myDate = 1270202674000L;
SimpleDateFormat fm1
= new SimpleDateFormat("MM"+""+"dd"+"");
String date
= fm1.format(myDate);

5.字符串转换时间

String unixDate = "1259200976";
SimpleDateFormat fm1
= new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
SimpleDateFormat fm2
= new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
long unixLong = 0;
String date
= "";
unixLong
= Long.parseLong(unixDate) * 1000;
date
= fm1.format(unixLong);
date
= fm2.format(new Date(date));

部分引用http://www.cnblogs.com/tt_mc/archive/2010/08/25/1808326.html#2051365