package com.lf.common;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
/**
* 日期工具类
*/
public class DateUtils {
/**
* 获取当前时间
*/
public static LocalDateTime getNow(){
return LocalDateTime.now();
}
/**
* 获取今日日期
*/
public static LocalDate getToday(){
return LocalDate.now();
}
/**
* 获取今天
* @return String
* */
public static String getToday(){
return new SimpleDateFormat("yyyy-MM-dd").format(new Date());
}
/**
* 获取昨天
* @return String
* */
public static String getYestoday(){
Calendar cal=Calendar.getInstance();
cal.add(Calendar.DATE,-1);
Date time=cal.getTime();
return new SimpleDateFormat("yyyy-MM-dd").format(time);
}
/**
* 获取本月开始日期
* @return String
*/
public static String getMonthStart(){
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.MONTH,0);
calendar.set(Calendar.DAY_OF_MONTH,1);
Date time = calendar.getTime();
return new SimpleDateFormat("yyyy-MM-dd").format(time);
}
/**
* 获取本月最后一天
* @return String
*/
public static String getMonthEnd(){
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_MONTH,calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
Date time = calendar.getTime();
return new SimpleDateFormat("yyyy-MM-dd").format(time);
}
/**
* 获取本周的第一天
* @return
*/
public static String getWeekStart(){
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.WEEK_OF_MONTH,0);
calendar.set(Calendar.DAY_OF_WEEK,2);
Date time=calendar.getTime();
return new SimpleDateFormat("yyyy-MM-dd").format(time);
}
/**
* 获取本周的最后一天
* @return String
* **/
public static String getWeekEnd(){
Calendar cal=Calendar.getInstance();
cal.set(Calendar.DAY_OF_WEEK, cal.getActualMaximum(Calendar.DAY_OF_WEEK));
cal.add(Calendar.DAY_OF_WEEK, 1);
Date time=cal.getTime();
return new SimpleDateFormat("yyyy-MM-dd").format(time);
}
/**
* 获取本年的第一天
* @return String
* **/
public static String getYearStart(){
return new SimpleDateFormat("yyyy").format(new Date())+"-01-01";
}
/**
* 获取本年的最后一天
* @return String
* **/
public static String getYearEnd(){
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.MONTH,calendar.getActualMaximum(Calendar.MONTH));
calendar.set(Calendar.DAY_OF_MONTH,calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
Date currYearLast = calendar.getTime();
return new SimpleDateFormat("yyyy-MM-dd").format(currYearLast);
}
public static Date LocalDateToUdate(LocalDate localDate) {
ZoneId zone = ZoneId.systemDefault();
Instant instant = localDate.atStartOfDay().atZone(zone).toInstant();
return Date.from(instant);
}
public static Date LocalDateTimeToUdate(LocalDateTime localDateTime) {
ZoneId zone = ZoneId.systemDefault();
Instant instant = localDateTime.atZone(zone).toInstant();
return Date.from(instant);
}
public static LocalDateTime stringToLocalDateTime(String str){
DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
return LocalDateTime.parse(str,df);
}
public static Integer getAge(Date birthDay) {
int age = 0;
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); //当前月份
int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH); //当前日期
cal.setTime(birthDay);
int yearBirth = cal.get(Calendar.YEAR);
int monthBirth = cal.get(Calendar.MONTH);
int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH);
age = yearNow - yearBirth; //计算整岁数
if (monthNow <= monthBirth) {
if (monthNow == monthBirth) {
if (dayOfMonthNow < dayOfMonthBirth) age--;//当前日期在生日之前,年龄减一
} else {
age--;//当前月份在生日之前,年龄减一
}
}
return age;
}
/**
* date2比date1多的天数
* @param date1
* @param date2
* @return
*/
public static Integer differentDays(Date date1, Date date2) {
Calendar cal = Calendar.getInstance();
cal.setTime(date1);
long time1 = cal.getTimeInMillis();
cal.setTime(date2);
long time2 = cal.getTimeInMillis();
long between_days=(time2-time1)/(1000*3600*24);
return Integer.parseInt(String.valueOf(between_days));
}
}
JAVA获取日期、时间
最新推荐文章于 2024-04-30 10:00:00 发布