package com.sd.sdactivity.utils;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/**
* 时间工具类
*/
public class DateUtil {
/**
* 获取 当天时间 的 0 点
* @return 当天 的0点时间
*/
public static Date getSameDayZeroTime() {
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
return calendar.getTime();
}
/**
* date格式的时间 转成 字符串
*
* @param date:时间
* @return 字符串的时间
*/
public static String dateToString(Date date) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return sdf.format(date);
} /**
* 字符串时间 转 date 格式
*
* @param time:字符串 时间
* @return date 时间
* @throws ParseException 转换错误将抛出异常
*/
public static Date stringToDate(String time) throws ParseException { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return sdf.parse(time);
}
/**
* 把时间转成几分钟前,几小时前的形式,这里暂时保留7天
*
* @param date:时间
* @return 传进来的时间距离当前时间 多久
*/
public static String getTimeFormatText(Date date) {
String timeFormatText = "";
long minute = 60 * 1000;// 1分钟
long hour = 60 * minute;// 1小时
long day = 24 * hour;// 1天
//实现 如果 时间 是短期内的话 就显示 为(1分钟前、一天前之类的) 7 天以上显示日期格式
if (date == null) {
return null;
}
long diff = new Date().getTime() - date.getTime();
long r;
if (diff > day) {
r = (diff / day);
if (r <= 7) {
timeFormatText = r + "天前";
}
} else if (diff > hour) {
r = (diff / hour);
timeFormatText = r + "个小时前";
} else if (diff > minute) {
r = (diff / minute);
timeFormatText = r + "分钟前";
} else {
timeFormatText = "刚刚";
}
return timeFormatText;
}
/**
* 把过期时间转成几小时、几天过期 的形式
*
* @param date:要转换的时间
* @return 转换之后的时间
*/
public static String getEndTimeFormatText(Date date) {
String endTimeFormatText = dateToString(date);
long minute = 60 * 1000;// 1分钟
long hour = 60 * minute;// 1小时
long day = 24 * hour;// 1天
//实现 如果 时间 是短期内的话 就显示 为(1分钟前、一天前之类的) 7 天以上显示日期格式
if (date == null) {
return null;
}
long diff = date.getTime() - new Date().getTime();
long r;
if (diff > day) {
r = (diff / day);
if (r <= 7) {
endTimeFormatText = r + "天后过期";
}
} else if (diff > hour) {
r = (diff / hour);
endTimeFormatText = r + "个小时后过期";
} else if (diff > minute) {
r = (diff / minute);
endTimeFormatText = r + "分钟后过期";
} else {
endTimeFormatText = "已过期";
}
return endTimeFormatText;
}
/**
* 验证时间是否大于当前 时间
*
* @param endTime:时间
* @return 是否过期
*/ public static boolean isEndTime(Date endTime) {
boolean isAppEndTime = true;
if (endTime != null) {
//判断是否过期
if (endTime.getTime() >= new Date().getTime()) {
isAppEndTime = false;
}
}
return isAppEndTime;
}
/**
* 返回服务器当前时间
*
* @return 服务器当前时间
*/
public static Date now() {
return new Date();
}
}
DateUtil 时间工具类
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
推荐阅读更多精彩内容
- 干货要点: 1、叙述、描写和说明是三种记“实”的能力。 2、议论和抒情是两种写“虚”的能力。 3、根据表达的中心,...
- 一:实现onKeyDown()方法。根据时间,具体代码如下。 二:实现onBackPressed()方法。根据点击...