//工具类:
/** * @author CJF */ public class DateTimeUtil { private volatile static DateTimeUtil dateTimeUtil = null; private Date date = new Date(); @SuppressLint("SimpleDateFormat") private final SimpleDateFormat simpleDateFormat = new SimpleDateFormat(); public static DateTimeUtil getInstance() { if (null == dateTimeUtil) { synchronized (DateTimeUtil.class) { if (null == dateTimeUtil) { dateTimeUtil = new DateTimeUtil(); } } } return dateTimeUtil; } /** * 获取当前系统时间戳 * * @return */ public long getNowLongTime() { return System.currentTimeMillis(); } /** * 根据时间毫秒数格式化时间 * * @param time * @param style * @return */ public String formatDateTime(long time, int style) { date.setTime(time); switch (style) { case 0: simpleDateFormat.applyPattern("yyyy-MM-dd HH:mm:ss"); break; case 1: simpleDateFormat.applyPattern("yyyy/MM/dd HH:mm:ss"); break; case 2: simpleDateFormat.applyPattern("MM-dd HH:mm"); break; case 3: simpleDateFormat.applyPattern("MM-dd HH:mm:ss"); break; case 4: simpleDateFormat.applyPattern("M-d"); break; case 5: simpleDateFormat.applyPattern("M-d HH:mm:ss"); break; case 6: simpleDateFormat.applyPattern("mm:ss"); break; case 7: simpleDateFormat.applyPattern("yyyy/M/d"); break; case 8: simpleDateFormat.applyPattern("yyyy/MM/dd HH:mm"); break; case 9: simpleDateFormat.applyPattern("M/d HH:mm"); break; case 10: simpleDateFormat.applyPattern("yyyy年MM月"); break; case 11: simpleDateFormat.applyPattern("HH:mm:ss"); break; case 12: simpleDateFormat.applyPattern("M月d日"); break; case 13: simpleDateFormat.applyPattern("HH:mm"); break; case 14: simpleDateFormat.applyPattern("yyyy-MM-dd"); break; case 15: simpleDateFormat.applyPattern("yyyy-MM-dd HH:mm"); break; case 16: simpleDateFormat.applyPattern("M月d日 HH:mm"); break; default: simpleDateFormat.applyPattern("yyyy-MM-dd HH:mm:ss"); break; } return simpleDateFormat.format(date); } /** * 将2013:10:08 11:48:07如此格式的时间,转化为毫秒数 * * @param datetime 字符串时间 * @return 毫秒数 */ public long dateTimeToMs(String datetime) { if (TextUtils.isEmpty(datetime)) { return 0; } try { simpleDateFormat.applyPattern("yyyy-MM-dd HH:mm:ss"); date = simpleDateFormat.parse(datetime); } catch (ParseException e) { e.printStackTrace(); return 0; } return date.getTime(); } /** * 将20131008114807如此格式的时间,转化为毫秒数 * * @param datetime 字符串时间 * @return 毫秒数 */ public long dateTime2Ms(String datetime) { if (TextUtils.isEmpty(datetime) || datetime.length() != 14) { return 0; } String stringBuilder = datetime.substring(0, 4) + ":" + datetime.substring(4, 6) + ":" + datetime.substring(6, 8) + " " + datetime.substring(8, 10) + ":" + datetime.substring(10, 12) + ":" + datetime.substring(12, 14); return dateTimeToMs(stringBuilder); } /** * 将2013-10-08如此格式的时间,转化为毫秒数 * * @param datetime 字符串时间 * @return 毫秒数 */ public long dateTime3ToMs(String datetime) { if (TextUtils.isEmpty(datetime)) { return 0; } try { simpleDateFormat.applyPattern("yyyy-MM-dd"); date = simpleDateFormat.parse(datetime); } catch (ParseException e) { e.printStackTrace(); return 0; } return date.getTime(); } /** * 计算两个日期相隔的天数. * * @param d1 * @param d2 * @return 返回两个日期相隔的天数, 如果是同一天返回0. */ public int getDaysBetween(Calendar d1, Calendar d2) { if (d1.after(d2)) { java.util.Calendar swap = d1; d1 = d2; d2 = swap; } int days = d2.get(java.util.Calendar.DAY_OF_YEAR) - d1.get(java.util.Calendar.DAY_OF_YEAR); int y2 = d2.get(java.util.Calendar.YEAR); if (d1.get(java.util.Calendar.YEAR) != y2) { d1 = (java.util.Calendar) d1.clone(); do { days += d1.getActualMaximum(java.util.Calendar.DAY_OF_YEAR); d1.add(java.util.Calendar.YEAR, 1); } while (d1.get(java.util.Calendar.YEAR) != y2); } return days; } /** * 根据文件路径,返回文件修改时间 * * @param path * @return */ public long getFileDate(String path) { File file = new File(path); return file.lastModified(); } /** * 将毫秒转化为时分秒,用于播放录音显示时长 * * @param timeLength * @return */ @SuppressLint("SimpleDateFormat") public String ssToTime(long timeLength) { SimpleDateFormat formatter = null; if (timeLength >= 3600000) { formatter = new SimpleDateFormat("HH:mm:ss"); } else { formatter = new SimpleDateFormat("mm:ss"); } formatter.setTimeZone(TimeZone.getTimeZone("GMT+00:00")); return formatter.format(timeLength); } /** * 获取当前年月日 * * @return */ public String getNowTime() { return formatDateTime(getNowLongTime(), 14); } /** * 获取今日时间戳(只精确到天) * * @return */ public long getTodayNowTime() { simpleDateFormat.applyPattern("yyyy-MM-dd"); date.setTime(getNowLongTime()); String format = simpleDateFormat.format(date); try { date = simpleDateFormat.parse(format); } catch (ParseException e) { e.printStackTrace(); } return date.getTime(); } /** * 获取当前时间的总分钟数 * * @return */ public int getCurrentMinute() { // 当前日期 Calendar cal = Calendar.getInstance(); // 获取小时 int hour = cal.get(Calendar.HOUR_OF_DAY); // 获取分钟 int minute = cal.get(Calendar.MINUTE); // 从0:00分开是到目前为止的分钟数 return hour * 60 + minute; } /** * 获取指定时间的总分钟数 * * @param time * @return */ public int getCurrentMinute(long time) { if (time == 0) { return 0; } // 当前日期 Calendar cal = Calendar.getInstance(); //设置时间 cal.setTime(new Date(time)); // 获取小时 int hour = cal.get(Calendar.HOUR_OF_DAY); // 获取分钟 int minute = cal.get(Calendar.MINUTE); // 从0:00分开是到目前为止的分钟数 return hour * 60 + minute; } /** * 获取指定时间的long值(精确到小时) * * @param time * @return */ public long getDateHour(long time) { simpleDateFormat.applyPattern("yyyy-MM-dd HH"); date.setTime(time); String format = simpleDateFormat.format(date); try { date = simpleDateFormat.parse(format); } catch (ParseException e) { e.printStackTrace(); } return date.getTime(); } /** * 获取到指定时间的long值(精确到天) * * @param time * @return */ public long getDate(long time) { simpleDateFormat.applyPattern("yyyy-MM-dd"); date.setTime(time); String format = simpleDateFormat.format(date); try { date = simpleDateFormat.parse(format); } catch (ParseException e) { e.printStackTrace(); } return date.getTime(); } /** * 获取增加指定天数后系统的时间long(精确到天) * * @param days * @return */ public long getIncreaseTime(int days) { //日期 Calendar c = Calendar.getInstance(); //日期增加到指定天数后 c.add(Calendar.DAY_OF_MONTH, days); long time = c.getTime().getTime(); long date = getDate(time); return date; } /** * 获取两个时间点,间隔多少分钟 * * @param startTime * @param endTime * @return */ public int timeInterval(long startTime, long endTime) { Date startDate = new Date(startTime); Date endDate = new Date(endTime); simpleDateFormat.applyPattern("yyyy-MM-dd HH:mm:ss"); try { startDate = simpleDateFormat.parse(simpleDateFormat.format(startDate)); endDate = simpleDateFormat.parse(simpleDateFormat.format(endDate)); } catch (ParseException e) { e.printStackTrace(); } Calendar calendar = Calendar.getInstance(); calendar.setTime(startDate); long time1 = calendar.getTimeInMillis(); calendar.setTime(endDate); long time2 = calendar.getTimeInMillis(); long betweenDays = (time2 - time1) / 1000 / 60; return Integer.parseInt(String.valueOf(betweenDays)); } /** * 判断当前总分钟对应的时间是否处于两个总分钟数对应的时间点之间 * * @param startMinutes * @param endMinutes * @return */ public boolean judgingTime(int startMinutes, int endMinutes) { //获取当前时间的总分钟数 int currentMinute = getCurrentMinute(); if (currentMinute >= startMinutes && currentMinute <= endMinutes) { return true; } else { return false; } } /** * 将18:00:00如此格式的时间 转化为今日指定时间的时间戳(精确到毫秒) * * @param time * @return */ public long getTodayAppointTime(String time) { String dateTime = getNowTime() + " " + time; simpleDateFormat.applyPattern("yyyy-MM-dd HH:mm:ss"); ParsePosition position = new ParsePosition(0); date = simpleDateFormat.parse(dateTime, position); return date.getTime(); } /** * 获取两个时间点,间隔多少分钟 * * @param startTime * @param endTime * @return */ public int timeIntervalinutes(long startTime, long endTime) { Date startDate = new Date(startTime); Date endDate = new Date(endTime); simpleDateFormat.applyPattern("yyyy-MM-dd HH:mm:ss"); try { startDate = simpleDateFormat.parse(simpleDateFormat.format(startDate)); endDate = simpleDateFormat.parse(simpleDateFormat.format(endDate)); } catch (ParseException e) { e.printStackTrace(); } Calendar calendar = Calendar.getInstance(); calendar.setTime(startDate); long time1 = calendar.getTimeInMillis(); calendar.setTime(endDate); long time2 = calendar.getTimeInMillis(); long betweenDays = (time2 - time1) / 1000 / 60; return Integer.parseInt(String.valueOf(betweenDays)); } /** * 获取今天周几 * * @return */ public String[] getWeek() { //week:星期- 星期二 ... simpleDateFormat.applyPattern("EEEE"); String week = simpleDateFormat.format(getNowLongTime()); String allWeekStr = "【6:星期日】【0:星期一】【1:星期二】【2:星期三】【3:星期四】【4:星期五】【5:星期六】"; // weekIndex: 1 2 ... // Calendar cal = Calendar.getInstance(); // int weekIndex = cal.get(Calendar.DAY_OF_WEEK) - 1; // 6/星期日 0/星期一 1/星期二 2/星期三 3/星期四 4/星期五 5/星期六 String[] strings = {"6", "0", "1", "2", "3", "4", "5"}; Calendar cal = Calendar.getInstance(); int weekIndex = cal.get(Calendar.DAY_OF_WEEK) - 1; String str = strings[weekIndex]; return new String[]{str, week, allWeekStr}; } /** * 时长转换 * * @param duration * @return */ public String timeCovert(int duration) { if (duration <= 0) { return "0秒"; } if (duration >= 60) { return (duration / 60) + "分" + ((duration % 60) < 10 ? "0" + (duration % 60) : (duration % 60)) + "秒"; } else { return duration + "秒"; } } /** * 判断时间是否是今日 * * @param time * @return */ public boolean isToday(long time) { return DateUtils.isToday(time); } /** * 转换为分钟 * * @param time * @return */ public int convertToMinutes(String time) { simpleDateFormat.applyPattern("HH:mm"); Date parse = null; try { parse = simpleDateFormat.parse(time); } catch (ParseException e) { e.printStackTrace(); } return (parse.getHours() * 60) + parse.getMinutes(); } /** * 获取N天前的日期或N天后日期(负数之前,正数之后) * * @param distanceDay 例如:-7/七天前日期 7/七天后日期 * @return */ public String getOldDate(int distanceDay) { Date beginDate = new Date(); Calendar date = Calendar.getInstance(); date.setTime(beginDate); date.set(Calendar.DATE, date.get(Calendar.DATE) + distanceDay); long time = date.getTime().getTime(); return formatDateTime(time, 14); } /** * 获取N天前的日期时间戳或N天后日期时间戳(负数之前,正数之后) * * @param distanceDay 例如:-7/七天前日期 7/七天后日期 * @return */ public long getOldTime(int distanceDay) { Date beginDate = new Date(); Calendar date = Calendar.getInstance(); date.setTime(beginDate); date.set(Calendar.DATE, date.get(Calendar.DATE) + distanceDay); return date.getTime().getTime(); } /** * 将秒数转为时分秒 * 00:00 * 00:00:00 * * @param second * @return */ public String getChange(int second) { String time; if (second < 60) { time = String.format("00:%02d", second % 60); } else if (second < 3600) { time = String.format("%02d:%02d", second / 60, second % 60); } else { time = String.format("%02d:%02d:%02d", second / 3600, second % 3600 / 60, second % 60); } return time; } /** * 根据秒数转化为时分秒 * second + "秒" * minute + "分" + second + "秒" * hour + "小时" + minute + "分" + second + "秒" * * @param second * @return */ public String getChangeTime(int second) { if (second < 60) { return second + "秒"; } if (second < 3600) { int minute = second / 60; second = second - minute * 60; return minute + "分" + second + "秒"; } int hour = second / 3600; int minute = (second - hour * 3600) / 60; second = second - hour * 3600 - minute * 60; return hour + "小时" + minute + "分" + second + "秒"; } /** * 判断如果是今天返回 "今天" 否则返回"M月d日 HH:mm" * * @param date * @return */ public String getChangeTodayDate(long date) { if (date == 0) { return ""; } if (DateUtils.isToday(date)) { return "今天"; } return formatDateTime(date, 16); } /** * 判断如果是今天返回 "今天"+HH:mm 否则返回"M月d日 HH:mm" * * @param date * @return */ public String getChangeTodayDateTime(long date) { if (date == 0) { return ""; } if (DateUtils.isToday(date)) { return "今天" + formatDateTime(date, 13); } return formatDateTime(date, 16); } /** * 时间戳转成提示性日期格式:今天、昨天、M月d日 * * @param date * @return */ public String getChangeDateTimeFormat(long date) { if (date == 0) { return ""; } if (DateUtils.isToday(date)) { return "今天"; } //现在时间 yyyy-MM-dd String today = formatDateTime(getNowLongTime(), 14); //昨天 yyyy-MM-dd Calendar cal = Calendar.getInstance(); cal.add(Calendar.DATE, -1); String yesterday = formatDateTime(cal.getTime().getTime(), 14); if (today.equals(yesterday)) { return "昨天"; } return formatDateTime(date, 12); } }
public static String getDateToString(long milSecond) {
//现在时间
String now = new SimpleDateFormat("yyyy-MM-dd ").format(milSecond);
//昨天
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, -1);
String yday = new SimpleDateFormat("yyyy-MM-dd ").format(cal.getTime());
//今天
Calendar c = Calendar.getInstance();
c.add(Calendar.DATE, 0);
String today = new SimpleDateFormat("yyyy-MM-dd ").format(c.getTime());
//其他时间段
Date date = new Date(milSecond);
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
if (now.equals(today)) {
return "今天";
} else if (now.equals(yday)) {
return "昨天";
} else {
return format.format(date);
}
}
//日期转换工具类:
public class DateTimeUtility {
/**
* 根据时间毫秒数格式化时间
*
* @param time 时间
* @return 格式化时间
*/
public static String formatDate(long time) {
return formatDate(time, 0);
}
/**
* 根据时间毫秒数格式化时间
* 0: "yyyy-MM-dd hh:mm:ss"
* 1: "yyyy/MM/dd hh:mm:ss"
* 2: "MM-dd HH:mm"
*
* @param time 时间
* @param style 样式
* @return 格式化时间
*/
public static String formatDateTime(long time, int style) {
Date date = new Date(time);
String strTime;
SimpleDateFormat df;
switch (style) {
case 0:
df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
break;
case 1:
df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
break;
case 2:
df = new SimpleDateFormat("MM-dd HH:mm");
break;
case 3:
df = new SimpleDateFormat("MM-dd HH:mm:ss");
break;
case 4:
df = new SimpleDateFormat("M-d");
break;
case 5:
df = new SimpleDateFormat("M-d HH:mm:ss");
break;
case 6:
df = new SimpleDateFormat("mm:ss");
break;
case 7:
df = new SimpleDateFormat("yyyy/M/d");
break;
case 8:
df = new SimpleDateFormat("yyyy/MM/dd HH:mm");
break;
case 9:
df = new SimpleDateFormat("M/d HH:mm");
break;
case 10:
df = new SimpleDateFormat("yyyy年MM月");
break;
case 11:
df = new SimpleDateFormat("HH:mm:ss");
break;
case 12:
df = new SimpleDateFormat("M月d日");
break;
case 13:
df = new SimpleDateFormat("M-d");
break;
case 14:
df = new SimpleDateFormat("HH:mm");
break;
default:
df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
break;
}
strTime = df.format(date);
return strTime;
}
/**
* 根据时间毫秒数格式化时间
* 0: "yyyy-MM-dd"
* 1: "yyyy/MM/dd"
* 2: "MM/dd yyyy"
*
* @param time 时间
* @param style 样式
* @return 格式化时间
*/
public static String formatDate(long time, int style) {
String strTime;
SimpleDateFormat df;
switch (style) {
case 0:
df = new SimpleDateFormat("yyyy-MM-dd");
break;
case 1:
df = new SimpleDateFormat("yyyy/MM/dd");
break;
case 2:
df = new SimpleDateFormat("MM/dd yyyy");
break;
case 3:
df = new SimpleDateFormat("MM/dd HH:mm");
break;
case 4:
df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
break;
default:
df = new SimpleDateFormat("yyyy-MM-dd");
break;
}
strTime = df.format(new Date(time));
return strTime;
}
/**
* 计算两个日期相隔的天数.
*
* @param d1
* @param d2
* @return 返回两个日期相隔的天数, 如果是同一天返回0.
*/
public static int getDaysBetween(java.util.Calendar d1, java.util.Calendar d2) {
if (d1.after(d2)) {
java.util.Calendar swap = d1;
d1 = d2;
d2 = swap;
}
int days = d2.get(java.util.Calendar.DAY_OF_YEAR) - d1.get(java.util.Calendar.DAY_OF_YEAR);
int y2 = d2.get(java.util.Calendar.YEAR);
if (d1.get(java.util.Calendar.YEAR) != y2) {
d1 = (java.util.Calendar) d1.clone();
do {
days += d1.getActualMaximum(java.util.Calendar.DAY_OF_YEAR);
d1.add(java.util.Calendar.YEAR, 1);
}
while (d1.get(java.util.Calendar.YEAR) != y2);
}
return days;
}
public static String formatMsTime(long lt) {
Date msgTime = new Date(lt);
return msgTime.toString().substring(11, 19);
}
public static String formatMsShortTime(long lt) {
Date msgTime = new Date(lt);
return msgTime.toString().substring(11, 16);
}
public static String formatMsDate(long lt) {
Date msgTime = new Date(lt);
int year = msgTime.getYear() + 1900;
int month = msgTime.getMonth();
int day = msgTime.getDate();
return year + "-" + (month + 1) + "-" + (day);
}
public static String formatMsDate(long lt, String separator) {
Date msgTime = new Date(lt);
int year = msgTime.getYear() + 1900;
int month = msgTime.getMonth();
int day = msgTime.getDate();
return year + separator + (month + 1) + separator + (day);
}
/**
* 根据文件路径
* 返回文件修改时间
*
* @param path 文件路径
* @return 文件修改时间
*/
public static long getFileDate(String path) {
File file = new File(path);
return file.lastModified();
}
/**
* 将2013:10:08 11:48:07如此格式的时间
* 转化为毫秒数
*
* @param datetime 字符串时间
* @return 毫秒数
*/
public static long dateTimeToMS(String datetime) {
if (TextUtils.isEmpty(datetime)) return 0;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy:MM:dd HH:mm:ss");
final Date parse;
try {
parse = sdf.parse(datetime);
} catch (ParseException e) {
e.printStackTrace();
return 0;
}
return parse.getTime();
}
/**
* 将2013:10:08 11:48:07如此格式的时间
* 转化为毫秒数
*
* @param datetime 字符串时间
* @return 毫秒数
*/
public static long dateTimeToMS(String datetime, int style) {
if (TextUtils.isEmpty(datetime)) return 0;
SimpleDateFormat sdf = null;
switch (style) {
case 0:
sdf = new SimpleDateFormat("yyyy:MM:dd HH:mm:ss");
break;
case 1:
sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm");
break;
case 2:
sdf = new SimpleDateFormat("yyyy年MM月");
break;
}
final Date parse;
try {
parse = sdf.parse(datetime);
} catch (ParseException e) {
e.printStackTrace();
return 0;
}
return parse.getTime();
}
/**
* 将2013:10:08 11:48:07如此格式的时间
* 转化为毫秒数
*
* @param datetime 字符串时间
* @return 毫秒数
*/
public static long dateTimeToMSs(String datetime) {
if (TextUtils.isEmpty(datetime)) return 0;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm");
final Date parse;
try {
parse = sdf.parse(datetime);
} catch (ParseException e) {
e.printStackTrace();
return 0;
}
return parse.getTime();
}
/**
* 将20131008114807如此格式的时间
* 转化为毫秒数
*
* @param datetime 字符串时间
* @return 毫秒数
*/
public static long dateTime2MS(String datetime) {
if (TextUtils.isEmpty(datetime) || datetime.length() != 14) {
return 0;
}
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(datetime.substring(0, 4));
stringBuilder.append(":");
stringBuilder.append(datetime.substring(4, 6));
stringBuilder.append(":");
stringBuilder.append(datetime.substring(6, 8));
stringBuilder.append(" ");
stringBuilder.append(datetime.substring(8, 10));
stringBuilder.append(":");
stringBuilder.append(datetime.substring(10, 12));
stringBuilder.append(":");
stringBuilder.append(datetime.substring(12, 14));
return dateTimeToMS(stringBuilder.toString(), 0);
}
public static Calendar getNextTime() {
Calendar cal = Calendar.getInstance();
cal.setTime(new java.util.Date());
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
cal.add(Calendar.DAY_OF_MONTH, 1);
return cal;
}
/**
* 将毫秒转化为时分秒,用于播放录音显示时长
*
* @param timeLength
* @return
*/
public static String ssToTime(long timeLength) {
SimpleDateFormat formatter = new SimpleDateFormat("mm:ss");
// SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
formatter.setTimeZone(TimeZone.getTimeZone("GMT+00:00"));
String hms = formatter.format(timeLength);
return hms;
}
/**
* 方法名:getNowTime()
* 功 能:获取当前年月日
* 参 数:无
* 返回值:String
*/
public static String getNowTime() {
// SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss"); //当前年月日 时分秒
// SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日");
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date date = new Date(System.currentTimeMillis());
return simpleDateFormat.format(date);
}
/**
* 方法名:getNowLongTime()
* 功 能:获取当前系统时间戳
* 参 数:无
* 返回值:long
*/
public static long getNowLongTime() {
return System.currentTimeMillis();
}
/**
* 方法名:getNowLongDateTime()
* 功 能:获取当前时间戳(只精确到天)
* 参 数:无
* 返回值:long
*/
public static long getNowLongDateTime() {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date date = new Date(System.currentTimeMillis());
String format = simpleDateFormat.format(date);
Date parse = null;
try {
parse = simpleDateFormat.parse(format);
} catch (ParseException e) {
e.printStackTrace();
}
return parse.getTime();
}
/**
* 方法名:getCurrentMinute()
* 功 能:获取当前时间的总分钟数
* 参 数:无
* 返回值:int
*/
public static int getCurrentMinute() {
Calendar cal = Calendar.getInstance();// 当前日期
int hour = cal.get(Calendar.HOUR_OF_DAY);// 获取小时
int minute = cal.get(Calendar.MINUTE);// 获取分钟
int minuteOfDay = hour * 60 + minute;// 从0:00分开是到目前为止的分钟数
return minuteOfDay;
}
/**
* 方法名:getDate(long time)
* 功 能:获取到指定时间的long值(精确到天)
* 参 数:long time
* 返回值:long
*/
public static long getDate(long time) {
Date date = new Date(time);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
String format = simpleDateFormat.format(date);
Date parse = null;
try {
parse = simpleDateFormat.parse(format);
} catch (ParseException e) {
e.printStackTrace();
}
return parse.getTime();
}
/**
* 方法名:getIncreaseTime(int days)
* 功 能:获取增加指定天数后系统的时间long
* 参 数:int days
* 返回值:long
*/
public static long getIncreaseTime(int days) {
Calendar c = Calendar.getInstance();//日期
c.add(Calendar.DAY_OF_MONTH, days);//日期增加到指定天数后
long time = c.getTime().getTime();
long date = DateTimeUtility.getDate(time);
return date;
}
/**
* 方法名: timeInterval(long startTime, long endTime)
* 功 能:获取两个时间点,间隔多少分钟
* 参 数:daysBetween(Date startDate, Date endDate)
* 返回值:int
*/
public static int timeInterval(long startTime, long endTime) {
Date startDate = new Date(startTime);
Date endDate = new Date(endTime);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
startDate = sdf.parse(sdf.format(startDate));
endDate = sdf.parse(sdf.format(endDate));
} catch (ParseException e) {
e.printStackTrace();
}
Calendar calendar = Calendar.getInstance();
calendar.setTime(startDate);
long time1 = calendar.getTimeInMillis();
calendar.setTime(endDate);
long time2 = calendar.getTimeInMillis();
long betweenDays = (time2 - time1) / 1000 / 60;
return Integer.parseInt(String.valueOf(betweenDays));
}
/**
* 方法名:judgingTime(int startMinutes, int endMinutes)
* 功 能:判断当前总分钟对应的时间是否处于两个总分钟数对应的时间点之间
* 参 数:int startMinutes, int endMinutes
* 返回值:boolean
*/
public static boolean judgingTime(int startMinutes, int endMinutes) {
int currentMinute = DateTimeUtility.getCurrentMinute();//获取当前时间的总分钟数
if (currentMinute >= startMinutes && currentMinute <= endMinutes) {
return true;
} else {
return false;
}
}
/**
* 方法名:getTodayAppointTime(String time)
* 功 能:获取今日指定时间的时间戳
* 参 数:String time
* 返回值:long
*/
public static long getTodayAppointTime(String time) {
String dateTime = getNowTime() + " " + time;
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
ParsePosition position = new ParsePosition(0);
Date date = simpleDateFormat.parse(dateTime, position);
return date.getTime();
}
public static String getCrmTimeData(String date) {
if (TextUtils.isEmpty(date)) {
return "";
}
long time = Long.valueOf(date);
String today = DateTimeUtility.formatDateTime(System.currentTimeMillis(), 9);
String timeStr = DateTimeUtility.formatDateTime(time, 9);
if (today.split(" ")[0].equals(timeStr.split(" ")[0])) {
return "今天 " + timeStr.split(" ")[1];
} else {
return timeStr.replace("/", "月").replaceFirst(" ", "日 ");
}
}
public static String getCrmData(String date) {
if (TextUtils.isEmpty(date)) {
return "";
}
long time = Long.valueOf(date);
String timeStr = DateTimeUtility.formatDateTime(time, 13);
return timeStr;
}
public static String getCrmTimeFlag(String date) {
long time = Long.valueOf(date);
String timeStr = DateTimeUtility.formatDateTime(time, 12);
//时间戳转成提示性日期格式(昨天、今天……)
String dateToString = getDateToString(Long.valueOf(date));
if (dateToString.equals("今天")) {
return dateToString;
} else if (dateToString.equals("昨天")) {
return dateToString;
} else {
return timeStr;
}
}
/**
* 方法名:getDateToString(long milSecond)
* 功 能:时间戳转成提示性日期格式(昨天、今天……)
* 参 数:long milSecond
* 返回值:String
*/
public static String getDateToString(long milSecond) {
//现在时间
String now = new SimpleDateFormat("yyyy-MM-dd ").format(milSecond);
//昨天
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, -1);
String yday = new SimpleDateFormat("yyyy-MM-dd ").format(cal.getTime());
//今天
Calendar c = Calendar.getInstance();
c.add(Calendar.DATE, 0);
String today = new SimpleDateFormat("yyyy-MM-dd ").format(c.getTime());
//其他时间段
Date date = new Date(milSecond);
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
if (now.equals(today)) {
return "今天";
} else if (now.equals(yday)) {
return "昨天";
} else {
return format.format(date);
}
}
public static String getCrmDataTime(String date) {
if (TextUtils.isEmpty(date)) {
return "";
}
long time = Long.valueOf(date);
String today = DateTimeUtility.formatDateTime(System.currentTimeMillis(), 9);
String timeStr = DateTimeUtility.formatDateTime(time, 9);
if (today.split(" ")[0].equals(timeStr.split(" ")[0])) {
return "今天";
} else {
return timeStr.replace("/", "月").replaceFirst(" ", "日 ");
}
}
public static String timeCovert(String timeLengthStr) {
if (TextUtils.isEmpty(timeLengthStr)) {
return "";
}
try {
int timeLength = Integer.valueOf(timeLengthStr);
if (timeLength >= 60) {
return (int) (timeLength / 60) + "分" + (timeLength % 60) + "秒";
} else if (timeLength > 0 && timeLength < 60) {
return timeLength + "秒";
}
} catch (Exception e) {
e.printStackTrace();
return "";
}
return "";
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/118294.html