package com.enation.app.javashop.framework.util;
import org.apache.commons.lang.StringUtils;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
/**
* 日期相关的操作
*
* @author Dawei
*/
@SuppressWarnings({"AlibabaCollectionInitShouldAssignCapacity", "AlibabaUndefineMagicConstant"})
public class DateUtil {
/**
* 一天的秒数
*/
public static final long ONE_DAY = 86400;
public static long startOfThisWeek() {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
return cal.getTime().getTime() / 1000;
}
public static long startOfThisMonth() {
Calendar calendar = Calendar.getInstance();
//获取某月最小天数
int firstDay = calendar.getMinimum(Calendar.DATE);
//设置日历中月份的最小天数
calendar.set(Calendar.DAY_OF_MONTH, firstDay);
//格式化日期
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
Date date = calendar.getTime();
return date.getTime() / 1000;
}
/**
* 将 Sun Apr 15 04:12:39 CST 2018 格式的时间转换为时间戳
*
* @param dateFormat Sun Apr 15 04:12:39 CST 2018 格式的时间
* @return 时间戳
*/
public static long getFormatDate(String dateFormat) {
try {
SimpleDateFormat sdf1 = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.ENGLISH);
return sdf1.parse(dateFormat).getTime() / 1000;
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}
/**
* 当天的开始时间
*
* @return
*/
public static long startOfTodDay() {
return firstOfSomeDay(0).getTime() / 1000;
}
/**
* 当天的开始时间
*
* @return
*/
public static Date startDateOfTodDay() {
return firstOfSomeDay(0);
}
/**
* 明天的结束时间
*
* @return
*/
public static Date endDateOfTomorrow() {
return lastOfSomeDay(1);
}
/**
* 当天的结束时间
*
* @return
*/
public static long endOfTodDay() {
return lastOfSomeDay(0).getTime() / 1000;
}
/**
* 昨天的开始时间
*
* @return
*/
public static long startOfyesterday() {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.add(Calendar.DATE, -1);
calendar.set(Calendar.MILLISECOND, 0);
Date date = calendar.getTime();
return date.getTime() / 1000;
}
/**
* 昨天的结束时间
*
* @return
*/
public static long endOfyesterday() {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 23);
calendar.set(Calendar.MINUTE, 59);
calendar.set(Calendar.SECOND, 59);
calendar.set(Calendar.MILLISECOND, 999);
calendar.add(Calendar.DATE, -1);
Date date = calendar.getTime();
return date.getTime() / 1000;
}
/**
* 某天的开始时间
*
* @param dayUntilNow 距今多少天以前
* @return 时间戳
*/
public static long startOfSomeDay(int dayUntilNow) {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
calendar.add(Calendar.DATE, -dayUntilNow);
Date date = calendar.getTime();
return date.getTime() / 1000;
}
/**
* 某天的开始时间
*
* @param dayUntilNow 距今多少天以后
* @return 时间戳
*/
public static long endOfSomeDay(int dayUntilNow) {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
calendar.add(Calendar.DATE, +dayUntilNow);
Date date = calendar.getTime();
return date.getTime() / 1000;
}
/**
* 某天的开始时间00:00:00,000
*
* @param dayUntilNow
* @return 时间戳
*/
public static Date firstOfSomeDay(int dayUntilNow) {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
calendar.add(Calendar.DATE, dayUntilNow);
return calendar.getTime();
}
/**
* 某天的结束时间23:59:59,999
*
* @param dayUntilNow
* @return 时间戳
*/
public static Date lastOfSomeDay(int dayUntilNow) {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 23);
calendar.set(Calendar.MINUTE, 59);
calendar.set(Calendar.SECOND, 59);
calendar.set(Calendar.MILLISECOND, 0);
calendar.add(Calendar.DATE, dayUntilNow);
return calendar.getTime();
}
public static Date lastOfDate(Date d) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(d);
calendar.set(Calendar.HOUR_OF_DAY, 23);
calendar.set(Calendar.MINUTE, 59);
calendar.set(Calendar.SECOND, 59);
calendar.set(Calendar.MILLISECOND, 0);
return calendar.getTime();
}
public static Date firstOfDate(Date d) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(d);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
return calendar.getTime();
}
/**
* 计算小时数,四舍五入
*
* @param date1
* @param date2
* @return
*/
public static int differentHour(Date date1, Date date2) {
int hour = (int) ((date2.getTime() - date1.getTime()) * 1.0 / (3600000) + 0.5);
return hour;
}
/**
* 计算两个日期相差的分钟数
* @param date1
* @param date2
* @return
* @throws ParseException
*/
public static long differentMinutes(Date date1, Date date2) {
long diff=Math.abs(date1.getTime()-date2.getTime());
return diff/(1000*60);
}
/**
* 某天的年月日
*
* @param dayUntilNow 距今多少天以前
* @return 年月日map key为 year month day
*/
public static Map<String, Object> getYearMonthAndDay(int dayUntilNow) {
Map<String, Object> map = new HashMap<String, Object>(3);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
calendar.add(Calendar.DATE, -dayUntilNow);
map.put("year", calendar.get(Calendar.YEAR));
map.put("month", calendar.get(Calendar.MONTH) + 1);
map.put("day", calendar.get(Calendar.DAY_OF_MONTH));
return map;
}
/**
* 将一个字符串转换成日期格式
*
* @param date
* @param pattern
* @return
*/
public static Date toDate(String date, String pattern) {
if ("".equals("" + date)) {
return null;
}
if (pattern == null) {
pattern = "yyyy-MM-dd";
}
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
Date newDate = new Date();
try {
newDate = sdf.parse(date);
} catch (Exception ex) {
ex.printStackTrace();
}
return newDate;
}
/**
* 获取上个月的开始结束时间
*
* @return
*/
public static Long[] getLastMonth() {
// 取得系统当前时间
Calendar cal = Calendar.getInstance();
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH) + 1;
// 取得系统当前时间所在月第一天时间对象
cal.set(Calendar.DAY_OF_MONTH, 1);
// 日期减一,取得上月最后一天时间对象
cal.add(Calendar.DAY_OF_MONTH, -1);
// 输出上月最后一天日期
int day = cal.get(Calendar.DAY_OF_MONTH);
String months = "";
String days = "";
if (month > 1) {
month--;
} else {
year--;
month = 12;
}
if (!(String.valueOf(month).length() > 1)) {
months = "0" + month;
} else {
months = String.valueOf(month);
}
if (!(String.valueOf(day).length() > 1)) {
days = "0" + day;
} else {
days = String.valueOf(day);
}
String firstDay = "" + year + "-" + months + "-01";
String lastDay = "" + year + "-" + months + "-" + days + " 23:59:59";
Long[] lastMonth = new Long[2];
lastMonth[0] = DateUtil.getDateline(firstDay);
lastMonth[1] = DateUtil.getDateline(lastDay, "yyyy-MM-dd HH:mm:ss");
return lastMonth;
}
/**
* 获取当月的开始结束时间
*
* @return
*/
public static Long[] getCurrentMonth() {
// 取得系统当前时间
Calendar cal = Calendar.getInstance();
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH) + 1;
// 输出下月第一天日期
int notMonth = cal.get(Calendar.MONTH) + 2;
// 取得系统当前时间所在月第一天时间对象
cal.set(Calendar.DAY_OF_MONTH, 1);
// 日期减一,取得上月最后一天时间对象
cal.add(Calendar.DAY_OF_MONTH, -1);
String months = "";
String nextMonths = "";
if (!(String.valueOf(month).length() > 1)) {
months = "0" + month;
} else {
months = String.valueOf(month);
}
if (!(String.valueOf(notMonth).length() > 1)) {
nextMonths = "0" + notMonth;
} else {
nextMonths = String.valueOf(notMonth);
}
String firstDay = "" + year + "-" + months + "-01";
String lastDay = "" + year + "-" + nextMonths + "-01";
Long[] currentMonth = new Long[2];
currentMonth[0] = DateUtil.getDateline(firstDay);
currentMonth[1] = DateUtil.getDateline(lastDay);
return currentMonth;
}
/**
* 获取某年开始结束时间
*
* @return
*/
public static Long[] getYearTime(Integer year) {
Calendar firstCal = Calendar.getInstance();
firstCal.set(Calendar.YEAR, year - 1);
firstCal.set(Calendar.MONTH, Calendar.DECEMBER);
firstCal.set(Calendar.DATE, 31);
Calendar lastCal = Calendar.getInstance();
lastCal.set(Calendar.YEAR, year);
lastCal.set(Calendar.MONTH, Calendar.DECEMBER);
lastCal.set(Calendar.DATE, 31);
Long[] yearTime = new Long[2];
yearTime[0] = firstCal.getTime().getTime() / 1000;
yearTime[1] = lastCal.getTime().getTime() / 1000;
return yearTime;
}
/**
* 把日期转换成字符串型
*
* @param date
* @param pattern
* @return
*/
public static String toString(Date date, String pattern) {
if (date == null) {
return "";
}
if (pattern == null) {
pattern = "yyyy-MM-dd";
}
String dateString = "";
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
try {
dateString = sdf.format(date);
} catch (Exception ex) {
ex.printStackTrace();
}
return dateString;
}
public static String toString(Long time, String pattern) {
if (time > 0) {
if (time.toString().length() == 10) {
time = time * 1000;
}
Date date = new Date(time);
String str = DateUtil.toString(date, pattern);
return str;
}
return "";
}
public static long getDateline() {
return System.currentTimeMillis() / 1000;
}
/**
* 查询两个日期相差的天数
* @param date1
* @param date2
* @return
*/
public static int differentDays(Date date1, Date date2) {
Calendar cal1 = Calendar.getInstance();
cal1.setTime(date1);
Calendar cal2 = Calendar.getInstance();
cal2.setTime(date2);
int day1 = cal1.get(Calendar.DAY_OF_YEAR);
int day2 = cal2.get(Calendar.DAY_OF_YEAR);
int year1 = cal1.get(Calendar.YEAR);
int year2 = cal2.get(Calendar.YEAR);
if (year1 != year2) { //不同年
int timeDistance = 0;
for (int i = year1; i < year2; i++) {
if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0) {//闰年
timeDistance += 366;
} else {//不是闰年
timeDistance += 365;
}
}
return timeDistance + (day2 - day1);
} else {//同一年
return day2 - day1;
}
}
public static long getDateline(String date) {
return (long) (toDate(date, "yyyy-MM-dd").getTime() / 1000);
}
public static long getDateHaveHour(String date) {
return (long) (toDate(date, "yyyy-MM-dd HH").getTime() / 1000);
}
public static long getDateline(String date, String pattern) {
return (long) (toDate(date, pattern).getTime() / 1000);
}
public static int getAge(Date birthday) {
int age = 0;
try {
Calendar now = Calendar.getInstance();
now.setTime(new Date()); // 当前时间
Calendar birth = Calendar.getInstance();
birth.setTime(birthday); // 传入的时间
//如果传入的时间,在当前时间的后面,返回0岁
if (birth.after(now)) {
age = 0;
} else {
age = now.get(Calendar.YEAR) - birth.get(Calendar.YEAR);
if (now.get(Calendar.DAY_OF_YEAR) > birth.get(Calendar.DAY_OF_YEAR)) {
age += 1;
}
}
return age;
} catch (Exception e) {
return 0;
}
}
public static Date getDate(Date d, int field, int i) {
Calendar instance = Calendar.getInstance();
instance.setTime(d);
instance.set(field, i);
return instance.getTime();
}
public static Date addDate(Date d, int field, int i) {
Calendar instance = Calendar.getInstance();
instance.setTime(d);
instance.add(field, i);
return instance.getTime();
}
public static boolean isSameDate(Date actualEndDate, Date date) {
Calendar cal1 = Calendar.getInstance();
cal1.setTime(actualEndDate);
Calendar cal2 = Calendar.getInstance();
cal2.setTime(date);
int day1 = cal1.get(Calendar.DAY_OF_YEAR);
int day2 = cal2.get(Calendar.DAY_OF_YEAR);
int year1 = cal1.get(Calendar.YEAR);
int year2 = cal2.get(Calendar.YEAR);
if (year1 != year2) { //不同年
return false;
} else {//同一年
return day2 - day1==0;
}
}
/**
* unix时间戳转时间字符串
*
* @param timestamp
* @return
*/
public static String TimeStamp2DateStr(Long timestamp) {
Long timestampJava = timestamp * 1000;
String date = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new java.util.Date(timestampJava));
return date;
}
/**
* 将日期字符串日期格式转换
*
* @param date 日期字符串
* @param pattern 日期格式
* @return
*/
public static String DateStrConvert(String date, String pattern) {
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
try {
Date start = sdf.parse(date);
return sdf.format(start);
} catch (ParseException e) {
e.printStackTrace();
}
return sdf.format(StringUtils.EMPTY);
}
public static Integer getTimePoint() {
return getTimePoint(null);
}
public static Integer getTimePoint(Date date) {
Calendar d1 = Calendar.getInstance();
if(date!=null){
d1.setTime(date);
}
int h = d1.get(Calendar.HOUR_OF_DAY);
int m = d1.get(Calendar.MINUTE);
return h * 60 + m;
}
/**
* timepoint转为时间字符串(例如7:00)
*
* @param timepoint
* @return
*/
public static String timePointToStr(Integer timepoint) {
int hours = (int) Math.floor(timepoint / 60);
int minute = timepoint % 60;
return hours + ":" + minute;
}
/**
* 上周一时间
* @param date
* @return
*/
public static Date geLastWeekMonday(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(getThisWeekMonday(date));
cal.add(Calendar.DATE, -7);
return cal.getTime();
}
/**
* 这周一时间
* @param date
* @return
*/
public static Date getThisWeekMonday(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
// 获得当前日期是一个星期的第几天
int dayWeek = cal.get(Calendar.DAY_OF_WEEK);
if (1 == dayWeek) {
cal.add(Calendar.DAY_OF_MONTH, -1);
}
// 设置一个星期的第一天,按中国的习惯一个星期的第一天是星期一
cal.setFirstDayOfWeek(Calendar.MONDAY);
// 获得当前日期是一个星期的第几天
int day = cal.get(Calendar.DAY_OF_WEEK);
// 根据日历的规则,给当前日期减去星期几与一个星期第一天的差值
cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);
return cal.getTime();
}
/**
* 下周一时间
* @param date
* @return
*/
public static Date getNextWeekMonday(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(getThisWeekMonday(date));
cal.add(Calendar.DATE, 7);
return cal.getTime();
}
}
日期操作常用的方法:
最新推荐文章于 2021-03-17 23:37:43 发布