Java日期常用方法
- 获取当前日期的上月最后一天
public static String getPreMonthLastDay(Date date, String format) {
if(date == null) {
return null;
}
if(format == null) {
format = FORMAT_YYYY_MM_DD;
}
SimpleDateFormat dataFormat = new SimpleDateFormat(format);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.MONTH, -1);
calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
Date time = calendar.getTime();
return dataFormat.format(time);
}
- 获取某年第一天
public static String getYearFirst(int year, String format){
SimpleDateFormat format = new SimpleDateFormat(format);
Calendar currCal = Calendar.getInstance();
currCal.clear();
currCal.set(Calendar.YEAR, year);
Date currYearFirst = currCal.getTime();
return format.format(currYearFirst);
}
- 获取上月相同日期
public static String getLastMonthToday(String dateStr, String format) throws ParseException {
Calendar calendar = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat(format);
Date date = sdf.parse(dateStr);
calendar.setTime(date);
calendar.add(Calendar.MONTH, -1);
return sdf.format(calendar.getTime());
}
- 获取上月第一天
public static String getLastMonthFirstDay(String dateStr, String format) throws ParseException {
SimpleDateFormat dataFormat = new SimpleDateFormat(format);
Date date = dataFormat.parse(dateStr);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.MONTH, -1);
calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
Date time = calendar.getTime();
return dataFormat.format(time);
}
- 获取去年同期
public static String getLastYearMonth(String queryMonth,String formatStr) throws Exception{
SimpleDateFormat format = new SimpleDateFormat(formatStr);
Calendar c = Calendar.getInstance();
c.setTime(format.parse(queryMonth));
c.add(Calendar.YEAR, -1);
Date y = c.getTime();
return format.format(y);
}
- 计算两个日期相隔月份
public static long getMonthDiff(String start, String end, String format) {
LocalDate startDate = LocalDate.parse(start, DateTimeFormatter.ofPattern(format));
LocalDate endDate = LocalDate.parse(end, DateTimeFormatter.ofPattern(format));
long between = ChronoUnit.MONTHS.between(startDate, endDate);
return between;
}
- 计算当前日期上一季度的结束时间
public static String getLastQuarter(String dateStr, String formatStr) throws Exception {
SimpleDateFormat format = new SimpleDateFormat(formatStr);
Date date = format.parse(dateStr);
Calendar endCalendar = Calendar.getInstance();
endCalendar.setTime(date);
endCalendar.set(Calendar.DAY_OF_MONTH, endCalendar.getActualMinimum(Calendar.DAY_OF_MONTH));
endCalendar.set(Calendar.MONTH, ((int) endCalendar.get(Calendar.MONTH) / 3 - 1) * 3 + 2);
endCalendar.set(Calendar.DAY_OF_MONTH, endCalendar.getActualMaximum(Calendar.DAY_OF_MONTH));
endCalendar.set(Calendar.HOUR_OF_DAY, endCalendar.getActualMaximum(Calendar.HOUR_OF_DAY));
endCalendar.set(Calendar.MINUTE, endCalendar.getActualMaximum(Calendar.MINUTE));
endCalendar.set(Calendar.SECOND, endCalendar.getActualMaximum(Calendar.SECOND));
endCalendar.set(Calendar.MILLISECOND, endCalendar.getActualMaximum(Calendar.MILLISECOND));
return format.format(endCalendar.getTime());
}
- 获取指定年月的最后一天
public static String getLastDayOfMonth(int year, int month, String pattern) {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.MONTH, month-1);
calendar.set(Calendar.DAY_OF_MONTH, 1);
calendar.set(Calendar.DATE, calendar.getActualMaximum(Calendar.DATE));
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
return sdf.format(calendar.getTime());
}
- 计算两个日期的相隔天数
public static Long getDaysBetween(String start, String end, String pattern) {
long betweenDate = 0;
try {
SimpleDateFormat df = new SimpleDateFormat(pattern);
Date startDate = df.parse(start);
Date endDate = df.parse(end);
betweenDate = (endDate.getTime() - startDate.getTime()) / (24 * 60 * 60 * 1000);
} catch (Exception e) {
e.printStackTrace();
}
return betweenDate;
}
- 获取上季度的最后一月同期
public static String getPreQuarterToday(String dateStr, String format) throws Exception {
Calendar calendar = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat(format);
Date date = sdf.parse(dateStr);
calendar.setTime(date);
int subMonth = ((int) calendar.get(Calendar.MONTH) / 3 - 1) * 3 + 2;
calendar.add(Calendar.MONTH, -Math.abs(calendar.get(Calendar.MONTH) - subMonth));
return sdf.format(calendar.getTime());
}
- 计算上季度同期
public static String getPreQuarterToday(String dateStr, String format) throws Exception {
Calendar calendar = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat(format);
Date date = sdf.parse(dateStr);
calendar.setTime(date);
calendar.add(Calendar.MONTH, -3);
return sdf.format(calendar.getTime());
}
- 获取当前季度第一天
public static String getCurQuarterFirstDay(String dateStr, String format) throws ParseException {
Calendar calendar = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat(format);
Date date = sdf.parse(dateStr);
calendar.setTime(date);
int month = calendar.get(Calendar.MONTH) + 1;
int quarter = (month - 1) / 3 + 1;
calendar.set(Calendar.MONTH, (quarter - 1) * 3);
calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
return sdf.format(calendar.getTime());
}
- 获取当前季度最后一天
public static String getCurQuarterLastDay(String dateStr, String format) throws ParseException {
Calendar calendar = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat(format);
Date date = sdf.parse(dateStr);
calendar.setTime(date);
int month = calendar.get(Calendar.MONTH) + 1;
int quarter = (month - 1) / 3 + 1;
calendar.set(Calendar.MONTH, quarter * 3 - 1);
calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
return sdf.format(calendar.getTime());
}
- 获取指定日期的周一与周日日期
public static List<String> getWeekDate(String date, String format) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat(format);
Calendar cal = Calendar.getInstance();
cal.setTime(sdf.parse(date));
cal.setFirstDayOfWeek(Calendar.MONDAY);
int dayWeek = cal.get(Calendar.DAY_OF_WEEK);
if(dayWeek==1){
dayWeek = 8;
}
cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - dayWeek);
Date mondayDate = cal.getTime();
String weekBegin = sdf.format(mondayDate);
cal.add(Calendar.DATE, 4 +cal.getFirstDayOfWeek());
Date sundayDate = cal.getTime();
String weekEnd = sdf.format(sundayDate);
return Arrays.asList(weekBegin, weekEnd);
}