JAVA 日期工具类
- 获取某天00:00:00点的时间戳
- 日期获取周
- 日期获取星期几
- 获取时间戳属于当月的哪天
- 获取指定周周一周日
- 获取某月月初月末日期
- 根据星期几获取日期
- 获取年月日,时分秒
- 根据day获取所在周,周一周日
- 根据day获取所在月,月初月末日期
- 根据day获取上月月末的时间戳
- 日期时间戳字符串转换
- 日期加减等
源码
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.5</version>
</dependency>
<dependency>
<groupId>org.projectlombok</gr.oupId>
<artifactId>lombok</artifactId>.1
<version>1.18.8</version>
</dependency>
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.time.DateFormatUtils;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
@Slf4j
public class TimeUtils {
public static final long ONEDAYMILLIS = 24 * 60 * 60 * 1000;
public static final String Y_M_D = "yyyy-MM-dd";
public static final String YMD = "yyyyMMdd";
public static final String YMDHMS = "yyyy-MM-dd HH:mm:ss.SSS";
public static long getBeginMillisOfDay() {
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
return calendar.getTimeInMillis();
}
public static long getBeginMillisOfDay(long millis) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date(millis));
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
return calendar.getTimeInMillis();
}
public String getWeekOfDate(Date date) {
String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
if (w < 0)
w = 0;
return weekDays[w];
}
public static int getIntWeekOfDate(long millis) {
Calendar cal = Calendar.getInstance();
cal.setTime(new Date(millis));
return cal.get(Calendar.DAY_OF_WEEK);
}
public static Integer getDayOfMonth(long millis) {
Calendar cal = Calendar.getInstance();
cal.setTime(new Date(millis));
int w = cal.get(Calendar.DAY_OF_MONTH);
return w;
}
public static String getYmd(Long epochSecond) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
return sdf.format(new Date(epochSecond));
}
public static String getYm(Long millis) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
return sdf.format(new Date(millis));
}
public static long getLastMonthMills(Long epochSecond) {
SimpleDateFormat sdf = new SimpleDateFormat(Y_M_D);
Calendar cale = Calendar.getInstance();
if (epochSecond != null && epochSecond != 0L) {
Date date = new Date();
date.setTime(epochSecond);
cale.setTime(date);
}
int firstDay = cale.getActualMinimum(Calendar.DAY_OF_MONTH);
cale.set(Calendar.DAY_OF_MONTH, firstDay);
return cale.getTimeInMillis() - ONEDAYMILLIS;
}
public static String getMonthFirstDay(Long epochSecond) {
SimpleDateFormat sdf = new SimpleDateFormat(Y_M_D);
Calendar cale = Calendar.getInstance();
if (epochSecond != null && epochSecond != 0L) {
Date date = new Date();
date.setTime(epochSecond);
cale.setTime(date);
}
int firstDay = cale.getActualMinimum(Calendar.DAY_OF_MONTH);
cale.set(Calendar.DAY_OF_MONTH, firstDay);
return sdf.format(cale.getTime());
}
public static String getMonthLastDay(Long epochSecond) {
SimpleDateFormat sdf = new SimpleDateFormat(Y_M_D);
Calendar cale = Calendar.getInstance();
if (epochSecond != null && epochSecond != 0L) {
Date date = new Date();
date.setTime(epochSecond);
cale.setTime(date);
}
int lastDay = cale.getActualMaximum(Calendar.DAY_OF_MONTH);
cale.set(Calendar.DAY_OF_MONTH, lastDay);
return sdf.format(cale.getTime());
}
public static String getMonthFirstDay(String day, String pattern) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
Calendar cale = Calendar.getInstance();
Date date = sdf.parse(day);
cale.setTime(date);
int firstDay = cale.getActualMinimum(Calendar.DAY_OF_MONTH);
cale.set(Calendar.DAY_OF_MONTH, firstDay);
return sdf.format(cale.getTime());
}
public static String getMonthLastDay(String day, String pattern) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
Calendar cale = Calendar.getInstance();
Date date = sdf.parse(day);
cale.setTime(date);
int lastDay = cale.getActualMaximum(Calendar.DAY_OF_MONTH);
cale.set(Calendar.DAY_OF_MONTH, lastDay);
return sdf.format(cale.getTime());
}
public static Date getDateByWeek(int week) {
int targetWeek = week;
Calendar c = Calendar.getInstance();
int currWeek = c.get(Calendar.DAY_OF_WEEK);
do {
if (currWeek == targetWeek) {
c.add(Calendar.DAY_OF_MONTH, 7);
} else if (currWeek < week) {
currWeek--;
}
} while (currWeek != targetWeek);
return c.getTime();
}
public static String getLastDay(int year, int month, String format) {
Calendar cale = Calendar.getInstance();
cale.set(Calendar.YEAR, year);
cale.set(Calendar.MONTH, month - 1);
int lastDay = cale.getActualMaximum(Calendar.DAY_OF_MONTH);
cale.set(Calendar.DAY_OF_MONTH, lastDay);
SimpleDateFormat sdf = new SimpleDateFormat(format);
String lastDayOfMonth = sdf.format(cale.getTime());
return lastDayOfMonth;
}
public static String getFirstDay(Long timeSecs, String format) {
Calendar cale = Calendar.getInstance();
if (timeSecs != null && timeSecs != 0L) {
Date date = new Date();
date.setTime(timeSecs);
cale.setTime(date);
}
int lastDay = cale.getActualMinimum(Calendar.DAY_OF_MONTH);
cale.set(Calendar.DAY_OF_MONTH, lastDay);
SimpleDateFormat sdf = new SimpleDateFormat(format);
String lastDayOfMonth = sdf.format(cale.getTime());
return lastDayOfMonth;
}
public static String getFirstDay(int year, int month, String format) {
Calendar cale = Calendar.getInstance();
cale.set(Calendar.YEAR, year);
cale.set(Calendar.MONTH, month - 1);
int lastDay = cale.getActualMinimum(Calendar.DAY_OF_MONTH);
cale.set(Calendar.DAY_OF_MONTH, lastDay);
SimpleDateFormat sdf = new SimpleDateFormat(format);
String lastDayOfMonth = sdf.format(cale.getTime());
return lastDayOfMonth;
}
public static String getLastDay(Long timeSecs, String format) {
Calendar cale = Calendar.getInstance();
if (timeSecs != null && timeSecs != 0L) {
Date date = new Date();
date.setTime(timeSecs);
cale.setTime(date);
}
int lastDay = cale.getActualMaximum(Calendar.DAY_OF_MONTH);
cale.set(Calendar.DAY_OF_MONTH, lastDay);
SimpleDateFormat sdf = new SimpleDateFormat(format);
String lastDayOfMonth = sdf.format(cale.getTime());
return lastDayOfMonth;
}
public static int getWeek(Date date) {
GregorianCalendar g = new GregorianCalendar();
g.setTime(date);
return g.get(Calendar.WEEK_OF_YEAR);
}
public static String getWeek(String day) throws ParseException {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(YMD);
Date date = simpleDateFormat.parse(day);
GregorianCalendar g = new GregorianCalendar();
g.setTime(date);
return day.substring(0, 4) + g.get(Calendar.WEEK_OF_YEAR);
}
public static String format(Calendar c, String pattern) {
Calendar calendar = null;
if (c != null) {
calendar = c;
} else {
calendar = Calendar.getInstance();
}
if (pattern == null || pattern.equals("")) {
pattern = YMD;
}
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
return sdf.format(calendar.getTime());
}
public static String getWeekFirstDay(String dateStr, String pattern) throws ParseException {
Calendar strDate = Calendar.getInstance();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(YMD);
Date date = simpleDateFormat.parse(dateStr);
strDate.setTime(date);
int day = getIntWeekOfDate(strDate.getTimeInMillis());
strDate.add(Calendar.DATE, -(day - 2));
return format(strDate, pattern);
}
public static String getWeekLastDay(String dateStr, String pattern) throws ParseException {
Calendar strDate = Calendar.getInstance();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(YMD);
Date date = simpleDateFormat.parse(dateStr);
strDate.setTime(date);
int day = getIntWeekOfDate(strDate.getTimeInMillis());
strDate.add(Calendar.DATE, 8 - day);
return format(strDate, pattern);
}
public static Date getFirstDayOfWeek(int year, int week) {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, year);
cal.set(Calendar.WEEK_OF_YEAR, week + 1);
cal.setFirstDayOfWeek(Calendar.MONDAY);
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);
return cal.getTime();
}
public static Date getLastDayOfWeek(int year, int week) {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, year);
cal.set(Calendar.WEEK_OF_YEAR, week + 1);
cal.setFirstDayOfWeek(Calendar.MONDAY);
cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
cal.set(Calendar.HOUR_OF_DAY, 23);
cal.set(Calendar.MINUTE, 59);
cal.set(Calendar.SECOND, 59);
return cal.getTime();
}
public static void main(String[] args) throws ParseException {
log.info("{} {}", System.currentTimeMillis(), System.currentTimeMillis() - 30 * 24 * 60 * 60 * 1000);
log.info("{} {}", getFirstDay(null, Y_M_D), getLastDay(null, Y_M_D));
log.info("{} {}", getFirstDay(1668654801114L, Y_M_D), getLastDay(1668654801114L, Y_M_D));
log.info("{}", DateFormatUtils.format(1668654590343L, Y_M_D));
log.info("{}", TimeUtils.getLastMonthMills(System.currentTimeMillis()));
log.info("{} {}", TimeUtils.getMonthFirstDay(System.currentTimeMillis()), TimeUtils.getMonthLastDay(System.currentTimeMillis()));
log.info("{} {}", TimeUtils.getMonthFirstDay(TimeUtils.getLastMonthMills(System.currentTimeMillis())),
TimeUtils.getMonthLastDay(TimeUtils.getLastMonthMills(System.currentTimeMillis())));
log.info("{} {}", getWeek(new Date()), getWeek("20221103"));
log.info("{} {}", getWeekFirstDay("20221103", YMD), getWeekLastDay("20221105", YMD));
log.info("{} {}", getWeekFirstDay("20220913", YMD), getWeekLastDay("20220916", YMD));
log.info("{} {}", getMonthFirstDay("20220913", YMD), getMonthLastDay("20220916", YMD));
log.info("{} {}", getMonthFirstDay("20221102", YMD), getMonthLastDay("20221105", YMD));
log.info("{} {}", getFirstDayOfWeek(2022, 52), getLastDayOfWeek(2023, 2));
log.info("{} {}", getFirstDayOfWeek(2023, 1), getLastDayOfWeek(2023, 2));
log.info("{} {}", DateFormatUtils.format(getFirstDayOfWeek(2022, 52), YMD), DateFormatUtils.format(getLastDayOfWeek(2023, 2), YMD));
log.info("{} {}", DateFormatUtils.format(getFirstDayOfWeek(2023, 1), YMD), DateFormatUtils.format(getLastDayOfWeek(2023, 2), YMD));
}
}