博客原文:[点击阅读](https://www.zjhuiwan.cn/info/20200520/4010402516784361.html)
老是忘,还是记录下 -_-||
package zjhuiwan.cn.helper;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
/**
-
Created by on 2019/4/17.
*/
public class WeekMonthUtil {/**
- leftDate是否在rightDate之前或相等
- @param leftDate
- @param rightDate
- @return
*/
public static Boolean beforeOrEquals(Date leftDate, Date rightDate) {
return leftDate.before(rightDate) || leftDate.equals(rightDate);
}
/**
- leftDate是否在rightDate之后或相等
- @param leftDate
- @param rightDate
- @return
*/
public static Boolean afterOrEquals(Date leftDate, Date rightDate) {
return leftDate.after(rightDate) || leftDate.equals(rightDate);
}
/**
- 获取传入时间的当天0点
- @param date
- @return
*/
public static Date getDateZero(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
return calendar.getTime();
}
/**
- 获取传入时间的当天最后一秒
- @param date
- @return
*/
public static Date getDateLast(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(getDateZero(date));
calendar.add(Calendar.DAY_OF_MONTH, 1); // 一天后的时间
calendar.add(Calendar.SECOND, -1);
return calendar.getTime();
}
/**
- 格式化日期为字符串
- @param date
- @return
*/
public static String getDateStr(Date date, String fmt) {
SimpleDateFormat sdf = new SimpleDateFormat(fmt);
return sdf.format(date);
}
/**
- 格式化字符串为日期
- @param date
- @return
*/
public static Date getDateLast(String date, String fmt) {
SimpleDateFormat sdf = new SimpleDateFormat(fmt);
Date dateSdf = null;
try {
dateSdf = sdf.parse(date);
} catch (ParseException e) {
e.printStackTrace();
}
return dateSdf;
}
/**
- 获得本周一与当前日期相差的天数
- @param date
- @return
*/
public static int getMondayPlus(Date date) {
Calendar cd = Calendar.getInstance();
cd.setTime(date);
int dayOfWeek = cd.get(Calendar.DAY_OF_WEEK);
if (dayOfWeek == 1) {
return -6;
} else {
return 2 - dayOfWeek;
}
}
/**
- 获得当前周的周一
- @param date
- @return
*/
public static Date getCurrentMonday(Date date) {
int mondayPlus = getMondayPlus(date);
GregorianCalendar currentDate = new GregorianCalendar();
currentDate.add(GregorianCalendar.DATE, mondayPlus);
Date monday = currentDate.getTime();
return monday;
}
/**
- 获得当前周的周日
- @param date
- @return
*/
public static Date getPreviousSunday(Date date) {
int mondayPlus = getMondayPlus(date);
GregorianCalendar currentDate = new GregorianCalendar();
currentDate.add(GregorianCalendar.DATE, mondayPlus + 6);
Date monday = currentDate.getTime();
return monday;
}
/**
- 获得当前月的开始日期
- @param date
- @return
*/
public static Date getMinMonthDate(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
return calendar.getTime();
}
/**
- 获得当前月的结束日期
- @param date
- @return
*/
public static Date getMaxMonthDate(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
return calendar.getTime();
}
/**
- 获取上N个月
- @return
*/
public static Date getPreMonth(Date date, int n) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.MONTH, -1 * n);
return calendar.getTime();
}
/**
- 获取下N个月.
- @return
*/
public static Date getNextMonth(Date date, int n) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.MONTH, n);
return calendar.getTime();
}
}
网友评论