package its.utcs.support;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
/**
* 日期工具类
* @author
* @since 2016-06-22
*/
public class DateUtil {
public static final String DATEYEAR_DEFAULT_FORMAT = "yyyy";
public static final String DATEMONTH_DEFAULT_FORMAT = "yyyy-MM";
// 默认日期格式
public static final String DATE_DEFAULT_FORMAT = "yyyy-MM-dd";
// 默认时间格式
public static final String DATETIME_DEFAULT_FORMAT = "yyyy-MM-dd HH:mm:ss";
public static final String DATETIMEHOUR_DEFAULT_FORMAT = "yyyy-MM-dd HH:mm";
public static final String TIME_DEFAULT_FORMAT = "HH:mm:ss";
private static DateFormat dateYearFormat = null;
private static DateFormat dateMonthFormat = null;
// 日期格式化
private static DateFormat dateFormat = null;
// 时间格式化
private static DateFormat dateTimeFormat = null;
private static DateFormat dateTimeHourFormat = null;
private static DateFormat timeFormat = null;
private static Calendar gregorianCalendar = null;
static {
dateYearFormat = new SimpleDateFormat(DATEYEAR_DEFAULT_FORMAT);
dateMonthFormat = new SimpleDateFormat(DATEMONTH_DEFAULT_FORMAT);
dateFormat = new SimpleDateFormat(DATE_DEFAULT_FORMAT);
dateTimeFormat = new SimpleDateFormat(DATETIME_DEFAULT_FORMAT);
dateTimeHourFormat = new SimpleDateFormat(DATETIMEHOUR_DEFAULT_FORMAT);
timeFormat = new SimpleDateFormat(TIME_DEFAULT_FORMAT);
gregorianCalendar = new GregorianCalendar();
}
/**
* 日期格式化yyyy-MM-dd
*
* @param date
* @return
*/
public static Date formatDate(String date, String format) {
try {
return new SimpleDateFormat(format).parse(date);
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
/**
* 日期格式化yyyy
*
* @param date
* @return
*/
public static String getDateYearFormat(Date date) {
return dateYearFormat.format(date);
}
/**
* 日期格式化yyyy-MM
*
* @param date
* @return
*/
public static String getDateMonthFormat(Date date) {
return dateMonthFormat.format(date);
}
/**
* 日期格式化yyyy-MM-dd
*
* @param date
* @return
*/
public static String getDateFormat(Date date) {
return dateFormat.format(date);
}
/**
* 日期格式化yyyy-MM-dd HH:mm:ss
*
* @param date
* @return
*/
public static String getDateTimeFormat(Date date) {
return dateTimeFormat.format(date);
}
/**
* 日期格式化yyyy-MM-dd HH:mm
*
* @param date
* @return
*/
public static String getDateTimeHourFormat(Date date) {
return dateTimeHourFormat.format(date);
}
/**
* 时间格式化
*
* @param date
* @return HH:mm:ss
*/
public static String getTimeFormat(Date date) {
return timeFormat.format(date);
}
/**
* 日期格式化
*
* @param date
* @param 格式类型
* @return
*/
public static String getDateFormat(Date date, String formatStr) {
if (StringUtils.isNotBlank(formatStr)) {
return new SimpleDateFormat(formatStr).format(date);
}
return null;
}
/**
* 日期格式化
*
* @param date
* @return yyyy-MM-dd
*/
public static Date getDateFormat(String date) {
try {
return dateFormat.parse(date);
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
/**
* 时间格式化
*
* @param date
* @return yyyy-MM-dd HH:mm:ss
*/
public static Date getDateTimeFormat(String date) {
try {
return dateTimeFormat.parse(date);
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
/**
* 获取当前日期(yyyy-MM-dd)
*
* @param date
* @return
*/
public static Date getNowDate() {
return DateUtil.getDateFormat(dateFormat.format(new Date()));
}
/**
* 获取当前日期星期一日期【本周一】
*
* @return date
*/
public static Date getFirstDayOfWeek() {
gregorianCalendar.setFirstDayOfWeek(Calendar.MONDAY);
gregorianCalendar.setTime(new Date());
gregorianCalendar.set(Calendar.DAY_OF_WEEK, gregorianCalendar.getFirstDayOfWeek()); // Monday
return gregorianCalendar.getTime();
}
/**
* 获取当前日期星期日日期【本周日】
*
* @return date
*/
public static Date getLastDayOfWeek() {
gregorianCalendar.setFirstDayOfWeek(Calendar.MONDAY);
gregorianCalendar.setTime(new Date());
gregorianCalendar.set(Calendar.DAY_OF_WEEK, gregorianCalendar.getFirstDayOfWeek() + 6); // Monday
return gregorianCalendar.getTime();
}
/**
* 获取指定日期的星期一日期
*
* @param 指定日期
* @return date
*/
public static Date getFirstDayOfWeek(Date date) {
if (date == null) {
return null;
}
gregorianCalendar.setFirstDayOfWeek(Calendar.MONDAY);
gregorianCalendar.setTime(date);
gregorianCalendar.set(Calendar.DAY_OF_WEEK, gregorianCalendar.getFirstDayOfWeek()); // Monday
return gregorianCalendar.getTime();
}
/**
* 获取日期星期一日期
*
* @param 指定日期
* @return date
*/
public static Date getLastDayOfWeek(Date date) {
if (date == null) {
return null;
}
gregorianCalendar.setFirstDayOfWeek(Calendar.MONDAY);
gregorianCalendar.setTime(date);
gregorianCalendar.set(Calendar.DAY_OF_WEEK, gregorianCalendar.getFirstDayOfWeek() + 6); // Monday
return gregorianCalendar.getTime();
}
/**
* 获取当前月的第一天
*
* @return date
*/
public static Date getFirstDayOfMonth() {
gregorianCalendar.setTime(new Date());
gregorianCalendar.set(Calendar.DAY_OF_MONTH, 1);
return gregorianCalendar.getTime();
}
/**
* 获取当前月的最后一天
*
* @return
*/
public static Date getLastDayOfMonth() {
gregorianCalendar.setTime(new Date());
gregorianCalendar.set(Calendar.DAY_OF_MONTH, 1);
gregorianCalendar.add(Calendar.MONTH, 1);
gregorianCalendar.add(Calendar.DAY_OF_MONTH, -1);
return gregorianCalendar.getTime();
}
/**
* 获取指定月的第一天
*
* @param date
* @return
*/
public static Date getFirstDayOfMonth(Date date) {
gregorianCalendar.setTime(date);
gregorianCalendar.set(Calendar.DAY_OF_MONTH, 1);
return gregorianCalendar.getTime();
}
/**
* 获取指定月的最后一天
*
* @param date
* @return
*/
public static Date getLastDayOfMonth(Date date) {
gregorianCalendar.setTime(date);
gregorianCalendar.set(Calendar.DAY_OF_MONTH, 1);
gregorianCalendar.add(Calendar.MONTH, 1);
gregorianCalendar.add(Calendar.DAY_OF_MONTH, -1);
return gregorianCalendar.getTime();
}
/**
* 获取指定日期的前一天
*
* @param date
* @return
*/
public static Date getDayBefore(Date date) {
gregorianCalendar.setTime(date);
int day = gregorianCalendar.get(Calendar.DATE);
gregorianCalendar.set(Calendar.DATE, day - 1);
return gregorianCalendar.getTime();
}
/**
* 获取指定日期的后一天
*
* @param date
* @return
*/
public static Date getDayAfter(Date date) {
gregorianCalendar.setTime(date);
int day = gregorianCalendar.get(Calendar.DATE);
gregorianCalendar.set(Calendar.DATE, day + 1);
return gregorianCalendar.getTime();
}
/**
* 获取当前年
*
* @return
*/
public static int getNowYear() {
Calendar d = Calendar.getInstance();
return d.get(Calendar.YEAR);
}
/**
* 获取当前月份
*
* @return
*/
public static int getNowMonth() {
Calendar d = Calendar.getInstance();
return d.get(Calendar.MONTH) + 1;
}
/**
* 获取当月天数
*
* @return
*/
public static int getNowMonthDay() {
Calendar d = Calendar.getInstance();
return d.getActualMaximum(Calendar.DATE);
}
/**
* 获取时间段的每一天
*
* @param 开始日期
* @param 结算日期
* @return 日期列表
*/
public static List<Date> getEveryDay(Date startDate, Date endDate) {
if (startDate == null || endDate == null) {
return null;
}
// 格式化日期(yy-MM-dd)
startDate = DateUtil.getDateFormat(DateUtil.getDateFormat(startDate));
endDate = DateUtil.getDateFormat(DateUtil.getDateFormat(endDate));
List<Date> dates = new ArrayList<Date>();
gregorianCalendar.setTime(startDate);
dates.add(gregorianCalendar.getTime());
while (gregorianCalendar.getTime().compareTo(endDate) < 0) {
// 加1天
gregorianCalendar.add(Calendar.DAY_OF_MONTH, 1);
dates.add(gregorianCalendar.getTime());
}
return dates;
}
/**
* 获取提前多少个月
*
* @param monty
* @return
*/
public static Date getFirstMonth(int monty) {
Calendar c = Calendar.getInstance();
c.add(Calendar.MONTH, -monty);
return c.getTime();
}
/**
* 取得当前日期是多少周
*
* @param date
* @return
*/
public static int getWeekOfYear(Date date) {
Calendar c = new GregorianCalendar();
c.setFirstDayOfWeek(Calendar.MONDAY);
c.setMinimalDaysInFirstWeek(7);
c.setTime (date);
return c.get(Calendar.WEEK_OF_YEAR);
}
/**
* 得到某一年周的总数
*
* @param year
* @return
*/
public static int getMaxWeekNumOfYear(int year) {
Calendar c = new GregorianCalendar();
c.set(year, Calendar.DECEMBER, 31, 23, 59, 59);
return getWeekOfYear(c.getTime());
}
/**
* 得到某年某周的第一天
*
* @param year
* @param week
* @return
*/
public static Date getFirstDayOfWeek(int year, int week) {
Calendar c = new GregorianCalendar();
c.set(Calendar.YEAR, year);
c.set (Calendar.MONTH, Calendar.JANUARY);
c.set(Calendar.DATE, 1);
Calendar cal = (GregorianCalendar) c.clone();
cal.add(Calendar.DATE, week * 7);
return getFirstDayOfWeek(cal.getTime ());
}
/**
* 得到某年某周的最后一天
*
* @param year
* @param week
* @return
*/
public static Date getLastDayOfWeek(int year, int week) {
Calendar c = new GregorianCalendar();
c.set(Calendar.YEAR, year);
c.set(Calendar.MONTH, Calendar.JANUARY);
c.set(Calendar.DATE, 1);
Calendar cal = (GregorianCalendar) c.clone();
cal.add(Calendar.DATE , week * 7);
return getLastDayOfWeek(cal.getTime());
}
/*public static void main(String[] args) {
System.out.println(DateUtil.getFirstDayOfWeek());
System.out.println(DateUtil.getLastDayOfWeek());
}*/
}
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
/**
* 日期工具类
* @author
* @since 2016-06-22
*/
public class DateUtil {
public static final String DATEYEAR_DEFAULT_FORMAT = "yyyy";
public static final String DATEMONTH_DEFAULT_FORMAT = "yyyy-MM";
// 默认日期格式
public static final String DATE_DEFAULT_FORMAT = "yyyy-MM-dd";
// 默认时间格式
public static final String DATETIME_DEFAULT_FORMAT = "yyyy-MM-dd HH:mm:ss";
public static final String DATETIMEHOUR_DEFAULT_FORMAT = "yyyy-MM-dd HH:mm";
public static final String TIME_DEFAULT_FORMAT = "HH:mm:ss";
private static DateFormat dateYearFormat = null;
private static DateFormat dateMonthFormat = null;
// 日期格式化
private static DateFormat dateFormat = null;
// 时间格式化
private static DateFormat dateTimeFormat = null;
private static DateFormat dateTimeHourFormat = null;
private static DateFormat timeFormat = null;
private static Calendar gregorianCalendar = null;
static {
dateYearFormat = new SimpleDateFormat(DATEYEAR_DEFAULT_FORMAT);
dateMonthFormat = new SimpleDateFormat(DATEMONTH_DEFAULT_FORMAT);
dateFormat = new SimpleDateFormat(DATE_DEFAULT_FORMAT);
dateTimeFormat = new SimpleDateFormat(DATETIME_DEFAULT_FORMAT);
dateTimeHourFormat = new SimpleDateFormat(DATETIMEHOUR_DEFAULT_FORMAT);
timeFormat = new SimpleDateFormat(TIME_DEFAULT_FORMAT);
gregorianCalendar = new GregorianCalendar();
}
/**
* 日期格式化yyyy-MM-dd
*
* @param date
* @return
*/
public static Date formatDate(String date, String format) {
try {
return new SimpleDateFormat(format).parse(date);
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
/**
* 日期格式化yyyy
*
* @param date
* @return
*/
public static String getDateYearFormat(Date date) {
return dateYearFormat.format(date);
}
/**
* 日期格式化yyyy-MM
*
* @param date
* @return
*/
public static String getDateMonthFormat(Date date) {
return dateMonthFormat.format(date);
}
/**
* 日期格式化yyyy-MM-dd
*
* @param date
* @return
*/
public static String getDateFormat(Date date) {
return dateFormat.format(date);
}
/**
* 日期格式化yyyy-MM-dd HH:mm:ss
*
* @param date
* @return
*/
public static String getDateTimeFormat(Date date) {
return dateTimeFormat.format(date);
}
/**
* 日期格式化yyyy-MM-dd HH:mm
*
* @param date
* @return
*/
public static String getDateTimeHourFormat(Date date) {
return dateTimeHourFormat.format(date);
}
/**
* 时间格式化
*
* @param date
* @return HH:mm:ss
*/
public static String getTimeFormat(Date date) {
return timeFormat.format(date);
}
/**
* 日期格式化
*
* @param date
* @param 格式类型
* @return
*/
public static String getDateFormat(Date date, String formatStr) {
if (StringUtils.isNotBlank(formatStr)) {
return new SimpleDateFormat(formatStr).format(date);
}
return null;
}
/**
* 日期格式化
*
* @param date
* @return yyyy-MM-dd
*/
public static Date getDateFormat(String date) {
try {
return dateFormat.parse(date);
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
/**
* 时间格式化
*
* @param date
* @return yyyy-MM-dd HH:mm:ss
*/
public static Date getDateTimeFormat(String date) {
try {
return dateTimeFormat.parse(date);
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
/**
* 获取当前日期(yyyy-MM-dd)
*
* @param date
* @return
*/
public static Date getNowDate() {
return DateUtil.getDateFormat(dateFormat.format(new Date()));
}
/**
* 获取当前日期星期一日期【本周一】
*
* @return date
*/
public static Date getFirstDayOfWeek() {
gregorianCalendar.setFirstDayOfWeek(Calendar.MONDAY);
gregorianCalendar.setTime(new Date());
gregorianCalendar.set(Calendar.DAY_OF_WEEK, gregorianCalendar.getFirstDayOfWeek()); // Monday
return gregorianCalendar.getTime();
}
/**
* 获取当前日期星期日日期【本周日】
*
* @return date
*/
public static Date getLastDayOfWeek() {
gregorianCalendar.setFirstDayOfWeek(Calendar.MONDAY);
gregorianCalendar.setTime(new Date());
gregorianCalendar.set(Calendar.DAY_OF_WEEK, gregorianCalendar.getFirstDayOfWeek() + 6); // Monday
return gregorianCalendar.getTime();
}
/**
* 获取指定日期的星期一日期
*
* @param 指定日期
* @return date
*/
public static Date getFirstDayOfWeek(Date date) {
if (date == null) {
return null;
}
gregorianCalendar.setFirstDayOfWeek(Calendar.MONDAY);
gregorianCalendar.setTime(date);
gregorianCalendar.set(Calendar.DAY_OF_WEEK, gregorianCalendar.getFirstDayOfWeek()); // Monday
return gregorianCalendar.getTime();
}
/**
* 获取日期星期一日期
*
* @param 指定日期
* @return date
*/
public static Date getLastDayOfWeek(Date date) {
if (date == null) {
return null;
}
gregorianCalendar.setFirstDayOfWeek(Calendar.MONDAY);
gregorianCalendar.setTime(date);
gregorianCalendar.set(Calendar.DAY_OF_WEEK, gregorianCalendar.getFirstDayOfWeek() + 6); // Monday
return gregorianCalendar.getTime();
}
/**
* 获取当前月的第一天
*
* @return date
*/
public static Date getFirstDayOfMonth() {
gregorianCalendar.setTime(new Date());
gregorianCalendar.set(Calendar.DAY_OF_MONTH, 1);
return gregorianCalendar.getTime();
}
/**
* 获取当前月的最后一天
*
* @return
*/
public static Date getLastDayOfMonth() {
gregorianCalendar.setTime(new Date());
gregorianCalendar.set(Calendar.DAY_OF_MONTH, 1);
gregorianCalendar.add(Calendar.MONTH, 1);
gregorianCalendar.add(Calendar.DAY_OF_MONTH, -1);
return gregorianCalendar.getTime();
}
/**
* 获取指定月的第一天
*
* @param date
* @return
*/
public static Date getFirstDayOfMonth(Date date) {
gregorianCalendar.setTime(date);
gregorianCalendar.set(Calendar.DAY_OF_MONTH, 1);
return gregorianCalendar.getTime();
}
/**
* 获取指定月的最后一天
*
* @param date
* @return
*/
public static Date getLastDayOfMonth(Date date) {
gregorianCalendar.setTime(date);
gregorianCalendar.set(Calendar.DAY_OF_MONTH, 1);
gregorianCalendar.add(Calendar.MONTH, 1);
gregorianCalendar.add(Calendar.DAY_OF_MONTH, -1);
return gregorianCalendar.getTime();
}
/**
* 获取指定日期的前一天
*
* @param date
* @return
*/
public static Date getDayBefore(Date date) {
gregorianCalendar.setTime(date);
int day = gregorianCalendar.get(Calendar.DATE);
gregorianCalendar.set(Calendar.DATE, day - 1);
return gregorianCalendar.getTime();
}
/**
* 获取指定日期的后一天
*
* @param date
* @return
*/
public static Date getDayAfter(Date date) {
gregorianCalendar.setTime(date);
int day = gregorianCalendar.get(Calendar.DATE);
gregorianCalendar.set(Calendar.DATE, day + 1);
return gregorianCalendar.getTime();
}
/**
* 获取当前年
*
* @return
*/
public static int getNowYear() {
Calendar d = Calendar.getInstance();
return d.get(Calendar.YEAR);
}
/**
* 获取当前月份
*
* @return
*/
public static int getNowMonth() {
Calendar d = Calendar.getInstance();
return d.get(Calendar.MONTH) + 1;
}
/**
* 获取当月天数
*
* @return
*/
public static int getNowMonthDay() {
Calendar d = Calendar.getInstance();
return d.getActualMaximum(Calendar.DATE);
}
/**
* 获取时间段的每一天
*
* @param 开始日期
* @param 结算日期
* @return 日期列表
*/
public static List<Date> getEveryDay(Date startDate, Date endDate) {
if (startDate == null || endDate == null) {
return null;
}
// 格式化日期(yy-MM-dd)
startDate = DateUtil.getDateFormat(DateUtil.getDateFormat(startDate));
endDate = DateUtil.getDateFormat(DateUtil.getDateFormat(endDate));
List<Date> dates = new ArrayList<Date>();
gregorianCalendar.setTime(startDate);
dates.add(gregorianCalendar.getTime());
while (gregorianCalendar.getTime().compareTo(endDate) < 0) {
// 加1天
gregorianCalendar.add(Calendar.DAY_OF_MONTH, 1);
dates.add(gregorianCalendar.getTime());
}
return dates;
}
/**
* 获取提前多少个月
*
* @param monty
* @return
*/
public static Date getFirstMonth(int monty) {
Calendar c = Calendar.getInstance();
c.add(Calendar.MONTH, -monty);
return c.getTime();
}
/**
* 取得当前日期是多少周
*
* @param date
* @return
*/
public static int getWeekOfYear(Date date) {
Calendar c = new GregorianCalendar();
c.setFirstDayOfWeek(Calendar.MONDAY);
c.setMinimalDaysInFirstWeek(7);
c.setTime (date);
return c.get(Calendar.WEEK_OF_YEAR);
}
/**
* 得到某一年周的总数
*
* @param year
* @return
*/
public static int getMaxWeekNumOfYear(int year) {
Calendar c = new GregorianCalendar();
c.set(year, Calendar.DECEMBER, 31, 23, 59, 59);
return getWeekOfYear(c.getTime());
}
/**
* 得到某年某周的第一天
*
* @param year
* @param week
* @return
*/
public static Date getFirstDayOfWeek(int year, int week) {
Calendar c = new GregorianCalendar();
c.set(Calendar.YEAR, year);
c.set (Calendar.MONTH, Calendar.JANUARY);
c.set(Calendar.DATE, 1);
Calendar cal = (GregorianCalendar) c.clone();
cal.add(Calendar.DATE, week * 7);
return getFirstDayOfWeek(cal.getTime ());
}
/**
* 得到某年某周的最后一天
*
* @param year
* @param week
* @return
*/
public static Date getLastDayOfWeek(int year, int week) {
Calendar c = new GregorianCalendar();
c.set(Calendar.YEAR, year);
c.set(Calendar.MONTH, Calendar.JANUARY);
c.set(Calendar.DATE, 1);
Calendar cal = (GregorianCalendar) c.clone();
cal.add(Calendar.DATE , week * 7);
return getLastDayOfWeek(cal.getTime());
}
/*public static void main(String[] args) {
System.out.println(DateUtil.getFirstDayOfWeek());
System.out.println(DateUtil.getLastDayOfWeek());
}*/
}