package org.springblade.jfpt.parcel.util;
|
|
import java.text.ParseException;
|
import java.text.SimpleDateFormat;
|
import java.util.ArrayList;
|
import java.util.Calendar;
|
import java.util.Date;
|
import java.util.List;
|
|
/**
|
* 日期工具类
|
*/
|
public class DateUtils {
|
|
/**
|
* 获取今天
|
* @return String
|
* */
|
public static String getToday(){
|
return new SimpleDateFormat("yyyy-MM-dd").format(new Date());
|
}
|
/**
|
* 获取昨天
|
* @return String
|
* */
|
public static String getYestoday(){
|
Calendar cal=Calendar.getInstance();
|
cal.add(Calendar.DATE,-1);
|
Date time=cal.getTime();
|
return new SimpleDateFormat("yyyy-MM-dd").format(time);
|
}
|
/**
|
* 获取本月开始日期
|
* @return String
|
* **/
|
public static String getMonthStart(){
|
Calendar cal=Calendar.getInstance();
|
cal.add(Calendar.MONTH, 0);
|
cal.set(Calendar.DAY_OF_MONTH, 1);
|
Date time=cal.getTime();
|
return new SimpleDateFormat("yyyy-MM-dd").format(time);
|
}
|
/**
|
* 获取本月最后一天
|
* @return String
|
* **/
|
public static String getMonthEnd(){
|
Calendar cal=Calendar.getInstance();
|
cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
|
Date time=cal.getTime();
|
return new SimpleDateFormat("yyyy-MM-dd").format(time);
|
}
|
/**
|
* 获取本周的第一天
|
* @return String
|
* **/
|
public static String getWeekStart(){
|
Calendar cal=Calendar.getInstance();
|
cal.add(Calendar.WEEK_OF_MONTH, 0);
|
cal.set(Calendar.DAY_OF_WEEK, 2);
|
Date time=cal.getTime();
|
return new SimpleDateFormat("yyyy-MM-dd").format(time);
|
}
|
/**
|
* 获取本周的最后一天
|
* @return String
|
* **/
|
public static String getWeekEnd(){
|
Calendar cal=Calendar.getInstance();
|
cal.set(Calendar.DAY_OF_WEEK, cal.getActualMaximum(Calendar.DAY_OF_WEEK));
|
cal.add(Calendar.DAY_OF_WEEK, 1);
|
Date time=cal.getTime();
|
return new SimpleDateFormat("yyyy-MM-dd").format(time);
|
}
|
/**
|
* 获取本年的第一天
|
* @return String
|
* **/
|
public static String getYearStart(){
|
return new SimpleDateFormat("yyyy").format(new Date())+"-01-01";
|
}
|
|
/**
|
* 获取本年的最后一天
|
* @return String
|
* **/
|
public static String getYearEnd(){
|
Calendar calendar = Calendar.getInstance();
|
calendar.set(Calendar.MONTH,calendar.getActualMaximum(Calendar.MONTH));
|
calendar.set(Calendar.DAY_OF_MONTH,calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
|
Date currYearLast = calendar.getTime();
|
return new SimpleDateFormat("yyyy-MM-dd").format(currYearLast)+" 23:59:59";
|
}
|
|
/**
|
* 获取过去7天内的日期数组
|
* @return 日期数组
|
*/
|
public static ArrayList<String> pastDay(String time){
|
ArrayList<String> pastDaysList = new ArrayList<>();
|
try {
|
//我这里传来的时间是个string类型的,所以要先转为date类型的。
|
SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
|
Date date = sdf.parse(time);
|
for (int i = 6; i >= 0; i--) {
|
pastDaysList.add(getPastDate(i,date));
|
}
|
}catch (ParseException e){
|
e.printStackTrace();
|
}
|
return pastDaysList;
|
}
|
|
/**
|
* 获取过去第几天的日期
|
*
|
* @param past
|
* @return
|
*/
|
public static String getPastDate(int past,Date date) {
|
Calendar calendar = Calendar.getInstance();
|
calendar.setTime(date);
|
calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) - past);
|
Date today = calendar.getTime();
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
return sdf.format(today);
|
}
|
|
/**
|
* 获取时间段内每天的日期
|
*
|
* @param startDate 开始日期 yyyy-MM-dd HH:mm:ss
|
* @param endDate 结束日期 yyyy-MM-dd HH:mm:ss
|
* @return
|
*/
|
public static List<String> getTimeOverOneDate(String startDate, String endDate) {
|
List<String> times = new ArrayList<>();
|
try {
|
if (startDate.length()<11){
|
startDate = startDate+" 00:00:00";
|
endDate = endDate+" 23:59:59";
|
}
|
Date startTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(startDate);
|
Date endTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(endDate);
|
int gap = (int) ((endTime.getTime() - startTime.getTime()) / (24*60*60*1000));
|
times.clear();
|
Calendar cld = Calendar.getInstance();
|
cld.setTime(startTime);
|
for (int i = 0; i < gap +1; i++) {
|
times.add(new SimpleDateFormat("yyyy-MM-dd").format(cld.getTime()));
|
cld.add(Calendar.DATE,1);
|
}
|
return times;
|
} catch (ParseException e) {
|
e.printStackTrace();
|
}
|
return null;
|
}
|
|
}
|