package com.tejia.lijin.app.util;
|
|
import java.text.SimpleDateFormat;
|
import java.util.Date;
|
|
/**
|
* -
|
* 判断时间 昨天 今天 前天
|
*/
|
public class TimeJudge {
|
public static String showDate(Date date, String pattern) {
|
String dateStr = format(date, pattern);
|
String year = dateStr.substring(0, 4);
|
Long yearNum = Long.parseLong(year);
|
int month = Integer.parseInt(dateStr.substring(5, 7));
|
int day = Integer.parseInt(dateStr.substring(8, 10));
|
String hour = dateStr.substring(11, 13);
|
String minute = dateStr.substring(14, 16);
|
|
long addtime = date.getTime();
|
long today = System.currentTimeMillis();//当前时间的毫秒数
|
Date now = new Date(today);
|
String nowStr = format(now, pattern);
|
int nowDay = Integer.parseInt(nowStr.substring(8, 10));
|
String result = "";
|
long l = today - addtime;//当前时间与给定时间差的毫秒数
|
long days = l / (24 * 60 * 60 * 1000);//这个时间相差的天数整数,大于1天为前天的时间了,小于24小时则为昨天和今天的时间
|
long hours = (l / (60 * 60 * 1000) - days * 24);//这个时间相差的减去天数的小时数
|
long min = ((l / (60 * 1000)) - days * 24 * 60 - hours * 60);//
|
long s = (l / 1000 - days * 24 * 60 * 60 - hours * 60 * 60 - min * 60);
|
if (days > 0) {
|
if (days > 0 && days < 2) {
|
// result = "前天" + hour + "点" + minute + "分";
|
result = "前天";
|
} else if (days > 1 && days < 10) {
|
// result = "前天" + hour + "点" + minute + "分";
|
result = days + "天前 " + hour + "." + minute;
|
} else {
|
// result = yearNum % 100 + "年" + month + "月 " + day + "日" + hour + "点" + minute + "分";
|
result = yearNum % 100 + "." + month + ". " + day + "." + hour + ":" + minute;
|
}
|
} else if (hours > 0) {
|
if (day != nowDay) {
|
// result = "昨天" + hour + "点" + minute + "分";
|
result = "昨天" + hour + "." + minute;
|
} else {
|
// result = hours + "小时 前";
|
result = "今天" + format2(date, null);
|
}
|
} else if (min > 0) {
|
if (min > 0 && min < 10) {
|
// result = "刚刚";
|
// result = "0" + min + "." + s;
|
result = "今天" + format2(date, null);
|
} else {
|
// result = min + "分 前";
|
// result = min + "." + s;
|
result = "今天" + format2(date, null);
|
}
|
} else {
|
// result = s + "秒 前";
|
result = "今天" + format2(date, null);
|
}
|
return result;
|
}
|
|
/**
|
* 日期格式化
|
*
|
* @param date 需要格式化的日期
|
* @param pattern 时间格式,如:yyyy-MM-dd HH:mm:ss
|
* @return 返回格式化后的时间字符串
|
*/
|
public static String format(Date date, String pattern) {
|
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
|
return sdf.format(date);
|
}
|
|
/**
|
* 日期格式化
|
*
|
* @param date 需要格式化的日期
|
* @param pattern 时间格式,如: HH:mm
|
* @return 返回格式化后的时间字符串
|
*/
|
public static String format2(Date date, String pattern) {
|
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
|
return sdf.format(date);
|
}
|
|
/**
|
* 获取 当前时间 对比 存入时间之间的 天数
|
*
|
* @param date
|
* @param pattern
|
* @return
|
*/
|
public static int getday(Date date, String pattern) {
|
String dateStr = format(date, pattern);
|
String year = dateStr.substring(0, 4);
|
Long yearNum = Long.parseLong(year);
|
int month = Integer.parseInt(dateStr.substring(5, 7));
|
int day = Integer.parseInt(dateStr.substring(8, 10));
|
String hour = dateStr.substring(11, 13);
|
String minute = dateStr.substring(14, 16);
|
|
long addtime = date.getTime();
|
long today = System.currentTimeMillis();//当前时间的毫秒数
|
Date now = new Date(today);
|
String nowStr = format(now, pattern);
|
int nowDay = Integer.parseInt(nowStr.substring(8, 10));
|
String result = "";
|
long l = today - addtime;//当前时间与给定时间差的毫秒数
|
long days = l / (24 * 60 * 60 * 1000);//这个时间相差的天数整数,大于1天为前天的时间了,小于24小时则为昨天和今天的时间
|
// long hours = (l / (60 * 60 * 1000) - days * 24);//这个时间相差的减去天数的小时数
|
// long min = ((l / (60 * 1000)) - days * 24 * 60 - hours * 60);//
|
// long s = (l / 1000 - days * 24 * 60 * 60 - hours * 60 * 60 - min * 60);
|
if (days > 0) {
|
return (int) days;
|
} else {
|
return 0;
|
}
|
}
|
|
/**
|
* 获取 当前时间 对比 存入时间
|
* 返回 天 时 分 秒
|
*
|
* @param date
|
* @param pattern
|
* @return
|
*/
|
public static int[] getTimeGroup(Date date, String pattern) {
|
|
long addtime = date.getTime();
|
long today = System.currentTimeMillis();//当前时间的毫秒数
|
long l = today - addtime;//当前时间与给定时间差的毫秒数
|
long days = l / (24 * 60 * 60 * 1000);//这个时间相差的天数整数,大于1天为前天的时间了,小于24小时则为昨天和今天的时间
|
long hours = (l / (60 * 60 * 1000) - days * 24);//这个时间相差的减去天数的小时数
|
long min = ((l / (60 * 1000)) - days * 24 * 60 - hours * 60);//
|
long s = (l / 1000 - days * 24 * 60 * 60 - hours * 60 * 60 - min * 60);
|
|
return new int[]{(int) days, (int) hours, (int) min, (int) s};
|
}
|
|
/**
|
* 获取 当前时间 对比 存入时间
|
* 返回 天 时 分 秒
|
*
|
* @param date
|
* @return
|
*/
|
public static long[] getTimeSecondTransformation(long date) {
|
// Log.e("eee", "getTimeSecondTransformation: " + date);
|
long days = date / (24 * 60 * 60 * 1000);//这个时间相差的天数整数,大于1天为前天的时间了,小于24小时则为昨天和今天的时间
|
long hours = (date / (60 * 60 * 1000) - days * 24);//这个时间相差的减去天数的小时数
|
long min = ((date / (60 * 1000)) - days * 24 * 60 - hours * 60);//
|
long s = (date / 1000 - days * 24 * 60 * 60 - hours * 60 * 60 - min * 60);
|
return new long[]{days, hours, min, s};
|
}
|
}
|