日期处理工具类
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 DateTimeUtil {
public static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd";
public static final String DEFAULT_DATETIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
public static SimpleDateFormat formatter3 = new SimpleDateFormat("yyyy-MM");
private DateTimeUtil() {}
public static Date getNow() {
return Calendar.getInstance().getTime();
}
public static String getDate() {
return getDateTime(DEFAULT_DATE_FORMAT);
}
public static String getDateTime() {
return getDateTime(DEFAULT_DATETIME_FORMAT);
}
public static String getDateTimeToMonth() {
return formatter3.format(Calendar.getInstance().getTime());
}
public static String getDateTime(String pattern) {
Date datetime = Calendar.getInstance().getTime();
return getDateTime(datetime, pattern);
}
public static String getDateTime(Date date, String pattern) {
if (null == pattern || "".equals(pattern)) {
pattern = DEFAULT_DATETIME_FORMAT;
}
SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
return dateFormat.format(date);
}
public static String getLocalHourAndMin(Date date)
{
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return dateFormat.format(date);
}
public static int getCurrentYear() {
return Calendar.getInstance().get(Calendar.YEAR);
}
public static int getCurrentMonth() {
return Calendar.getInstance().get(Calendar.MONTH) + 1;
}
public static int getCurrentDay() {
return Calendar.getInstance().get(Calendar.DATE);
}
public static Date addDays(int days) {
return add(getNow(), days, Calendar.DATE);
}
public static Date addDays(Date date, int days) {
return add(date, days, Calendar.DATE);
}
public static Date addMonths(int months) {
return add(getNow(), months, Calendar.MONTH);
}
public static Date addMonths(Date date, int months) {
return add(date, months, Calendar.MONTH);
}
public static Date add(Date date, int amount, int field) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(field, amount);
return calendar.getTime();
}
public static long diffDays(Date one, Date two) {
return (one.getTime() - two.getTime()) / (24 * 60 * 60 * 1000);
}
public static int diffMonths(Date one, Date two) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(one);
int yearOne = calendar.get(Calendar.YEAR);
int monthOne = calendar.get(Calendar.MONDAY);
calendar.setTime(two);
int yearTwo = calendar.get(Calendar.YEAR);
int monthTwo = calendar.get(Calendar.MONDAY);
return (yearOne - yearTwo) * 12 + (monthOne - monthTwo);
}
public static Date parse(String datestr, String pattern) {
Date date = null;
if (null == pattern || "".equals(pattern)) {
pattern = DEFAULT_DATE_FORMAT;
}
try {
SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
date = dateFormat.parse(datestr);
}
catch (ParseException e) {
}
return date;
}
public static Date getMonthLastDay() {
return getMonthLastDay(getNow());
}
public static Date getMonthLastDay(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH) + 1, 1);
calendar.add(Calendar.DATE, -1);
return calendar.getTime();
}
public static String getPastDate(int past,Date date,String pattern) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) - past);
Date today = calendar.getTime();
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
String result = sdf.format(today);
return result;
}
public static ArrayList<String> pastDay(String time, String pattern){
ArrayList<String> pastDaysList = new ArrayList<>();
try {
SimpleDateFormat sdf= new SimpleDateFormat(pattern);
Date date = sdf.parse(time);
for (int i = 6; i >= 0; i--) {
pastDaysList.add(getPastDate(i,date,pattern));
}
}catch (ParseException e){
e.printStackTrace();
}
return pastDaysList;
}
public static List<String> findDateHours(String dBegin, String dEnd) {
Date begin=parse(dBegin,DEFAULT_DATE_FORMAT);
Date end=new Date(parse(dEnd,DEFAULT_DATE_FORMAT).getTime()+24*60*60*1000);
List lDate = new ArrayList();
Calendar calBegin = Calendar.getInstance();
calBegin.setTime(begin);
Calendar calEnd = Calendar.getInstance();
calEnd.setTime(end);
while (end.after(calBegin.getTime())) {
if (calBegin.getTime().after(new Date())){
break;
}
lDate.add(getDateTime(calBegin.getTime(),"YYYY-MM-dd HH"));
calBegin.add(Calendar.HOUR, 1);
}
return lDate;
}
public static long getDiffHours(String begin,String end){
Date dbegin=parse(begin,DEFAULT_DATETIME_FORMAT);
Date dend=parse(end,DEFAULT_DATETIME_FORMAT);
return (dend.getTime()-dbegin.getTime())/(60*60*1000);
}
public static long getDiffDays(String begin,String end){
Date dbegin=parse(begin,DEFAULT_DATE_FORMAT);
Date dend=parse(end,DEFAULT_DATE_FORMAT);
return (dend.getTime()-dbegin.getTime())/(24*60*60*1000);
}
public static String getLastDayOfMonth(int year,int month)
{
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR,year);
cal.set(Calendar.MONTH, month-1);
int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
cal.set(Calendar.DAY_OF_MONTH, lastDay);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String lastDayOfMonth = sdf.format(cal.getTime());
return lastDayOfMonth;
}
public static void main(String[] args) {
String dBegin="2020-12-17";
String dEnd="2020-12-24";
System.out.println(getDiffDays(dBegin,dEnd));
}
}