Skip to content

Instantly share code, notes, and snippets.

@smallnewer
Created October 13, 2014 05:15
Show Gist options
  • Save smallnewer/8487f11848abe00d7427 to your computer and use it in GitHub Desktop.
Save smallnewer/8487f11848abe00d7427 to your computer and use it in GitHub Desktop.
解析ISO时间格式
/**
* 解析ISO时间
*
* 如果是两天内,格式为:“今天 08:08”"昨天 16:18"
* 如果是两天前,格式为:"月-日"
*
*/
public String ISOTimeFormat(String isotime) {
java.text.DateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.S'Z'");
sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
Date t = null;
try {
t = sdf.parse(isotime);
} catch (ParseException e) {
e.printStackTrace();
}
if (t == null) {
return null;
}
Calendar target = Calendar.getInstance();
target.setTime(t);
Calendar now = Calendar.getInstance();
int intervalMon = now.get(Calendar.MONTH) - target.get(Calendar.MONTH);
int intervalDay = now.get(Calendar.DAY_OF_MONTH) - target.get(Calendar.DAY_OF_MONTH);
if (intervalMon == 0 && intervalDay <= 1) {
if (intervalDay == 0) {
return "今天 " + target.get(Calendar.HOUR_OF_DAY) + ":" + target.get(Calendar.MINUTE);
}
if (intervalDay == 1) {
return "昨天 "+ target.get(Calendar.HOUR_OF_DAY) + ":" + target.get(Calendar.MINUTE);
}
}
java.text.DateFormat ret = new java.text.SimpleDateFormat("MM-dd");
return ret.format(target.getTime());
}
@SZooo
Copy link

SZooo commented Oct 13, 2014

学习了

@SZooo
Copy link

SZooo commented Oct 13, 2014

你在做镜像文件???

@smallnewer
Copy link
Author

没有~~

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment