一,在android中时间的形式包括:
1、时间戳 (1441355176150)
距1970年1月1日的毫秒数,一般作为对数据唯一性的一种判断依据,避免了重复修改数据所带来的错误。
获取方法(单位毫秒):
Long time = Calendar.getInstance().getTimeInMillis();
Long time = new Date().getTime();
Long time = System.currentTimeMillis();
2、Date时间格式

3、时间的字符串格式(20150903112723)
直接将时间的各个组成部分拼成String类型组成。例如:2017年03月03日11时27分23秒 ,2017-03-03 11:27:23都是时间的字符串形式。
二,项目中的时间格式以及时间差(几年前,几个月前,几天前,几小时前,刚刚)
public static String getTimeInterval(String inputTime) {
// inputTime传入的时间格式必须类似于“yyyy-MM-dd HH:mm:ss”这样的格式
if (inputTime.length() != 19) {
return inputTime;
}
String result = null;
try {
SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// ParsePosition 是指从哪个位置开始索引
ParsePosition pos = new ParsePosition(0);
Date d1 = sd.parse(inputTime, pos);
// 用现在距离1970年的时间间隔new
// Date().getTime()减去以前的时间距离1970年的时间间隔d1.getTime()得出的就是以前的时间与现在时间的时间间隔
long time = new Date().getTime() - d1.getTime();// 得出的时间间隔是毫秒
if (time / 1000 < 60) {
// 如果时间间隔小于等于60秒则显示“刚刚”
result = "刚刚";
} else if (time / 60000 < 60) {
// 如果时间间隔小于60分钟则显示多少分钟前
result = time / 60000 + "分钟前";
} else if (time / 3600000 < 24) {
// 如果时间间隔小于24小时则显示多少小时前
result = time / 3600000 + "小时前";
} else if (time / 86400000 < 2) {
// 如果时间间隔小于2天则显示昨天(3600000*24)
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
result = sdf.format(d1.getTime());
result = "昨天" +result;
} else if (time / 86400000 < 3) {
// 如果时间间隔小于3天则显示前天
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
result = sdf.format(d1.getTime());
result = "前天" +result;
} else if (time / 86400000 < 30) {
// 如果时间间隔小于30天则显示多少天前
result = time / 86400000 + "天前";
} else if (time / 86400000 < 60) {
result = "1个月前";
} else if (time / 86400000 < 90) {
result = "2个月前";
} else if (time / 86400000 < 120) {
result = "3个月前";
} else if (time / 86400000 < 150) {
result = "4个月前";
} else if (time / 86400000 < 180) {
result = "5个月前";
} else if (time / 86400000 < 210) {
result = "6个月前";
} else if (time / 86400000 < 240) {
result = "7个月前";
} else if (time / 86400000 < 270) {
result = "8个月前";
} else if (time / 86400000 < 300) {
result = "9个月前";
} else if (time / 86400000 < 330) {
result = "10个月前";
} else if (time / 86400000 < 360) {
result = "11个月前";
} else if (time / 86400000 < 720) {
result = "1年前";
} else if (time / 86400000 < 1080) {
result = "2年前";
} else {
// 大于2年,显示年月日时间
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
result = sdf.format(d1.getTime());
}
} catch (Exception e) {
return inputTime;
}
return result;
}