182 lines
5.6 KiB
Java
182 lines
5.6 KiB
Java
package com.base.util;
|
||
|
||
import java.text.DateFormat;
|
||
import java.text.ParseException;
|
||
import java.text.SimpleDateFormat;
|
||
import java.util.Calendar;
|
||
import java.util.Date;
|
||
import java.util.GregorianCalendar;
|
||
|
||
public class DateUtil {
|
||
public final static String FULL_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss.SSS";
|
||
public final static String TIME_MONTH = "MONTH";
|
||
public final static String TIME_DAY = "DAY";
|
||
public final static String TIME_YEAR = "YEAR";
|
||
|
||
public static int getIntervalDays(Date startDate, Date endDate) {
|
||
if ((null == startDate) || (null == endDate)) {
|
||
return -1;
|
||
}
|
||
long intervalMilli = (endDate.getTime() - startDate.getTime());
|
||
return (int) (intervalMilli / (24 * 60 * 60 * 1000));
|
||
}
|
||
|
||
@SuppressWarnings("static-access")
|
||
public static String getNexMonthDate(Date date, int iInterval, String returnForMat) {
|
||
Calendar calendar = new GregorianCalendar();
|
||
calendar.setTime(date);
|
||
calendar.add(calendar.MONTH, iInterval);// 把日期往后增加一个月.整数往后推,负数往前移动
|
||
date=calendar.getTime(); //这个时间就是日期往后推一天的结果
|
||
String sDate = DateUtil.getSystemTimeByFormat(date, returnForMat);
|
||
return sDate;
|
||
}
|
||
|
||
public static int daysOfTwo(Date startDate, Date endDate) {
|
||
Calendar aCalendar = Calendar.getInstance();
|
||
aCalendar.setTime(startDate);
|
||
int day1 = aCalendar.get(Calendar.DAY_OF_YEAR);
|
||
aCalendar.setTime(endDate);
|
||
int day2 = aCalendar.get(Calendar.DAY_OF_YEAR);
|
||
return day2 - day1;
|
||
}
|
||
|
||
public static int getMonthSpace(String date1, String date2)
|
||
throws ParseException {
|
||
|
||
int result = 0;
|
||
|
||
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
|
||
|
||
Calendar c1 = Calendar.getInstance();
|
||
Calendar c2 = Calendar.getInstance();
|
||
|
||
c1.setTime(sdf.parse(date1));
|
||
c2.setTime(sdf.parse(date2));
|
||
|
||
result = c2.get(Calendar.MONTH) - c1.get(Calendar.MONTH);
|
||
|
||
return result+( c2.get(Calendar.YEAR) - c1.get(Calendar.YEAR))*12;
|
||
|
||
}
|
||
|
||
public static String addDate(Date startDate, String type, Integer num) {
|
||
Calendar aCalendar = Calendar.getInstance();
|
||
aCalendar.setTime(startDate);
|
||
if (type.equalsIgnoreCase(DateUtil.TIME_DAY)) {
|
||
aCalendar.add(Calendar.DAY_OF_YEAR, num);
|
||
} else if (type.equalsIgnoreCase(DateUtil.TIME_MONTH)) {
|
||
aCalendar.add(Calendar.MONTH, num);
|
||
} else {
|
||
aCalendar.add(Calendar.YEAR, num);
|
||
}
|
||
return DateUtil
|
||
.getSystemTimeByFormat(aCalendar.getTime(), "yyyy-MM-dd");
|
||
}
|
||
|
||
public static String addDate(String startDate, String type, Integer num) {
|
||
Date dateStart = getTimeByFormat(startDate, "yyyy-MM-dd");
|
||
return addDate(dateStart, type, num);
|
||
}
|
||
|
||
public static Date getEndDateByDays(Date startDate, int days) {
|
||
Calendar aCalendar = Calendar.getInstance();
|
||
aCalendar.setTime(startDate);
|
||
aCalendar.add(Calendar.DAY_OF_YEAR, days);
|
||
return aCalendar.getTime();
|
||
}
|
||
|
||
public static String getSystemDate() {
|
||
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
||
return sdf.format(new Date());
|
||
}
|
||
|
||
public static String getSystemDateTime() {
|
||
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||
return sdf.format(new Date());
|
||
}
|
||
|
||
public static String getSystemDateDetailTime() {
|
||
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:sss");
|
||
return sdf.format(new Date());
|
||
}
|
||
|
||
public static String getDateTime(Date date) {
|
||
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||
if (null == date) {
|
||
date = new Date();
|
||
}
|
||
return sdf.format(date);
|
||
}
|
||
|
||
/**
|
||
* @param dateFormat
|
||
* 输出日期格式,形如:yyyy-MM-dd HH:mm:ss
|
||
* @return 格式化后的日期字符串
|
||
*/
|
||
public static String getSystemTimeByFormat(String dateFormat) {
|
||
SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
|
||
return sdf.format(new Date());
|
||
}
|
||
|
||
public static String getSystemTimeByFormat(Date date, String dateFormat) {
|
||
SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
|
||
return sdf.format(date);
|
||
}
|
||
|
||
public static String getTimeAllByFormat(String dateFormat)
|
||
throws ParseException {
|
||
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
||
Date date = sdf.parse(dateFormat);
|
||
DateFormat format = new SimpleDateFormat("yyyy年MM月dd日");
|
||
return format.format(date);
|
||
}
|
||
|
||
/**
|
||
* @param dateStr
|
||
* 日期字符串,形如:yyyy-MM-dd HH:mm:ss
|
||
* @param dateFormat
|
||
* 输出日期格式,形如:yyyy-MM-dd HH:mm:ss
|
||
* @return 格式化后的日期字符串
|
||
*/
|
||
public static Date getTimeByFormat(String dateStr, String dateFormat) {
|
||
SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
|
||
Date date = null;
|
||
try {
|
||
date = sdf.parse(dateStr);
|
||
} catch (ParseException e) {
|
||
e.printStackTrace();
|
||
}
|
||
return date;
|
||
}
|
||
|
||
/**
|
||
* 获取 yyyy-MM-dd 日期
|
||
*
|
||
* @param dateStr
|
||
* 日期时间字符串
|
||
* @param dateFormat
|
||
* dateStr的字符串格式
|
||
* @return ' yyyy-MM-dd '格式日期
|
||
*/
|
||
public static String getFormateDate(String dateStr, String dateFormat) {
|
||
SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
|
||
Date date = null;
|
||
try {
|
||
date = sdf.parse(dateStr);
|
||
} catch (ParseException e) {
|
||
e.printStackTrace();
|
||
}
|
||
sdf = new SimpleDateFormat("yyyy-MM-dd");
|
||
return sdf.format(date);
|
||
|
||
}
|
||
public static void main(String[] args) {
|
||
String date = getSystemTimeByFormat(new Date(), "yyyy/MM/dd HH:mm:ss");
|
||
System.out.println(date);
|
||
String []dateArray = date.split("/");
|
||
System.out.println("year :" + dateArray[0]);
|
||
System.out.println("month :" + dateArray[1]);
|
||
System.out.println("day :" + dateArray[2]);
|
||
}
|
||
}
|