/**
* 根据age计算出生日期
*/
public static Date validatorAge(Integer age) throws ParseException {
if (age == null) {
return null;
}
Date date = new Date();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
String stratData = df.format(date).substring(4, 9);
Integer middle = Integer.valueOf(df.format(date).substring(0, 4));
middle = middle - age;
String endData = middle + stratData;
Date overDate = df.parse(endData);
return overDate;
}
public static void main(String[] args) throws ParseException {
// 根据出生日期计算年龄
System.out.println(validatorAge(new Date()));
}
public static Integer validatorAge(Date getBirthday) throws ParseException {
Date date = new Date();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
String birthdayStr = df.format(getBirthday).substring(0, 4);
String nowDataStr = df.format(date).substring(0, 4);
Integer birthday = Integer.valueOf(birthdayStr);
Integer nowData = Integer.valueOf(nowDataStr);
Integer age = nowData - birthday;
return age ;
}
/**
* 开始时间 > 结束时间 = true
* 开始时间 < 结束时间 = false
*/
public static boolean checkTime(Date startTime, Date endTime) {
if (startTime == null && endTime == null) {
return false;
}
int flag = startTime.compareTo(endTime);
if (flag != -1) {
return false;
}
return true;
}
/**
* 设置时间格式 "yyyy-MM-dd HH:mm:ss"
* <p>
* 例如 当天最初时刻 yyyy-MM-dd 23:59:59
*/
public static Date formatDateStart(Date date) throws ParseException {
if (date == null) {
return null;
}
SimpleDateFormat sm = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(sm.format(date.getTime()));
String dataStr = sm.format(date.getTime());
dataStr = dataStr.substring(0, 10) + " 23:59:59";
return sm.parse(dataStr);
}
获取之前日期 入参 小时
传入2 就获取两小时前的时间 当前时间 - 2小时
public static String getBeforeHourTime(int ihour) {
String returnstr = "";
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) - ihour);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
returnstr = sdf.format(calendar.getTime());
return returnstr;
}
package org.jeecg.modules.minprogram.company.util;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
public class DateUtil {
/**
* 根据标识 获取查询开始时间
* index = 1 近7天
* index = 2 近1月
* index = 3 近3月
* inex = 4 近6月
* @param index
* @return
*/
public static Date getSearchStartDate(Integer index){
Date pastDate = null;
if(index == null){
return null;
}
switch (index){
case 1 :
pastDate = getPastDate(7);break;
case 2 :
pastDate= getPastMonth(1);break;
case 3 :
pastDate= getPastMonth(3);break;
case 4 :
pastDate= getPastMonth(6);break;
}
return pastDate;
}
/**
* 查询当前日期过去几天的时间
* @param past
* @return
*/
public static Date getPastDate(int past) {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - past);
Date today = calendar.getTime();
return today;
}
/**
* 查询当前日期过去几月的日期
* @param past
* @return
*/
public static Date getPastMonth(int past) {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) - past);
return calendar.getTime();
}
/**
* java 获取 获取某年某月 所有日期(yyyy-mm-dd格式字符串)
* @param year
* @param month
* @return
*/
public static List<String> getMonthFullDay(int year , int month){
SimpleDateFormat dateFormatYYYYMMDD = new SimpleDateFormat("yyyy-MM-dd");
List<String> fullDayList = new ArrayList<>(32);
// 获得当前日期对象
Calendar cal = Calendar.getInstance();
cal.clear();// 清除信息
cal.set(Calendar.YEAR, year);
// 1月从0开始
cal.set(Calendar.MONTH, month - 1);
// 当月1号
cal.set(Calendar.DAY_OF_MONTH, 1);
int count = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
for (int j = 1; j <= count ; j++) {
fullDayList.add(dateFormatYYYYMMDD.format(cal.getTime()));
cal.add(Calendar.DAY_OF_MONTH,1);
}
return fullDayList;
}
/**
* java 获取 当前时间 - 入参 = 结果时间 (yyyy-MM-dd HH:mm:ss 格式字符串)
* @param ihour
* @return
*/
public static String getBeforeHourTime(int ihour) {
String returnstr = "";
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) - ihour);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
returnstr = sdf.format(calendar.getTime());
return returnstr;
}
}
/**
* startTime - endTime = 日期差值
* result n 天 n 时 n 分
*/
public static String timeDifference(String startTime, String endTime) {
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String cha = "";
try {
Date d1 = df.parse("2021-04-15 10:31:40");
Date d2 = df.parse("2021-04-15 08:30:24");
//这样得到的差值是毫秒级别
long diff = d1.getTime() - d2.getTime();
long days = diff / (1000 * 60 * 60 * 24);
long hours = (diff - days * (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
long minutes = (diff - days * (1000 * 60 * 60 * 24) - hours * (1000 * 60 * 60)) / (1000 * 60);
// 返回日期差值
cha = "" + days + "天" + hours + "小时" + minutes + "分";
} catch (Exception e) {
e.printStackTrace();
}
return cha;
}
/**
* 数字日期格式 转 Date日期
*/
public static void main(String[] args) {
try {
String startTime = "20231025092641";
startTime = startTime.substring(0, 4) + "-" + startTime.substring(4, 6) + "-" + startTime.substring(6, 8) + " " + startTime.substring(8, 10) + ":" + startTime.substring(10, 12)+ ":" +startTime.substring(12,14);
System.out.println(startTime);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = sdf.parse(startTime);
System.out.println(date);
}catch (Exception e) {
e.printStackTrace();
}
}