获取系统日期时间,数据返回类型:Date
import java.util.Date;
public class DateUtils{
public static Date getNowDate() {
return new Date();
}
}
字符串日期时间转换成Date类型的日期时间,数据返回类型:Date
import java.util.Date;
public class DateUtils{
public static Date getDateTime(String dateTime) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date Date = new Date();
try {
Date = sdf.parse(dateTime);
} catch (ParseException e) {
e.printStackTrace();
}
return Date;
}
}
获取系统日期时间,数据返回类型:Date
import java.util.Date;
import java.time.LocalDateTime;
import java.time.ZoneId;
public class DateUtils{
public static Date getCurrentDate() {
return Date.from(LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant());
}
}
获取系统日期时间,数据返回类型:LocalDateTime
import java.time.LocalDateTime;
public class DateUtils{
public static LocalDateTime getCurrentLocalDateTime() {
return LocalDateTime.now();
}
}
获取日期文件路径,即:年/月/日,如:2018/08/08,数据返回类型:String
import java.util.Date;
import org.apache.commons.lang3.time.DateFormatUtils;
public class DateUtils{
public static final String getDatePath() {
return DateFormatUtils.format(new Date(), "yyyy/MM/dd");
}
}
获取系统日期时间,字符串格式:年-月-日,如:2018-08-08,数据返回类型:String
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class DateUtils{
public final static String DATE_PATTERN = "yyyy-MM-dd";
public static String getCurrentYMD() {
return LocalDateTime.now().format(DateTimeFormatter.ofPattern(DATE_PATTERN));
}
}
获取系统日期时间,字符串格式:年-月-日 时:分:秒,如:2018-08-08 08:08:08,数据返回类型:String
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class DateUtils{
public final static String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";
public static String getCurrentYMDHMS() {
return LocalDateTime.now().format(DateTimeFormatter.ofPattern(DATE_TIME_PATTERN));
}
}
获取系统日期时间,字符串格式:年月日时分秒毫秒,如:20180808080808888,数据返回类型:String
import java.util.Calendar;
public class DateUtils{
public static String getDateTimeStr() {
Calendar Cld = Calendar.getInstance();
int YY = Cld.get(Calendar.YEAR);
String MM = Cld.get(Calendar.MONTH) + 1 + "";
if (Cld.get(Calendar.MONTH) + 1 < 10) {
MM = "0" + MM;
}
String DD = Cld.get(Calendar.DATE) + "";
if (Cld.get(Calendar.DATE) < 10) {
DD = "0" + DD;
}
String HH = Cld.get(Calendar.HOUR_OF_DAY) + "";
if (Cld.get(Calendar.HOUR_OF_DAY) < 10) {
HH = "0" + HH;
}
String mm = Cld.get(Calendar.MINUTE) + "";
if (Cld.get(Calendar.MINUTE) < 10) {
mm = "0" + mm;
}
String SS = Cld.get(Calendar.SECOND) + "";
if (Cld.get(Calendar.SECOND) < 10) {
SS = "0" + SS;
}
int MI = Cld.get(Calendar.MILLISECOND);
return YY + MM + DD + HH + mm + SS + MI;
}
}
获取上个月最后一天的最后一秒,数据返回类型:Date
import java.util.Calendar;
public class DateUtils{
public static Date getLastMonthLastDay() {
Calendar calendar = Calendar.getInstance();
int month = calendar.get(Calendar.MONTH);
calendar.set(Calendar.MONTH, month - 1);
calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
calendar.set(Calendar.HOUR_OF_DAY, calendar.getActualMaximum(Calendar.HOUR_OF_DAY));
calendar.set(Calendar.MINUTE, calendar.getActualMaximum(Calendar.MINUTE));
calendar.set(Calendar.SECOND, calendar.getActualMaximum(Calendar.SECOND));
return calendar.getTime();
}
}
计算两个时间的时间差(向上取整,单位:天),数据返回类型:int
import java.util.Date;
public class DateUtils{
public static int getDiffDays(Date yesterday, Date today) {
if (yesterday == null || today== null) {
throw new NullPointerException("by calculating the difference between two [java.util.Date]");
}
double yDouble = today.getTime() - yesterday.getTime();
double oneDouble = 24 * 60 * 60 * 1000;//一天的时间
double days = yDouble / oneDouble;
return (int) Math.ceil(days);
}
}
计算两个时间的时间差(向上取整,单位:天),只计算工作日(24小时为一天),数据返回类型:int
import java.util.Date;
import java.util.Calendar;
public class DateUtils{
public static int getDiffDayWithOutWeekends(Date yesterday, Date today) {
Calendar calendar = Calendar.getInstance();
if (yesterday == null || today== null) {
throw new NullPointerException("by calculating the difference between two [java.util.Date]");
}
double yDouble = today.getTime() - yesterday.getTime();
double oneDouble = 24 * 60 * 60 * 1000;//一天的时间
int days = (int) (yDouble / oneDouble);
int sum = 0;
for (int i = 0; i < days; i++) {
calendar.setTime(now);
calendar.add(Calendar.DAY_OF_YEAR, -i);
if (isWeekend(calendar)) {
sum++;
}
}
// subtract weekends
days = days - sum;
return days;
}
}
判断一个日期是否是周末(星期六、星期日),数据返回类型:boolean
import java.util.Date;
import java.util.Calendar;
public class DateUtils{
public static boolean isWeekend(Date now) {
Calendar cal = Calendar.getInstance();
cal.setTime(Objects.requireNonNull(now));
int week = cal.get(Calendar.DAY_OF_WEEK) - 1;
return week == 6 || week == 0;
}
}
根据时间偏移量得到新的时间,数据返回类型:String
import java.util.Date;
import java.util.Calendar;
public class DateUtils{
/**
* 根据时间偏移量得到新的时间
*
* @param way (1-代表天数,2-代表月数)
* @param time (时间偏移量,整数往后推,负数往前移动)
* @param type (格式化时间类型,1-"yyyy-MM-dd",2-"yyyy-MM-dd HH:mm:ss")
* @param datetime (传入时间)
* @return 得到新的时间
*/
public static String getDate(int way, int time, int type, Date datetime) {
Date date = new Date(); // 获取时间
if (datetime != null) {
date = datetime;
}
Calendar calendar = new GregorianCalendar();
calendar.setTime(date);
if (way == 1) {
calendar.add(Calendar.DATE, time); // 把日期往后增加天数,整数往后推,负数往前移动
}
if (way == 2) {
calendar.add(Calendar.MONTH, time); // 把日期往后增加月数,整数往后推,负数往前移动
}
date = calendar.getTime(); // 这个时间就是日期往后推一天的结果
String dateString = "";
if (type == 1) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
dateString = formatter.format(date);
} else if (type == 2) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
dateString = formatter.format(date);
}
return dateString;
}
}
计算某个日期加上N个月之后的日期
import java.util.Date;
import java.util.Calendar;
public class DateUtils{
/**
* 计算某个日期加上N个月之后的日期
*
* @param date 当前时间
* @param moth 月数
* @return 结果
*/
public static String dataAddMoth(Date date, int moth) {
SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.MONTH, moth);
return fmt.format(calendar.getTime());
}
}
传入字符串格式的日期时间,获取N小时后/N小时前的时间,数据返回类型:String
import java.util.Date;
import java.util.Calendar;
import java.text.SimpleDateFormat;
public class DateUtils{
/**
* 计算传入参数时间加上N个月之后的日期
*
* @param dateTime当前时间
* @param i 小时数(正数向后推,负数向前推)
* @return 结果
*/
public static String getDateTimeString(String dateTime, int i) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date();
try {
date = sdf.parse(dateTime);
} catch (ParseException e) {
e.printStackTrace();
}
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.HOUR_OF_DAY, i);
return sdf.format(calendar.getTime());
}
}
获取n天前的所有日期,数据返回类型:List
import java.util.Date;
import java.util.Calendar;
import java.text.SimpleDateFormat;
public class DateUtils{
public final static String DATE_PATTERN = "yyyy-MM-dd";
public static String format(Date date) {
return format(date, DATE_PATTERN);
}
public static String format(Date date, String pattern) {
if (date != null) {
SimpleDateFormat df = new SimpleDateFormat(pattern);
return df.format(date);
}
return null;
}
/**
* 获取n天前的所有日期
*
* @param i 天数(正数向后推,负数向前推)
* @return 结果
*/
public static List<String> getDays(int n) {
List<String> dateList = new ArrayList<String>();
for (int j = -n; j < 0; j++) {
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DAY_OF_MONTH, j); // 设置为前j天
String tempDate = format(cal.getTime());
dateList.add(tempDate);
}
return dateList;
}
}
当天开始时间,数据返回类型:String
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class DateUtils{
public static final DateTimeFormatter DFY_MD_HMS = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
public static String todayStart() {
LocalDateTime today_start = LocalDateTime.of(LocalDate.now(), LocalTime.MIN);
return today_start.format(DFY_MD_HMS);
}
}
当天结束时间,数据返回类型:String
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class DateUtils{
public static String todayEnd() {
LocalDateTime today_start = LocalDateTime.of(LocalDate.now(), LocalTime.MAX);
return today_start.format(DFY_MD_HMS);
}
}
获取两个日期之间的所有日期列表,数据返回类型:List
import java.time.LocalDate;
public class DateUtils{
public static List<LocalDate> getMiddleDate(LocalDate begin, LocalDate end) {
List<LocalDate> localDateList = new ArrayList<>();
long length = end.toEpochDay() - begin.toEpochDay();
for (long i = length; i >= 0; i--) {
localDateList.add(end.minusDays(i));
}
return localDateList;
}
}
开始日期与结束日期之间相差的天数,数据返回类型:long
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
public class DateUtils{
public static long getMiddleDate(LocalDateTime startDateTime, LocalDateTime endDateTime) {
return ChronoUnit.DAYS.between(startDateTime, endDateTime);
}
}
同一天日期比较,数据返回类型:boolean
import java.time.ZonedDateTime;
import java.time.ZoneId;
import java.util.Date;
public class DateUtils{
public static ZoneId ZONE_ID_BJ = ZoneId.of("GMT+08:00");
public static boolean sameDate(Date d1, Date d2) {
try {
return ZonedDateTime.ofInstant(d1.toInstant(), ZONE_ID_BJ).toLocalDate().isEqual(
ZonedDateTime.ofInstant(
d2.toInstant(), ZONE_ID_BJ).toLocalDate());
} catch (Exception e) {
return false;
}
}
}
日期比较,数据返回类型:boolean
import java.time.ZonedDateTime;
import java.time.ZoneId;
import java.util.Date;
public class DateUtils{
public static ZoneId ZONE_ID_BJ = ZoneId.of("GMT+08:00");
public static boolean compareDate(Date d1, Date d2) {
try {
return ZonedDateTime.ofInstant(d1.toInstant(), ZONE_ID_BJ).toLocalDateTime().isAfter(
ZonedDateTime.ofInstant(
d2.toInstant(), ZONE_ID_BJ)
.toLocalDateTime());
} catch (Exception e) {
return false;
}
}
}
LocalDateTime转时间戳,数据返回类型:Long
import java.time.LocalDateTime;
import java.time.ZoneId;
public class DateUtils{
public static Long getTimeStamp(LocalDateTime localDateTime) {
return localDateTime.atZone(ZoneId.systemDefault()).toEpochSecond();
}
}
时间戳转LocalDateTime,数据返回类型:LocalDateTime
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.OffsetDateTime;
public class DateUtils{
public static LocalDateTime fromTimeStamp(Long timeStamp) {
return LocalDateTime.ofEpochSecond(timeStamp, 0, OffsetDateTime.now().getOffset());
}
}
LocalDateTime转Date(Jdk8后,不推荐使用Date),数据返回类型:Date
import java.time.LocalDateTime;
import java.time.ZoneId;
public class DateUtils{
/**
* LocalDateTime 转 Date
* Jdk8 后 不推荐使用 {@link Date} Date
*
* @param localDateTime /
* @return /
*/
public static Date toDate(LocalDateTime localDateTime) {
return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
}
}
LocalDate转Date,数据返回类型:Date
import java.time.LocalDateTime;
import java.time.ZoneId;
public class DateUtils{
/**
* LocalDate 转 Date
* Jdk8 后 不推荐使用 {@link Date} Date
*
* @param localDate /
* @return /
*/
public static Date toDate(LocalDate localDate) {
return toDate(localDate.atTime(LocalTime.now(ZoneId.systemDefault())));
}
}
Date转LocalDateTime,数据返回类型:LocalDateTime
import java.time.LocalDateTime;
import java.time.ZoneId;
public class DateUtils{
/**
* Date转 LocalDateTime
* Jdk8 后 不推荐使用 {@link Date} Date
*
* @param date /
* @return /
*/
public static LocalDateTime toLocalDateTime(Date date) {
return LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
}
}
LocalDateTime日期格式化,数据返回类型:String
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class DateUtils{
/**
* 日期 格式化
*
* @param localDateTime /
* @param patten /
* @return /
*/
public static String localDateTimeFormat(LocalDateTime localDateTime, String patten) {
DateTimeFormatter df = DateTimeFormatter.ofPattern(patten);
return df.format(localDateTime);
}
}
字符串转LocalDateTime,字符串格式 yyyy-MM-dd / yyyy-MM-dd HH:mm:ss,数据返回类型:LocalDateTime
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class DateUtils{
/**
* 字符串转 LocalDateTime ,字符串格式 yyyy-MM-dd / yyyy-MM-dd HH:mm:ss
*
* @param localDateTime /
* @return /
*/
public static LocalDateTime parseLocalDateTimeFormat(String localDateTime, String pattern) {
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(pattern);
return LocalDateTime.from(dateTimeFormatter.parse(localDateTime));
}
}
获取当天的每个小时,数据返回类型:List
import java.util.Date;
import java.util.Calendar;
import java.text.SimpleDateFormat;
public class DateUtils{
public static List<String> findHoursStr() {
Date day = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String s = sdf.format(day);
Date date = null;
try {
date = sdf.parse(s);
} catch (ParseException e) {
e.printStackTrace();
}
ArrayList<String> hoursStrList = new ArrayList<String>();
hoursStrList.add(format.format(date));
for (int i = 1; i < 24; i++) {
Calendar cal = Calendar.getInstance();
cal.setTime(Objects.requireNonNull(date));
cal.add(Calendar.HOUR, 1);
date = cal.getTime();
String hourStr = format.format(date);
hoursStrList.add(hourStr);
}
return hoursStrList;
}
}
获取服务器启动时间,数据返回类型:Date
import java.util.Date;
import java.lang.management.ManagementFactory;
public class DateUtils{
public static Date getServerStartDate() {
long time = ManagementFactory.getRuntimeMXBean().getStartTime();
return new Date(time);
}
}
计算两个时间差,数据返回类型:String
import java.util.Date;
public class DateUtils{
public static String getDatePoor(Date endDate, Date nowDate) {
long nd = 1000 * 24 * 60 * 60;
long nh = 1000 * 60 * 60;
long nm = 1000 * 60;
// long ns = 1000;
// 获得两个时间的毫秒时间差异
long diff = endDate.getTime() - nowDate.getTime();
// 计算差多少天
long day = diff / nd;
// 计算差多少小时
long hour = diff % nd / nh;
// 计算差多少分钟
long min = diff % nd % nh / nm;
// 计算差多少秒//输出结果
// long sec = diff % nd % nh % nm / ns;
return day + "天" + hour + "小时" + min + "分钟";
}
}
计算两个时间分钟差,数据返回类型:String
import java.time.LocalDateTime;
import java.time.ZoneId;
public class DateUtils{
public static String getMinutePoor(Date endDate, Date nowDate) {
long nd = 1000 * 24 * 60 * 60;
long nh = 1000 * 60 * 60;
long nm = 1000 * 60;
// 获得两个时间的毫秒时间差异
long diff = endDate.getTime() - nowDate.getTime();
// 计算差多少分钟
long min = diff / nm;
if (min < 1) {
return "1分钟";
}
return min + "分钟";
}
}
判断两个日期是否是同一周,数据返回类型:boolean
import java.util.Date;
import java.util.Calendar;
public class DateUtils{
public static boolean isSameWeek(Date startTime, Date endTime) {
Calendar calendar = Calendar.getInstance();
calendar.setFirstDayOfWeek(Calendar.MONDAY); // 设置一周的第一天是星期几
calendar.setTime(startTime);
int startWeek = calendar.get(Calendar.WEEK_OF_YEAR);
calendar.setTime(endTime);
int endWeek = calendar.get(Calendar.WEEK_OF_YEAR);
if (endWeek == startWeek) {
return true;
}
return false;
}
}
判断两个日期是否是同一天,数据返回类型:boolean
import java.time.LocalDateTime;
import java.time.ZoneId;
public class DateUtils{
public static boolean isSameTime(Date startTime, Date endTime, String pattern) {
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
String start = sdf.format(startTime); // 参数时间
String end = sdf.format(endTime); // 当前时间
if (start.equals(end)) {
return true;
}
return false;
}
}
我们都知道我们正在为自由而奋斗,根据我们自己的观念来决定我们的一生。
We know that we are fighting for freedom to shape our life according to our own ideas.