0. 全部
package com.wyc.utils;
import lombok.extern.slf4j.Slf4j;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
@Slf4j
public class DateUtil {
public static final String yyyy_MM_dd = "yyyy-MM-dd";
public static final String yyyy_MM_dd_HH_mm_ss = "yyyy-MM-dd HH:mm:ss";
public static String getTodayDate(){
SimpleDateFormat sdf = new SimpleDateFormat(yyyy_MM_dd);
return sdf.format(new Date());
}
public static String getTodayDate(String format){
SimpleDateFormat sdf = getSimpleDateFormat(format);
return sdf.format(new Date());
}
public static String getCurrentTime(){
SimpleDateFormat sdf = new SimpleDateFormat(yyyy_MM_dd_HH_mm_ss);
return sdf.format(new Date());
}
public static String getCurrentTime(String format){
SimpleDateFormat sdf = getSimpleDateFormat(format);
return sdf.format(new Date());
}
public static String getTomorrowDate(String format){
SimpleDateFormat sdf = getSimpleDateFormat(format);
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DATE, 1);
return sdf.format(calendar.getTime());
}
public static String getYesterdayDate(String format){
SimpleDateFormat sdf = getSimpleDateFormat(format);
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DATE, -1);
return sdf.format(calendar.getTime());
}
public static String getAfterDayDate(String dateTime, String format, int day){
SimpleDateFormat sdf = getSimpleDateFormat(format);
try {
Calendar calendar = Calendar.getInstance();
calendar.setTime(sdf.parse(dateTime));
calendar.add(Calendar.DATE,day);
return sdf.format(calendar.getTime());
} catch (ParseException e) {
log.error("================获取几天后的日期出错,错误信息:{}",e);
}
return "";
}
public static String getBeforeDayDate(String dateTime, String format, int day){
SimpleDateFormat sdf = getSimpleDateFormat(format);
try {
Calendar calendar = Calendar.getInstance();
calendar.setTime(sdf.parse(dateTime));
calendar.add(Calendar.DATE,-day);
return sdf.format(calendar.getTime());
} catch (ParseException e) {
log.error("================获取几天前的日期出错,错误信息:{}",e);
}
return "";
}
public static String changeDateFormat(String dateTime,String oldFormat, String newFormat){
SimpleDateFormat oldSdf = getSimpleDateFormat(oldFormat);
SimpleDateFormat newSdf = getSimpleDateFormat(newFormat);
try {
Date date = oldSdf.parse(dateTime);
return newSdf.format(date);
} catch (ParseException e) {
log.error("================转换日期格式出错,错误信息:{}",e);
}
return "";
}
public static String changeDateToStr(Date date, String format){
SimpleDateFormat sdf = getSimpleDateFormat(format);
return sdf.format(date);
}
public static Date changeStrToDate(String dateTime, String format){
SimpleDateFormat sdf = getSimpleDateFormat(format);
try {
return sdf.parse(dateTime);
} catch (ParseException e) {
log.error("================将字符串日期转换成Date类型日期出错,错误信息:{}",e);
}
return null;
}
public static int getAgeByBirth(String birthday, String format){
if (isEmpty(birthday)){
return 0;
}
SimpleDateFormat sdf = getSimpleDateFormat(format);
int age = 0;
try {
Date birthDate = sdf.parse(birthday);
Calendar cal = Calendar.getInstance();
Calendar bir = Calendar.getInstance();
bir.setTime(birthDate);
if (cal.before(birthDate)){
return 0;
}
int nowYear = cal.get(Calendar.YEAR);
int nowMonth = cal.get(Calendar.MONTH);
int nowDay = cal.get(Calendar.DAY_OF_MONTH);
int birYear = bir.get(Calendar.YEAR);
int birMonth = bir.get(Calendar.MONTH);
int birDay = bir.get(Calendar.DAY_OF_MONTH);
age = nowYear - birYear;
if (nowMonth < birMonth || (nowMonth == birMonth && nowDay < birDay)){
age--;
}
} catch (ParseException e) {
log.error("================根据出生日期获取年龄出错,错误信息:{}",e);
}
return age;
}
public static long getDifferDay(String startTime, String endTime){
if (isEmpty(startTime) || isEmpty(endTime)){
return 0;
}
SimpleDateFormat sdf = getSimpleDateFormat(yyyy_MM_dd);
try {
Date startDate = sdf.parse(startTime);
Date endDate = sdf.parse(endTime);
long result = (endDate.getTime() - startDate.getTime()) / (24 * 60 * 60 * 1000);
return Math.abs(result);
} catch (ParseException e) {
log.error("================获取两个日期相隔多少天出错,错误信息:{}",e);
}
return 0;
}
public static long getDifferTime(String startTime, String endTime, String format){
if (isEmpty(startTime) || isEmpty(endTime)){
return 0L;
}
SimpleDateFormat sdf = getSimpleDateFormat(format);
try {
Date startDate = sdf.parse(startTime);
Date endDate = sdf.parse(endTime);
long differ = endDate.getTime() - startDate.getTime();
return differ;
} catch (ParseException e) {
log.error("================获取两个日期相隔多长时间出错,错误信息:{}",e);
}
return 0L;
}
public static Map<String,Long> getDetailDifferTime(String startTime, String endTime, String format){
Map<String, Long> map = new HashMap<>();
long day = 0L;
long hour = 0L;
long minute = 0L;
long seconds = 0L;
if (!isEmpty(startTime) && !isEmpty(endTime)){
long nd = 1000 * 24 * 60 * 60;
long nh = 1000 * 60 * 60;
long nm = 1000 * 60;
long ns = 1000;
long differ = getDifferTime(startTime, endTime, format);
day = differ / nd;
hour = differ % nd / nh;
minute = differ % nd % nh / nm;
seconds = differ % nd % nh % nm / ns;
}
map.put("day",day);
map.put("hour",hour);
map.put("minute",minute);
map.put("seconds",seconds);
return map;
}
public static boolean afterDate(String time1, String time2, String format){
SimpleDateFormat sdf = getSimpleDateFormat(format);
try {
Date date1 = sdf.parse(time1);
Date date2 = sdf.parse(time2);
return date1.after(date2);
} catch (ParseException e) {
log.error("================判断两个日期的前后顺序时间出错,错误信息:{}",e);
}
return false;
}
public static SimpleDateFormat getSimpleDateFormat(String format) {
if (isEmpty(format)) {
format = yyyy_MM_dd;
}
return new SimpleDateFormat(format);
}
private static boolean isEmpty(String str){
if (str == null || "".equals(str.trim())){
return true;
}
return false;
}
}
1. 获取今天的日期
public static String getTodayDate(){
SimpleDateFormat sdf = new SimpleDateFormat(yyyy_MM_dd);
return sdf.format(new Date());
}
public static String getTodayDate(String format){
SimpleDateFormat sdf = getSimpleDateFormat(format);
return sdf.format(new Date());
}
2. 获取当前时间
public static String getCurrentTime(){
SimpleDateFormat sdf = new SimpleDateFormat(yyyy_MM_dd_HH_mm_ss);
return sdf.format(new Date());
}
public static String getCurrentTime(String format){
SimpleDateFormat sdf = getSimpleDateFormat(format);
return sdf.format(new Date());
}
3. 获取明天的日期
public static String getTomorrowDate(String format){
SimpleDateFormat sdf = getSimpleDateFormat(format);
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DATE, 1);
return sdf.format(calendar.getTime());
}
4. 获取昨天的日期
public static String getYesterdayDate(String format){
SimpleDateFormat sdf = getSimpleDateFormat(format);
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DATE, -1);
return sdf.format(calendar.getTime());
}
5. 获取几天后的日期
public static String getAfterDayDate(String dateTime, String format, int day){
SimpleDateFormat sdf = getSimpleDateFormat(format);
try {
Calendar calendar = Calendar.getInstance();
calendar.setTime(sdf.parse(dateTime));
calendar.add(Calendar.DATE,day);
return sdf.format(calendar.getTime());
} catch (ParseException e) {
log.error("================获取几天后的日期出错,错误信息:{}",e);
}
return "";
}
6. 获取几天前的日期
public static String getBeforeDayDate(String dateTime, String format, int day){
SimpleDateFormat sdf = getSimpleDateFormat(format);
try {
Calendar calendar = Calendar.getInstance();
calendar.setTime(sdf.parse(dateTime));
calendar.add(Calendar.DATE,-day);
return sdf.format(calendar.getTime());
} catch (ParseException e) {
log.error("================获取几天前的日期出错,错误信息:{}",e);
}
return "";
}
7. 转换日期格式
public static String changeDateFormat(String dateTime,String oldFormat, String newFormat){
SimpleDateFormat oldSdf = getSimpleDateFormat(oldFormat);
SimpleDateFormat newSdf = getSimpleDateFormat(newFormat);
try {
Date date = oldSdf.parse(dateTime);
return newSdf.format(date);
} catch (ParseException e) {
log.error("================转换日期格式出错,错误信息:{}",e);
}
return "";
}
8. 将Date类型日期转换成字符串类型日期
public static String changeDateToStr(Date date, String format){
SimpleDateFormat sdf = getSimpleDateFormat(format);
return sdf.format(date);
}
9. 将字符串日期转换成Date类型日期
public static Date changeStrToDate(String dateTime, String format){
SimpleDateFormat sdf = getSimpleDateFormat(format);
try {
return sdf.parse(dateTime);
} catch (ParseException e) {
log.error("================将字符串日期转换成Date类型日期出错,错误信息:{}",e);
}
return null;
}
10. 根据出生日期获取年龄
public static int getAgeByBirth(String birthday, String format){
if (isEmpty(birthday)){
return 0;
}
SimpleDateFormat sdf = getSimpleDateFormat(format);
int age = 0;
try {
Date birthDate = sdf.parse(birthday);
Calendar cal = Calendar.getInstance();
Calendar bir = Calendar.getInstance();
bir.setTime(birthDate);
if (cal.before(birthDate)){
return 0;
}
int nowYear = cal.get(Calendar.YEAR);
int nowMonth = cal.get(Calendar.MONTH);
int nowDay = cal.get(Calendar.DAY_OF_MONTH);
int birYear = bir.get(Calendar.YEAR);
int birMonth = bir.get(Calendar.MONTH);
int birDay = bir.get(Calendar.DAY_OF_MONTH);
age = nowYear - birYear;
if (nowMonth < birMonth || (nowMonth == birMonth && nowDay < birDay)){
age--;
}
} catch (ParseException e) {
log.error("================根据出生日期获取年龄出错,错误信息:{}",e);
}
return age;
}
11. 获取两个时间相差多少天(日期格式yyyy-MM-dd)
public static long getDifferDay(String startTime, String endTime){
if (isEmpty(startTime) || isEmpty(endTime)){
return 0;
}
SimpleDateFormat sdf = getSimpleDateFormat(yyyy_MM_dd);
try {
Date startDate = sdf.parse(startTime);
Date endDate = sdf.parse(endTime);
long result = (endDate.getTime() - startDate.getTime()) / (24 * 60 * 60 * 1000);
return Math.abs(result);
} catch (ParseException e) {
log.error("================获取两个日期相隔多少天出错,错误信息:{}",e);
}
return 0;
}
12. 获取两个时间相差多少(返回的是毫秒)
public static long getDifferTime(String startTime, String endTime, String format){
if (isEmpty(startTime) || isEmpty(endTime)){
return 0L;
}
SimpleDateFormat sdf = getSimpleDateFormat(format);
try {
Date startDate = sdf.parse(startTime);
Date endDate = sdf.parse(endTime);
long differ = endDate.getTime() - startDate.getTime();
return differ;
} catch (ParseException e) {
log.error("================获取两个日期相隔多长时间出错,错误信息:{}",e);
}
return 0L;
}
13. 获取两个时间相差多少详细信息
public static Map<String,Long> getDetailDifferTime(String startTime, String endTime, String format){
Map<String, Long> map = new HashMap<>();
long day = 0L;
long hour = 0L;
long minute = 0L;
long seconds = 0L;
if (!isEmpty(startTime) && !isEmpty(endTime)){
long nd = 1000 * 24 * 60 * 60;
long nh = 1000 * 60 * 60;
long nm = 1000 * 60;
long ns = 1000;
long differ = getDifferTime(startTime, endTime, format);
day = differ / nd;
hour = differ % nd / nh;
minute = differ % nd % nh / nm;
seconds = differ % nd % nh % nm / ns;
}
map.put("day",day);
map.put("hour",hour);
map.put("minute",minute);
map.put("seconds",seconds);
return map;
}
14. 判断两个日期的前后顺序
public static boolean afterDate(String time1, String time2, String format){
SimpleDateFormat sdf = getSimpleDateFormat(format);
try {
Date date1 = sdf.parse(time1);
Date date2 = sdf.parse(time2);
return date1.after(date2);
} catch (ParseException e) {
log.error("================判断两个日期的前后顺序时间出错,错误信息:{}",e);
}
return false;
}