package com.kidgrow.common.utils;
|
|
import org.apache.commons.lang3.StringUtils;
|
|
import java.text.SimpleDateFormat;
|
import java.util.*;
|
|
/**
|
* 石家庄喜高科技有限责任公司 版权所有 © Copyright 2020<br>
|
*
|
* @Description: <br>
|
* @Project: <br>
|
* @CreateDate: Created in 2020/2/28 11:07 <br>
|
* @Author: <a href="4345453@kidgrow.com">liuke</a>
|
*/
|
public class DateCalUtil {
|
/**
|
* 将字符串转换成具体的时间(Calendar)
|
* @return Calendar 时间
|
* @param str 为要格式化字符串
|
*/
|
public static Calendar getCalendarByStr(String str) {
|
if (str == null) return null;
|
Date date = getDateByStr(str);
|
Calendar cal = Calendar.getInstance();
|
cal.clear();
|
cal.setTime(date);
|
return cal;
|
}
|
|
/**
|
* 将Calendar类型转化为字符串类型
|
* @param calendar 要转化的Calendar类型变量
|
* @param hasTime true :yyyy-MM-dd HH:mm:ss;false:yyyy-MM-dd
|
* @return
|
*/
|
public static String getCalendarStr(Calendar calendar ,boolean hasTime ) {
|
if (calendar == null) return "";
|
return getDateStr(calendar.getTime() ,hasTime );
|
|
}
|
|
/**
|
* 将具体的时间转换成字符串
|
* @return 转换后的字符串格式为yyyy-MM-dd HH:mm:ss 或 yyyy-MM-dd
|
* @param aDate 为要格式化的时间
|
* @param hasTime :返回的字符串是否包括时间 true:包括时间 false:只有日期
|
*/
|
public static String getDateStr(Date aDate,boolean hasTime ) {
|
if (aDate == null) return "";
|
String dateStr = "";
|
if(hasTime)
|
dateStr = getDateStrByDefineStyle(aDate,"yyyy-MM-dd HH:mm:ss");
|
else
|
dateStr = getDateStrByDefineStyle(aDate,"yyyy-MM-dd");
|
return dateStr;
|
}
|
|
/**
|
* 返回由指定字符串转换成的时间,字符串格式类似"2006-2-12 14:22:30" 或"2006-2-12"
|
* @return 转换后的时间
|
* @param aStr 为要转换成时间的字符串
|
*/
|
public static Date getDateByStr(String aStr) {
|
SimpleDateFormat df =null;
|
if (aStr == null) return null;
|
if(aStr.indexOf(":")>0 )//如果有时间
|
df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
else //如果没有时间
|
df = new SimpleDateFormat("yyyy-MM-dd");
|
try
|
{
|
Date result = df.parse(aStr);
|
return result;
|
}
|
catch (Exception e)
|
{
|
return null;
|
}
|
}
|
|
/**
|
* 将具体的时间根据要求的格式,转换成字符串
|
* @param aDate 为要格式化的时间
|
* @param style 要转化的格式
|
* @return
|
*/
|
public static String getDateStrByDefineStyle(Date aDate,String style){
|
if (aDate == null) return "";
|
SimpleDateFormat df= new SimpleDateFormat(style);
|
String s = df.format(aDate);
|
return s;
|
}
|
|
/**
|
* 将具体的时间转换成字符串
|
* @return 转换后的字符串,格式为yyyy-MM-dd HH:mm:ss
|
* @param aDate 为要格式化的时间
|
*/
|
public static String getDateStr(Date aDate) {
|
if (aDate == null) return "";
|
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
String s = df.format(aDate);
|
return s;
|
// return getDateStr(aDate,true);
|
}
|
|
/**
|
* 获取昨日日期字符串
|
* @return 昨日日期字符串,格式为yyyy-MM-dd
|
*/
|
public static String getYesterdayStr() {
|
Calendar cal = Calendar.getInstance();
|
cal.add(Calendar.DATE, -1);
|
return getCalendarStr(cal, false);
|
}
|
|
/**
|
* 获取一周前日期字符串
|
* @return 一周前日期字符串,格式为yyyy-MM-dd
|
*/
|
public static String getWeekAgoStr() {
|
Calendar cal = Calendar.getInstance();
|
cal.add(Calendar.DATE, -7);
|
return getCalendarStr(cal, false);
|
}
|
|
/**
|
* 获取某日期半年后日期字符串
|
* @return 半年后日期字符串,格式为yyyy-MM-dd HH:mm:ss
|
*/
|
public static String getHalfYearStr(Date aDate) {
|
Calendar cal = Calendar.getInstance();
|
cal.setTime(aDate);
|
cal.add(Calendar.MONTH, 6);
|
return getCalendarStr(cal, true);
|
}
|
|
/**
|
* 获取本周第一天日期字符串
|
* @return 本周第一天日期字符串,格式为yyyy-MM-dd
|
*/
|
public static String getWeekFirstStr() {
|
Calendar cal = Calendar.getInstance();
|
int day_of_week = cal.get(Calendar.DAY_OF_WEEK) - 2;
|
cal.add(Calendar.DATE, -day_of_week);
|
return getCalendarStr(cal, false);
|
}
|
/**
|
* 获取本周最后一天日期字符串
|
* @return 本周最后一天日期字符串,格式为yyyy-MM-dd
|
*/
|
public static String getWeekLastStr() {
|
Calendar cal = Calendar.getInstance();
|
int day_of_week = cal.get(Calendar.DAY_OF_WEEK) - 2;
|
cal.add(Calendar.DATE, -day_of_week);
|
cal.add(Calendar.DATE, 6);
|
return getCalendarStr(cal, false);
|
}
|
/**
|
* 获取本月第一天日期字符串
|
* @return 本月第一天日期字符串,格式为yyyy-MM-dd
|
*/
|
public static String getMonthFirstStr() {
|
Calendar cal = Calendar.getInstance();
|
cal.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天
|
return getCalendarStr(cal, false);
|
}
|
/**
|
* 获取本月最后一天日期字符串
|
* @return 本月最后一天日期字符串,格式为yyyy-MM-dd
|
*/
|
public static String getMonthLastStr() {
|
Calendar cal = Calendar.getInstance();
|
cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
|
return getCalendarStr(cal, false);
|
}
|
/**
|
* 获取本年第一天日期字符串
|
* @return 本年第一天日期字符串,格式为yyyy-MM-dd
|
*/
|
public static String getYearFirstStr() {
|
Calendar cal = Calendar.getInstance();
|
int currentYear = cal.get(Calendar.YEAR);
|
cal.clear();
|
cal.set(Calendar.YEAR, currentYear);
|
return getCalendarStr(cal, false);
|
}
|
/**
|
* 获取本年最后一天日期字符串
|
* @return 本年最后一天日期字符串,格式为yyyy-MM-dd
|
*/
|
public static String getYearLastStr() {
|
Calendar cal = Calendar.getInstance();
|
int currentYear = cal.get(Calendar.YEAR);
|
cal.clear();
|
cal.set(Calendar.YEAR, currentYear);
|
cal.roll(Calendar.DAY_OF_YEAR, -1);
|
return getCalendarStr(cal, false);
|
}
|
|
/**
|
* 获取本月第一天日期时间字符串
|
* @author Han Guohong
|
* @return
|
*/
|
public static String getNowMonthFirstStr() {
|
Calendar cal = Calendar.getInstance();
|
cal.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为上月第一天
|
return getCalendarStr(cal, false)+" 00:00:00";
|
}
|
|
/**
|
* 获取本月最后一天日期时间字符串
|
* @author Han Guohong
|
* @return
|
*/
|
public static String getNowMonthLastStr() {
|
Calendar cal = Calendar.getInstance();
|
cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
|
return getCalendarStr(cal, false)+" 23:59:59";
|
}
|
|
/**
|
* 获取上月第一天日期字符串
|
* @return 上月第一天日期字符串,格式为yyyy-MM-dd
|
*/
|
public static String getPreMonthFirstStr() {
|
Calendar cal = Calendar.getInstance();
|
int currentMonth = cal.get(Calendar.MONTH);
|
cal.set(Calendar.MONTH, currentMonth-1);
|
cal.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为上月第一天
|
return getCalendarStr(cal, false);
|
}
|
/**
|
* 获取上月最后一天日期字符串
|
* @return 上月最后一天日期字符串,格式为yyyy-MM-dd
|
*/
|
public static String getPreMonthLastStr() {
|
Calendar cal = Calendar.getInstance();
|
int currentMonth = cal.get(Calendar.MONTH);
|
cal.set(Calendar.MONTH, currentMonth-1);
|
cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
|
return getCalendarStr(cal, false);
|
}
|
|
/**
|
* 根据用户生日计算年龄
|
*/
|
public static int getAgeByBirthday(Date birthday) {
|
Calendar cal = Calendar.getInstance();
|
if (cal.before(birthday)) {
|
throw new IllegalArgumentException(
|
"The birthDay is before Now.It's unbelievable!");
|
}
|
int yearNow = cal.get(Calendar.YEAR);
|
int monthNow = cal.get(Calendar.MONTH) + 1;
|
int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH);
|
cal.setTime(birthday);
|
int yearBirth = cal.get(Calendar.YEAR);
|
int monthBirth = cal.get(Calendar.MONTH) + 1;
|
int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH);
|
int age = yearNow - yearBirth;
|
if (monthNow <= monthBirth) {
|
if (monthNow == monthBirth) {
|
// monthNow==monthBirth
|
if (dayOfMonthNow < dayOfMonthBirth) {
|
age--;
|
}
|
} else {
|
// monthNow>monthBirth
|
age--;
|
}
|
}
|
return age;
|
}
|
|
//获得全球唯一性的id
|
public static String getId(){
|
String id= UUID.randomUUID().toString();
|
id=id.replace("-", "");//替换掉中间的那个斜杠
|
return id;
|
}
|
|
/**
|
* 计算两个时间差值(分钟)
|
*/
|
public static int dateDiffMin(Date date1,Date date2) {
|
long diff;
|
long nd = 1000*24*60*60;//一天的毫秒数
|
long nh = 1000*60*60;//一小时的毫秒数
|
long nm = 1000*60;//一分钟的毫秒数
|
//获得两个时间的毫秒时间差异
|
diff = date1.getTime() - date2.getTime();
|
long min = diff%nd%nh/nm;//计算差多少分钟
|
return new Long(min).intValue();
|
}
|
|
/**
|
* 获取指定日期前几天或者后几天的日期
|
* @author Han Guohong
|
* @param date
|
* @param days
|
* @return
|
*/
|
public static String getBeforeOrAfterDaysDate(Date date,int days) {
|
Calendar cal = Calendar.getInstance();
|
cal.setTime(date);
|
cal.add(Calendar.DATE, days);
|
return getCalendarStr(cal, false);
|
}
|
|
/**
|
* 获取指定时间前几分钟或者后几分钟的时间
|
* @param date
|
* @param minute
|
* @return
|
*/
|
public static String getBeforeOrAfterMinuteTime(Date date,int minute) {
|
Calendar cal = Calendar.getInstance();
|
cal.setTime(date);
|
cal.add(Calendar.MINUTE, minute);
|
return getCalendarStr(cal, false);
|
}
|
|
/**
|
* 获取指定日期中间的所有日期
|
*/
|
public static List<Date> findDates(Date dBegin, Date dEnd){
|
List lDate = new ArrayList();
|
lDate.add(dBegin);
|
Calendar calBegin = Calendar.getInstance();
|
// 使用给定的 Date 设置此 Calendar 的时间
|
calBegin.setTime(dBegin);
|
Calendar calEnd = Calendar.getInstance();
|
// 使用给定的 Date 设置此 Calendar 的时间
|
calEnd.setTime(dEnd);
|
// 测试此日期是否在指定日期之后
|
while (dEnd.after(calBegin.getTime())){
|
// 根据日历的规则,为给定的日历字段添加或减去指定的时间量
|
calBegin.add(Calendar.DAY_OF_MONTH, 1);
|
lDate.add(calBegin.getTime());
|
}
|
return lDate;
|
}
|
|
public static String removeLast(String dateStr){
|
if(StringUtils.isNotBlank(dateStr.trim())){
|
return dateStr.substring(0, 19);
|
}
|
return null;
|
}
|
|
/**
|
* 获取某天的开始时间
|
* @author Han Guohong
|
* @param date
|
* @return
|
*/
|
public static Date getDayBegin(Date date){
|
return getDateByStr(getDateStr(date, false)+" 00:00:00");
|
}
|
|
/**
|
* 获取某天的结束时间
|
* @author Han Guohong
|
* @param date
|
* @return
|
*/
|
public static Date getDayEnd(Date date){
|
String dateString = getBeforeOrAfterDaysDate(date, +1);
|
date = getDateByStr(dateString);
|
return getDateByStr(getDateStr(date, false)+" 00:00:00");
|
}
|
|
/*public static void main(String [] args){
|
Date now = new Date();
|
Date end = getDateByStr(getBeforeOrAfterDaysDate(now, -5));
|
Date begin = getDateByStr(getBeforeOrAfterDaysDate(now, -10));
|
List<Date> list = findDates(begin, end);
|
for (Date date : list) {
|
System.out.println(getDateStr(date, true));
|
}
|
System.out.println(getDayEnd(now));
|
}*/
|
}
|