有时候会遇到31日执行日历类 去取当月的最后一天会发现到了下月的第一天
比如7月31日 去计算 月份的最后一天 发现有31天的到了要下个月的一号了
package com.zoomlion.dataCollector.utils;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public abstract class DateUtils {
private static final String DEFAULT_PATTERN = "yyyyMMddhhmmss";
public static String getSystemDate() {
return formatDate(getSysDateTime(), DEFAULT_PATTERN);
}
public static String formatDate(Date date, String pattern){
DateFormat df = new SimpleDateFormat(pattern);
return df.format(date);
}
public static Date parseDate(String dateStr, String pattern){
DateFormat df = new SimpleDateFormat(pattern);
try {
return df.parse(dateStr);
} catch (ParseException e) {
throw new RuntimeException("Error while parse date", e);
}
}
public static Date getSysDateTime(){
return new Date();
}
public static Date getSysDate(){
return getDate(getSysDateTime());
}
public static Date getDate(Date date){
if (date == null) return null;
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
return cal.getTime();
}
public static Date add(Date date, int dateField, int mount){
if (date == null) return null;
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(dateField, mount);
return cal.getTime();
}
public static int getYear(Date date){
Calendar cal = Calendar.getInstance();
cal.setTime(date);
return cal.get(Calendar.YEAR);
}
public static int getMonth(Date date){
Calendar cal = Calendar.getInstance();
cal.setTime(date);
return cal.get(Calendar.MONTH)+1;
}
public static int getDayOfMonth(Date date){
Calendar cal = Calendar.getInstance();
cal.setTime(date);
return cal.get(Calendar.DAY_OF_MONTH);
}
/**
* Sunday=1
* SATURDAY=7
* @param date
* @return
*/
public static int getDayOfWeek(Date date){
Calendar cal = Calendar.getInstance();
cal.setTime(date);
return cal.get(Calendar.DAY_OF_WEEK);
}
public static int getDayOfYear(Date date){
Calendar cal = Calendar.getInstance();
cal.setTime(date);
return cal.get(Calendar.DAY_OF_YEAR);
}
public static int getHour(Date date){
Calendar cal = Calendar.getInstance();
cal.setTime(date);
return cal.get(Calendar.HOUR_OF_DAY);
}
public static int getMinute(Date date){
Calendar cal = Calendar.getInstance();
cal.setTime(date);
return cal.get(Calendar.MINUTE);
}
public static int getSecond(Date date){
Calendar cal = Calendar.getInstance();
cal.setTime(date);
return cal.get(Calendar.SECOND);
}
public static int getMillSecond(Date date){
Calendar cal = Calendar.getInstance();
cal.setTime(date);
return cal.get(Calendar.MILLISECOND);
}
public static Timestamp getTimestamp(Date date){
return new Timestamp(date.getTime());
}
public static java.sql.Date getSqlDate(Date date){
return new java.sql.Date(date.getTime());
}
public static Date getDate(int year, int month, int day){
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, month-1);
cal.set(Calendar.DAY_OF_MONTH, day);
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
return cal.getTime();
}
public static Date getDate(int year, int month, int day, int hour, int min, int second){
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, month-1);
cal.set(Calendar.DAY_OF_MONTH, day);
cal.set(Calendar.HOUR_OF_DAY, hour);
cal.set(Calendar.MINUTE, min);
cal.set(Calendar.SECOND, second);
cal.set(Calendar.MILLISECOND, 0);
return cal.getTime();
}
public static Date getFirstDateOfMonth(int year, int month){
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, month-1);
cal.set(Calendar.DAY_OF_MONTH, 1);
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
return cal.getTime();
}
public static Date getLastDateOfMonth(int year, int month){
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, month);
//cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
cal.set(Calendar.DAY_OF_MONTH, 1);//设置该月1号
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
cal.add(Calendar.DATE, -1);//使用add减一天
return cal.getTime();
}
public static Date getFirstDateOfYear(int year){
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, year);
cal.set(Calendar.DAY_OF_YEAR, 1);
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
return cal.getTime();
}
public static Date getLastDateOfYear(int year){
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, year);
cal.set(Calendar.DAY_OF_YEAR, cal.getActualMaximum(Calendar.DAY_OF_YEAR));
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
return cal.getTime();
}
public static Date getPreviousDate(Date date){
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.set(Calendar.DAY_OF_MONTH, cal.get(Calendar.DAY_OF_MONTH)-1);
return cal.getTime();
}
public static Date getNextDate(Date date){
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.set(Calendar.DAY_OF_MONTH, cal.get(Calendar.DAY_OF_MONTH)+1);
return cal.getTime();
}
public static Date getFirstDateOfMonth(Date date){
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH);
return getFirstDateOfMonth(year, month);
}
public static Date getLastDateOfMonth(Date date){
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH);
return getLastDateOfMonth(year, month);
}
/**
* 获得给定日期当周的WeekDay
* @param date
* @param weekOfDay 星期一是1,星期天是7
* @return
*/
public static Date getWeekDate(Date date, int weekOfDay){
Calendar cal = Calendar.getInstance();
cal.setTime(date);
if (weekOfDay < 7){
cal.set(Calendar.DAY_OF_WEEK, weekOfDay+1);
}else{
cal.set(Calendar.DAY_OF_WEEK, weekOfDay);//星期六
cal.add(Calendar.DATE, weekOfDay-6);//往后推一天到周日
}
return cal.getTime();
}
/**
* 两个时间相差毫秒数
* @param start
* @param end
* @return
*/
public static Long minusMilliseconds(Date start, Date end){
if (start.after(end)){//交换
Date tmp = start;
start = end;
end = tmp;
}
return end.getTime()-start.getTime();
}
public static Long minusSeconds(Date start, Date end){
return minusMilliseconds(start, end)/1000;
}
public static Long minusMinutes(Date start, Date end){
return minusMilliseconds(start, end)/(1000*60);
}
public static Long minusHours(Date start, Date end){
return minusMilliseconds(start, end)/(1000*60*60);
}
public static Long minusDay(Date start, Date end){
return minusMilliseconds(start, end)/(1000*60*60*24);
}
}