package com.jttech.cmci.pfcs.util;
|
|
import org.springframework.util.StringUtils;
|
|
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 DateUtil {
|
public static final SimpleDateFormat yyyyMMdd = new SimpleDateFormat("yyyy-MM-dd");
|
|
private static ThreadLocal<SimpleDateFormat> threadLocal = new ThreadLocal<SimpleDateFormat>() {
|
@Override
|
protected SimpleDateFormat initialValue() {
|
return new SimpleDateFormat("yyyy-MM-dd");
|
}
|
};
|
|
public static String[] pattern = new String[] { "yyyyMMdd", "yyyy-MM-dd", "yyyy.MM.dd", "yyyy/MM/dd",
|
"yyyy-MM", "yyyyMM", "yyyy/MM", "yyyyMMddHHmmss", "yyyy-MM-dd HH:mm:ss", "yyyy/MM/dd HH:mm:ss",
|
"yyyyMMddHHmmssSSS", "HH:mm", "HHmmss", "MM-dd HH:mm", "YYYY-MM-dd HH:mm", "MM-dd HH:mm:ss" };
|
|
|
|
public static Date previous(int days) {
|
return new Date(System.currentTimeMillis() - days * 3600000L * 24L);
|
}
|
|
|
|
public static Date addDay(Date date, int days) {
|
return new Date(date.getTime() + days * 3600000L * 24L);
|
}
|
|
|
|
public static Date addMin(Date date, int mins) {
|
Calendar c = Calendar.getInstance();
|
c.setTime(date);
|
c.add(Calendar.MINUTE, mins);
|
// System.out.println(c.getTime());
|
return c.getTime();
|
}
|
|
|
|
public static Date addHour(Date date, int hours) {
|
Calendar c = Calendar.getInstance();
|
c.setTime(date);
|
c.add(Calendar.HOUR_OF_DAY, hours);
|
// System.out.println(c.getTime());
|
return c.getTime();
|
}
|
|
|
|
public static String formatDateTime(String format, long d) {
|
return new SimpleDateFormat(format).format(d);
|
}
|
|
|
|
public static String formatDate(String format, Date d) {
|
|
return StringUtils.isEmpty(format) || null == d ? "" : new SimpleDateFormat(format).format(d);
|
}
|
|
|
|
public static String formatDate(Date d) {
|
|
return formatDate(pattern[7], d);
|
}
|
|
|
|
public static Date parseDate(String format, String d) {
|
try {
|
return new SimpleDateFormat(format).parse(d);
|
} catch (Exception e) {
|
throw new RuntimeException("需要时间格式为:" + format);
|
}
|
}
|
|
|
|
public static Date getNextWeekDay(int weekDay) {
|
Calendar calendar = Calendar.getInstance();
|
calendar.set(Calendar.HOUR_OF_DAY, 0);
|
calendar.set(Calendar.MINUTE, 0);
|
calendar.set(Calendar.SECOND, 0);
|
calendar.set(Calendar.MILLISECOND, 0);
|
|
int addDateNumber = 0;
|
if (Calendar.SUNDAY == calendar.get(Calendar.DAY_OF_WEEK)) {
|
if (Calendar.SUNDAY == weekDay) {
|
addDateNumber = 7;
|
}
|
} else {
|
addDateNumber = 7;
|
if (Calendar.SUNDAY == weekDay) {
|
addDateNumber = 14;
|
}
|
}
|
calendar.add(Calendar.DATE, addDateNumber);
|
calendar.set(Calendar.DAY_OF_WEEK, weekDay);
|
return calendar.getTime();
|
}
|
|
|
|
public static Date getThisWeekMonday() {
|
Calendar calendar = Calendar.getInstance();
|
calendar.set(Calendar.HOUR_OF_DAY, 0);
|
calendar.set(Calendar.MINUTE, 0);
|
calendar.set(Calendar.SECOND, 0);
|
calendar.set(Calendar.MILLISECOND, 0);
|
|
int min = calendar.getActualMinimum(Calendar.DAY_OF_WEEK); // 获取周开始基准
|
int current = calendar.get(Calendar.DAY_OF_WEEK); // 获取当天周内天数
|
calendar.add(Calendar.DAY_OF_WEEK, min - current + 1); // 当天-基准,获取周开始日期
|
return calendar.getTime();
|
}
|
|
|
|
/**
|
* 获取所在日期周的星期一(周一到周日为一周)
|
*
|
* @return 日期所在周的星期一
|
*/
|
public static Date getWeekMonday(Date date) {
|
Calendar calendar = Calendar.getInstance();
|
calendar.setTime(date);
|
calendar.set(Calendar.HOUR_OF_DAY, 0);
|
calendar.set(Calendar.MINUTE, 0);
|
calendar.set(Calendar.SECOND, 0);
|
calendar.set(Calendar.MILLISECOND, 0);
|
|
int min = calendar.getActualMinimum(Calendar.DAY_OF_WEEK); // 获取周开始基准
|
int current = calendar.get(Calendar.DAY_OF_WEEK); // 获取当天周内天数
|
calendar.add(Calendar.DAY_OF_WEEK, min - current + 1 + (current == Calendar.SUNDAY ? -7 : 0)); // 当天-基准,获取周开始日期
|
return calendar.getTime();
|
}
|
|
|
|
public static Date parse(String str) {
|
try {
|
return threadLocal.get().parse(str);
|
} catch (ParseException e) {
|
e.printStackTrace();
|
return null;
|
}
|
}
|
|
|
|
/**
|
* 获取字符串当月第一天,需要date(yyyy-MM-dd)
|
*
|
* @param date
|
* @return
|
*/
|
public static String getFirstDayOfMonth(String date) {
|
return getFirstDayOfMonth(parse(date));
|
}
|
|
|
|
/**
|
* 获取字符串当月最后一天
|
*
|
* @param date
|
* @return
|
*/
|
public static String getFirstDayOfMonth(String format, String date) {
|
return getFirstDayOfMonth(parseDate(format, date));
|
}
|
|
|
|
/**
|
* 获取字符串当月第一天,需要date(yyyy-MM-dd)
|
*
|
* @param date
|
* @return
|
*/
|
public static String getEndDayOfMonth(String date) {
|
return getEndDayOfMonth(parse(date));
|
}
|
|
|
|
/**
|
* 获取字符串当月最后一天
|
*
|
* @param date
|
* @return
|
*/
|
public static String getEndDayOfMonth(String format, String date) {
|
return getEndDayOfMonth(parseDate(format, date));
|
}
|
|
|
|
/**
|
* 获取字符串当月最后一天
|
*
|
* @param date
|
* @return
|
*/
|
public static Date getFirstDayDateOfMonth(String format, String date) {
|
return getFirstDayDateOfMonth(parseDate(format, date));
|
}
|
|
|
|
/**
|
* 获取字符串当月最后一天
|
*
|
* @param date
|
* @return
|
*/
|
public static Date getEndDayDateOfMonth(String format, String date) {
|
return getEndDayDateOfMonth(parseDate(format, date));
|
}
|
|
|
|
public static String getFirstDayOfMonth(Date date) {
|
Calendar cal = Calendar.getInstance();
|
cal.setTime(date);
|
cal.set(Calendar.DATE, 1);
|
return threadLocal.get().format(cal.getTime());
|
}
|
|
|
|
public static String getEndDayOfMonth(Date date) {
|
Calendar cal = Calendar.getInstance();
|
cal.setTime(date);
|
cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
|
return threadLocal.get().format(cal.getTime());
|
}
|
|
|
|
public static Date getFirstDayDateOfMonth(Date date) {
|
Calendar cal = Calendar.getInstance();
|
cal.setTime(date);
|
cal.set(Calendar.DATE, 1);
|
return cal.getTime();
|
}
|
|
|
|
public static Date getEndDayDateOfMonth(Date date) {
|
Calendar cal = Calendar.getInstance();
|
cal.setTime(date);
|
cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
|
return cal.getTime();
|
}
|
|
|
|
public static Date addMonth(Date date, int count) {
|
Calendar c = Calendar.getInstance();
|
c.setTime(date);
|
c.add(2, count);
|
return c.getTime();
|
}
|
|
|
|
public static Date addYear(Date date, int count) {
|
Calendar c = Calendar.getInstance();
|
c.setTime(date);
|
c.add(Calendar.YEAR, count);
|
return c.getTime();
|
}
|
|
|
|
public static String getCurrentDate() {
|
return threadLocal.get().format(new Date());
|
}
|
|
|
|
public static Date getTodayDate() {
|
try {
|
return threadLocal.get().parse(getCurrentDate());
|
} catch (ParseException e) {
|
throw new RuntimeException();
|
}
|
}
|
|
|
|
public static int getDayOfWeek() {
|
Calendar cal = Calendar.getInstance();
|
return cal.get(7);
|
}
|
|
|
|
public static int getDayOfWeek(Date date) {
|
Calendar cal = Calendar.getInstance();
|
cal.setTime(date);
|
return cal.get(7);
|
}
|
|
|
|
public static int getDayOfWeek(String date) {
|
Calendar cal = Calendar.getInstance();
|
cal.setTime(parse(date));
|
return cal.get(7);
|
}
|
|
|
|
public static int getMaxDayOfMonth(Date date) {
|
Calendar cal = Calendar.getInstance();
|
cal.setTime(date);
|
return cal.getActualMaximum(5);
|
}
|
|
|
|
public static String toString(Date date) {
|
if (date == null)
|
return "";
|
else
|
return threadLocal.get().format(date);
|
}
|
|
|
|
public static String toString(Date date, String format) {
|
SimpleDateFormat t = new SimpleDateFormat(format);
|
return t.format(date);
|
}
|
|
|
|
// 获取当前年
|
public static String getYear() {
|
SimpleDateFormat formatter = new SimpleDateFormat("yyyy");
|
return formatter.format(new Date());
|
}
|
|
|
|
// 获取年
|
public static String getYear(Date date) {
|
SimpleDateFormat formatter = new SimpleDateFormat("yyyy");
|
return formatter.format(date);
|
}
|
|
|
|
// 获取当年月
|
public static String getMonth() {
|
SimpleDateFormat formatter = new SimpleDateFormat("MM");
|
return formatter.format(new Date());
|
}
|
|
|
|
// 获取当年月
|
public static String getYearMonth() {
|
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMM");
|
return formatter.format(new Date());
|
}
|
|
|
|
// 获取月
|
public static String getMonth(Date date) {
|
SimpleDateFormat formatter = new SimpleDateFormat("MM");
|
return formatter.format(date);
|
}
|
|
|
|
// 获取当日
|
public static String getDay() {
|
SimpleDateFormat formatter = new SimpleDateFormat("dd");
|
return formatter.format(new Date());
|
}
|
|
|
|
// 获取日
|
public static String getDay(Date date) {
|
SimpleDateFormat formatter = new SimpleDateFormat("dd");
|
return formatter.format(date);
|
}
|
|
|
|
/**
|
* 月
|
*
|
* @return
|
*/
|
public static List<String> getMonthList() {
|
List<String> list = new ArrayList<String>();
|
list.add("01");
|
list.add("02");
|
list.add("03");
|
list.add("04");
|
list.add("05");
|
list.add("06");
|
list.add("07");
|
list.add("08");
|
list.add("09");
|
list.add("10");
|
list.add("11");
|
list.add("12");
|
return list;
|
}
|
|
|
|
// 计算相差时间
|
public static double dateDiff(String startTime, String endTime, String format) {
|
SimpleDateFormat sd = new SimpleDateFormat(format);
|
long nd = 1000 * 24 * 60 * 60;// 一天的毫秒数
|
long nh = 1000 * 60 * 60;// 一小时的毫秒数
|
long diff;
|
double hour = 0;
|
try {
|
// 获得两个时间的毫秒时间差异
|
diff = sd.parse(endTime).getTime() - sd.parse(startTime).getTime();
|
hour = (diff % nd) * 1.0f / nh;// 计算差多少小时
|
|
} catch (ParseException e) {
|
e.printStackTrace();
|
}
|
// DecimalFormat df = new DecimalFormat("#.00");
|
System.out.println(new Double(String.format("%.2f", hour)));
|
return new Double(String.format("%.2f", hour));
|
}
|
|
|
|
// 计算相差天数时间
|
public static int dateDayDiff(String startTime, String endTime, String format) {
|
SimpleDateFormat sd = new SimpleDateFormat(format);
|
long nd = 1000 * 24 * 60 * 60;// 一天的毫秒数
|
long diff;
|
int day = 0;
|
try {
|
// 获得两个时间的毫秒时间差异
|
diff = sd.parse(endTime).getTime() - sd.parse(startTime).getTime();
|
day = (int) (diff / nd);// 计算差多少天
|
|
} catch (ParseException e) {
|
e.printStackTrace();
|
}
|
return day;
|
}
|
|
|
|
public static List<WeekDay> getDisplayWeekDays(Date startTime, Date endTime) {
|
List<WeekDay> weekDays = new ArrayList<WeekDay>();
|
Date date = startTime;
|
long endTimeLong = endTime.getTime();
|
while (date.getTime() <= endTimeLong) {
|
weekDays.add(new WeekDay(date));
|
Calendar calendar = Calendar.getInstance();
|
calendar.setTime(date);
|
calendar.add(Calendar.DATE, 1);
|
date = calendar.getTime();
|
}
|
return weekDays;
|
}
|
|
public static class WeekDay {
|
|
private Date date;
|
|
private static final String[] WEEK_DAY_NAME = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
|
|
|
|
public WeekDay() {
|
}
|
|
|
|
public WeekDay(Date date) {
|
this.setDate(date);
|
}
|
|
|
|
public Date getDate() {
|
return date;
|
}
|
|
|
|
public void setDate(Date date) {
|
this.date = date;
|
}
|
|
|
|
@SuppressWarnings("deprecation")
|
@Override
|
public String toString() {
|
return threadLocal.get().format(date) + "(" + WEEK_DAY_NAME[date.getDay()] + ")";
|
}
|
|
|
|
@SuppressWarnings("deprecation")
|
public String getDay() {
|
return WEEK_DAY_NAME[date.getDay()];
|
}
|
}
|
|
|
|
// 指定日期所在月的第一天日期
|
public static Date getBeginDayOfMonth(Date date) {
|
Calendar calendar = Calendar.getInstance();
|
calendar.setTime(date);
|
calendar.set(Calendar.DATE, 1);
|
calendar.set(Calendar.HOUR_OF_DAY, 0);
|
calendar.set(Calendar.MINUTE, 0);
|
calendar.set(Calendar.SECOND, 0);
|
calendar.set(Calendar.MILLISECOND, 0);
|
return calendar.getTime();
|
}
|
|
|
|
// 获取指定日期的下个月的日期
|
public static Date getNextBeginDayOfMonth(Date date) {
|
Calendar calendar = Calendar.getInstance();
|
calendar.setTime(date);
|
calendar.set(Calendar.DATE, 1);
|
calendar.set(Calendar.HOUR_OF_DAY, 0);
|
calendar.set(Calendar.MINUTE, 0);
|
calendar.set(Calendar.SECOND, 0);
|
calendar.set(Calendar.MILLISECOND, 0);
|
calendar.add(Calendar.MONTH, +1);
|
return calendar.getTime();
|
}
|
|
|
|
// 指定日期所在年的第一天日期
|
public static Date getBeginDayOfYear(Date date) {
|
Calendar calendar = Calendar.getInstance();
|
calendar.setTime(date);
|
calendar.set(Calendar.MONTH, 0);
|
calendar.set(Calendar.DATE, 1);
|
calendar.set(Calendar.HOUR_OF_DAY, 0);
|
calendar.set(Calendar.MINUTE, 0);
|
calendar.set(Calendar.SECOND, 0);
|
calendar.set(Calendar.MILLISECOND, 0);
|
return calendar.getTime();
|
}
|
|
|
|
public static Date getEndDayOfYear(Date date) {
|
Calendar calendar = Calendar.getInstance();
|
calendar.setTime(date);
|
calendar.set(Calendar.MONTH, calendar.getActualMaximum(Calendar.MONTH));
|
calendar.set(Calendar.DATE, calendar.getActualMaximum(Calendar.DATE));
|
calendar.set(Calendar.HOUR_OF_DAY, calendar.getActualMaximum(Calendar.HOUR_OF_DAY));
|
calendar.set(Calendar.MINUTE, calendar.getActualMaximum(Calendar.MINUTE));
|
calendar.set(Calendar.SECOND, calendar.getActualMaximum(Calendar.SECOND));
|
calendar.set(Calendar.MILLISECOND, calendar.getActualMaximum(Calendar.MILLISECOND));
|
return calendar.getTime();
|
}
|
|
|
|
// 获取指定日期的下年的日期
|
public static Date getNextBeginDayOfYear(Date date) {
|
Calendar calendar = Calendar.getInstance();
|
calendar.setTime(date);
|
calendar.set(Calendar.MONTH, 1);
|
calendar.set(Calendar.DATE, 1);
|
calendar.set(Calendar.HOUR_OF_DAY, 0);
|
calendar.set(Calendar.MINUTE, 0);
|
calendar.set(Calendar.SECOND, 0);
|
calendar.set(Calendar.MILLISECOND, 0);
|
calendar.add(Calendar.YEAR, +1);
|
return calendar.getTime();
|
}
|
|
|
|
/**
|
* @param startDate
|
* @param endDate
|
* @return
|
* @功能描述:
|
* <p>
|
* 获取两个时间相差秒
|
* </p>
|
* @创建作者: lance
|
* @创建日期: 2016年12月12日 下午12:02:30
|
*/
|
public static Integer getTimeSecond(Date startDate, Date endDate) {
|
long start = startDate.getTime();
|
long end = endDate.getTime();
|
int second = (int) ((end - start) / 1000);
|
return second;
|
|
}
|
|
|
|
/**
|
* @param beginDate
|
* @param endDate
|
* @return
|
* @功能描述:
|
* <p>
|
* 获取两个日期之间的天数
|
* </p>
|
* @创建作者: lance
|
* @创建日期: 2017年1月6日 上午10:52:47
|
*/
|
public static Integer getDays(String beginDate, String endDate) {
|
SimpleDateFormat sd = new SimpleDateFormat(DateUtil.pattern[1]);
|
try {
|
return (int) ((sd.parse(endDate).getTime() - sd.parse(beginDate).getTime()) / 86400000);
|
} catch (ParseException e) {
|
e.printStackTrace();
|
}
|
return null;
|
}
|
|
|
|
/**
|
* @param beginDate
|
* @param endDate
|
* @return
|
* @功能描述:
|
* <p>
|
* 获取两个日期之间的天数
|
* </p>
|
* @创建作者: lance
|
* @创建日期: 2017年1月6日 上午10:52:47
|
*/
|
public static Integer getDays(Date beginDate, Date endDate) {
|
return (int) ((getDayBeginTime(endDate).getTime() - getDayBeginTime(beginDate).getTime()) / 86400000);
|
}
|
|
|
|
/**
|
* @param date
|
* @return
|
* @功能描述:
|
* <p>
|
* 获取两个日期之间的天数
|
* </p>
|
* @创建作者: lance
|
* @创建日期: 2017年1月6日 上午10:52:47
|
*/
|
public static Date getDayBeginTime(Date date) {
|
try {
|
String dateStr = threadLocal.get().format(date);
|
return threadLocal.get().parse(dateStr);
|
} catch (ParseException e) {
|
e.printStackTrace();
|
}
|
return null;
|
}
|
|
|
|
public static Integer getDayOfHour(Date date) {
|
String hour = new SimpleDateFormat("HH").format(date);
|
return Integer.valueOf(hour);
|
}
|
|
|
|
public static Integer getDayOfMinute(Date date) {
|
String hour = new SimpleDateFormat("mm").format(date);
|
return Integer.valueOf(hour);
|
}
|
|
|
|
public static Date getDayEndTime(Date date) {
|
try {
|
return new Date(getDayBeginTime(date).getTime() + 0x5265BFFL);
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
return null;
|
}
|
|
|
|
/**
|
* 获取当前时间距离当天结束的分钟数
|
*
|
* @param date
|
* @return
|
*/
|
public static Integer getDayEndMinute(Date date) {
|
Date dayEndTime = getDayEndTime(date);
|
Integer timeSecond = getTimeSecond(date, dayEndTime);
|
return timeSecond / 60;
|
}
|
|
|
|
/**
|
* 计算两点时间间隔年
|
*
|
* @param startTime
|
* @param endTime
|
* @param format
|
* @return
|
*/
|
public static int betweenTimesYear(String startTime, String endTime, String format) {
|
SimpleDateFormat sd = new SimpleDateFormat(format);
|
|
int year = 0;
|
try {
|
long end = sd.parse(endTime).getTime();
|
long start = sd.parse(startTime).getTime();
|
long oneYear = 1000l * 60 * 60 * 24 * 365;
|
year = (int) ((end - start) / oneYear);
|
} catch (ParseException e) {
|
// TODO Auto-generated catch block
|
e.printStackTrace();
|
}
|
return year;
|
}
|
|
|
|
public static int getAgeByBirth(Date birthday) {
|
int age = 0;
|
try {
|
Calendar now = Calendar.getInstance();
|
now.setTime(new Date());// 当前时间
|
|
Calendar birth = Calendar.getInstance();
|
birth.setTime(birthday);
|
|
if (birth.after(now)) {// 如果传入的时间,在当前时间的后面,返回0岁
|
age = 0;
|
} else {
|
age = now.get(Calendar.YEAR) - birth.get(Calendar.YEAR);
|
if (now.get(Calendar.DAY_OF_YEAR) < birth.get(Calendar.DAY_OF_YEAR)) {
|
age -= 1;
|
}
|
}
|
return age;
|
} catch (Exception e) {
|
throw new RuntimeException("身份证号异常");
|
}
|
}
|
|
|
|
/**
|
* 获取精确到秒的时间戳
|
*
|
* @param pDate
|
* @return
|
*/
|
public static int getSecondTimestamp(Date pDate) {
|
if (null == pDate) {
|
return 0;
|
}
|
String timestamp = String.valueOf(pDate.getTime() / 1000);
|
return Integer.valueOf(timestamp);
|
}
|
|
|
|
/**
|
* 秒时间戳转成时间
|
*
|
* @param pSecondTimestamp
|
* @return
|
*/
|
public static Date parseSecondTimestamp(int pSecondTimestamp) {
|
Date date = null;
|
if (pSecondTimestamp != 0) {
|
date = new Date(pSecondTimestamp * 1000L);
|
}
|
return date;
|
}
|
|
|
|
public static Date getLastQuarterStartTime() {
|
Calendar startCalendar = Calendar.getInstance();
|
startCalendar.set(Calendar.MONTH, (startCalendar.get(Calendar.MONTH) / 3 - 1) * 3);
|
startCalendar.set(Calendar.DAY_OF_MONTH, 1);
|
setMinTime(startCalendar);
|
return startCalendar.getTime();
|
}
|
|
|
|
public static Date getLastQuarterEndTime() {
|
Calendar endCalendar = Calendar.getInstance();
|
endCalendar.set(Calendar.MONTH, (endCalendar.get(Calendar.MONTH) / 3 - 1) * 3 + 2);
|
endCalendar.set(Calendar.DAY_OF_MONTH, endCalendar.getActualMaximum(Calendar.DAY_OF_MONTH));
|
setMaxTime(endCalendar);
|
|
return endCalendar.getTime();
|
}
|
|
|
|
public static Date getQuarterStartTime(Integer year, Integer quarter) {
|
Calendar quarterCalendar = Calendar.getInstance();
|
quarterCalendar.set(Calendar.YEAR, year);
|
switch (quarter) {
|
case 1:
|
quarterCalendar.set(Calendar.MONTH, 0);
|
break;
|
case 2:
|
quarterCalendar.set(Calendar.MONTH, 3);
|
break;
|
case 3:
|
quarterCalendar.set(Calendar.MONTH, 6);
|
break;
|
case 4:
|
quarterCalendar.set(Calendar.MONTH, 9);
|
break;
|
default:
|
}
|
quarterCalendar.set(Calendar.DAY_OF_MONTH, 1);
|
setMinTime(quarterCalendar);
|
return quarterCalendar.getTime();
|
}
|
|
|
|
public static Date getQuarterEndTime(Integer year, Integer quarter) {
|
Calendar quarterCalendar = Calendar.getInstance();
|
quarterCalendar.set(Calendar.YEAR, year);
|
switch (quarter) {
|
case 1:
|
quarterCalendar.set(Calendar.MONTH, 2);
|
break;
|
case 2:
|
quarterCalendar.set(Calendar.MONTH, 5);
|
break;
|
case 3:
|
quarterCalendar.set(Calendar.MONTH, 8);
|
break;
|
case 4:
|
quarterCalendar.set(Calendar.MONTH, 11);
|
break;
|
default:
|
}
|
quarterCalendar.set(Calendar.DAY_OF_MONTH, quarterCalendar.getActualMaximum(Calendar.DAY_OF_MONTH));
|
setMaxTime(quarterCalendar);
|
return quarterCalendar.getTime();
|
}
|
|
|
|
public static Date getMonthStartTime(Integer year, Integer month) {
|
Calendar monthCalendar = Calendar.getInstance();
|
monthCalendar.set(Calendar.YEAR, year);
|
monthCalendar.set(Calendar.MONTH, month - 1);
|
monthCalendar.set(Calendar.DAY_OF_MONTH, 1);
|
setMinTime(monthCalendar);
|
return monthCalendar.getTime();
|
}
|
|
|
|
public static Date getMonthEndTime(Integer year, Integer month) {
|
Calendar monthCalendar = Calendar.getInstance();
|
monthCalendar.set(Calendar.YEAR, year);
|
monthCalendar.set(Calendar.MONTH, month - 1);
|
monthCalendar.set(Calendar.DAY_OF_MONTH, monthCalendar.getActualMaximum(Calendar.DAY_OF_MONTH));
|
setMaxTime(monthCalendar);
|
return monthCalendar.getTime();
|
}
|
|
|
|
public static Date getYearStartTime(Integer year) {
|
Calendar quarterCalendar = Calendar.getInstance();
|
quarterCalendar.set(Calendar.YEAR, year);
|
quarterCalendar.set(Calendar.MONTH, 0);
|
quarterCalendar.set(Calendar.DAY_OF_MONTH, 1);
|
setMinTime(quarterCalendar);
|
return quarterCalendar.getTime();
|
}
|
|
|
|
public static Date getYearEndTime(Integer year) {
|
Calendar quarterCalendar = Calendar.getInstance();
|
quarterCalendar.set(Calendar.YEAR, year);
|
quarterCalendar.set(Calendar.MONTH, 11);
|
quarterCalendar.set(Calendar.DAY_OF_MONTH, quarterCalendar.getActualMaximum(Calendar.DAY_OF_MONTH));
|
setMaxTime(quarterCalendar);
|
return quarterCalendar.getTime();
|
}
|
|
|
|
public static Date getDayEndTimeWithoutMillisecond(Date date) {
|
Calendar calendar = Calendar.getInstance();
|
calendar.setTime(date);
|
calendar.set(Calendar.HOUR_OF_DAY, 23);
|
calendar.set(Calendar.MINUTE, 59);
|
calendar.set(Calendar.SECOND, 59);
|
calendar.set(Calendar.MILLISECOND, 0);
|
return calendar.getTime();
|
}
|
|
|
|
private static void setMinTime(Calendar calendar) {
|
calendar.set(Calendar.HOUR_OF_DAY, 0);
|
calendar.set(Calendar.MINUTE, 0);
|
calendar.set(Calendar.SECOND, 0);
|
calendar.set(Calendar.MILLISECOND, 0);
|
}
|
|
|
|
private static void setMaxTime(Calendar calendar) {
|
calendar.set(Calendar.HOUR_OF_DAY, calendar.getActualMaximum(Calendar.HOUR_OF_DAY));
|
calendar.set(Calendar.MINUTE, calendar.getActualMaximum(Calendar.MINUTE));
|
calendar.set(Calendar.SECOND, calendar.getActualMaximum(Calendar.SECOND));
|
calendar.set(Calendar.MILLISECOND, calendar.getActualMaximum(Calendar.MILLISECOND));
|
}
|
|
}
|