1、获取某年开始时间和结束时间
//获取某年开始时间
public static LocalDateTime getBeginDayOfYear(String date) {
LocalDate today;
if (null==date){
today = LocalDate.now();
}else {
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd");
today=LocalDate.parse(date, fmt);
}
today = LocalDate.of(today.getYear(), Month.JANUARY, 1);
return today.atTime(00,00,01);
}
//获取某年结束时间
public static LocalDateTime getEndDayOfYear(String date) {
LocalDate today;
if (null==date){
today = LocalDate.now();
}else {
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd");
today=LocalDate.parse(date, fmt);
}
today = LocalDate.of(today.getYear(), Month.DECEMBER, Month.DECEMBER.length(today.isLeapYear()));
return today.atTime(23,59,59);
}
2、获取某月开始时间和结束时间
//获取某月开始时间
public static Date getBeginDayOfMonth(String date) {
LocalDate date2;
if (null==date){
date2=LocalDate.now();
}else {
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd");
date2=LocalDate.parse(date, fmt);
}
Calendar calendar = Calendar.getInstance();
calendar.set(date2.getYear(), date2.getMonthValue()-1, 1);
return getDayStartTime(calendar.getTime());
}
//获取某月结束时间
public static Date getEndDayOfMonth(String date) {
LocalDate date2;
if (null==date){
date2=LocalDate.now();
}else {
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd");
date2=LocalDate.parse(date, fmt);
}
Calendar calendar = Calendar.getInstance();
calendar.set(date2.getYear(), date2.getMonthValue()-1, 1);
int day = calendar.getActualMaximum(5);
calendar.set(date2.getYear(), date2.getMonthValue()-1, day);
return getDayEndTime(calendar.getTime());
}
//--------公共方法-----------------------------------
//获取某个日期的开始时间
public static Timestamp getDayStartTime(Date d) {
Calendar calendar = Calendar.getInstance();
if(null != d) calendar.setTime(d);
calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
calendar.set(Calendar.MILLISECOND, 0);
return new Timestamp(calendar.getTimeInMillis());
}
//获取某个日期的结束时间
public static Timestamp getDayEndTime(Date d) {
Calendar calendar = Calendar.getInstance();
if(null != d) calendar.setTime(d);
calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 23, 59, 59);
calendar.set(Calendar.MILLISECOND, 999);
return new Timestamp(calendar.getTimeInMillis());
}
3、获取某周开始时间和结束时间
//获取本周的开始时间
public static Date getBeginDayOfWeek() {
Date date = new Date();
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int dayofweek = cal.get(Calendar.DAY_OF_WEEK);
if (dayofweek == 1) {
dayofweek += 7;
}
cal.add(Calendar.DATE, 2 - dayofweek);
return getDayStartTime(cal.getTime());
}
//获取本周的结束时间
public static Date getEndDayOfWeek(){
Calendar cal = Calendar.getInstance();
cal.setTime(getBeginDayOfWeek());
cal.add(Calendar.DAY_OF_WEEK, 6);
Date weekEndSta = cal.getTime();
return getDayEndTime(weekEndSta);
}
//---------------公共方法------------------------
//获取某个日期的开始时间
public static Timestamp getDayStartTime(Date d) {
Calendar calendar = Calendar.getInstance();
if(null != d) calendar.setTime(d);
calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
calendar.set(Calendar.MILLISECOND, 0);
return new Timestamp(calendar.getTimeInMillis());
}
//获取某个日期的结束时间
public static Timestamp getDayEndTime(Date d) {
Calendar calendar = Calendar.getInstance();
if(null != d) calendar.setTime(d);
calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 23, 59, 59);
calendar.set(Calendar.MILLISECOND, 999);
return new Timestamp(calendar.getTimeInMillis());
}
4、获取明天的开始时间和结束时间
//获取明天的开始时间
public static Date getBeginDayOfTomorrow() {
Calendar cal = new GregorianCalendar();
cal.setTime(getDayBegin());
cal.add(Calendar.DAY_OF_MONTH, 1);
return cal.getTime();
}
//获取明天的结束时间
public static Date getEndDayOfTomorrow() {
Calendar cal = new GregorianCalendar();
cal.setTime(getDayEnd());
cal.add(Calendar.DAY_OF_MONTH, 1);
return cal.getTime();
}
5、获取昨天的开始时间和结束时间
//获取昨天的开始时间
public static Date getBeginDayOfYesterday() {
Calendar cal = new GregorianCalendar();
cal.setTime(getDayBegin());
cal.add(Calendar.DAY_OF_MONTH, -1);
return cal.getTime();
}
//获取昨天的结束时间
public static Date getEndDayOfYesterDay() {
Calendar cal = new GregorianCalendar();
cal.setTime(getDayEnd());
cal.add(Calendar.DAY_OF_MONTH, -1);
return cal.getTime();
}
6、获取今天的开始时间和结束时间
//获取当天的开始时间
public static java.util.Date getDayBegin() {
Calendar cal = new GregorianCalendar();
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 java.util.Date getDayEnd() {
Calendar cal = new GregorianCalendar();
cal.set(Calendar.HOUR_OF_DAY, 23);
cal.set(Calendar.MINUTE, 59);
cal.set(Calendar.SECOND, 59);
return cal.getTime();
}
7、给时间加上几天/几小时/几分钟
/**
* 1、返回String
* 给时间date加上天数,小时,分钟
* @param date //原本时间
* @param day //要加的天数
* @param hour //要加的小时
* @param minute //要加的分钟
*/
public static String addDateTime(Date date, int day, int hour, int minute) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.DATE, day);// 24小时制,加天
cal.add(Calendar.HOUR, hour);// 24小时制 ,加小时
cal.add(Calendar.MINUTE, minute);// 24小时制,加分钟
date = cal.getTime();
String str_date = format.format(date);
return str_date;
}
/**
* 2、返回date
* 给时间date加上天数,小时,分钟
* @param date //需要加的时间
* @param day //要加的天数
* @param hour //要加的小时
* @param minute //要加的分钟
* 返回date
*/
public static Date addDateTime(Date date, int day, int hour, int minute) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.DATE, day);// 24小时制,加天
cal.add(Calendar.HOUR, hour);// 24小时制 ,加小时
cal.add(Calendar.MINUTE, minute);// 24小时制,加分钟
date = cal.getTime();
return date;
}