Created
August 6, 2012 11:26
-
-
Save fankay/3273719 to your computer and use it in GitHub Desktop.
Java中友好的时间显示
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.kaishengit.util; | |
import java.text.ParseException; | |
import java.text.SimpleDateFormat; | |
import java.util.Date; | |
public class DateUtil { | |
public static String getNiceDate(String dateString) { | |
String result = dateString; | |
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); | |
try { | |
Date date = df.parse(dateString); | |
long time = System.currentTimeMillis() - date.getTime(); | |
time = time / 1000; | |
if(time < 10) { | |
result = "刚刚"; | |
} else if(time >= 10 && time < 60) { | |
result = "一分钟之内"; | |
} else if(time >= 60 && time < 3600) { | |
result = "一小时之内"; | |
} else if(time >= 3600 && time < 86400) { | |
result = "今天"; | |
} | |
} catch (ParseException e) { | |
e.printStackTrace(); | |
} | |
return result; | |
} | |
public static void main(String[] args) { | |
System.out.println(getNiceDate("2012-08-06 18:25:34")); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
3Q~ man!