123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688 |
- package com.inet.ailink.receiver.common.utils;
-
- import org.apache.commons.lang.StringUtils;
- import org.apache.commons.lang.time.FastDateFormat;
-
- import java.math.BigDecimal;
- import java.text.DateFormat;
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- import java.util.Calendar;
- import java.util.Date;
- import java.util.GregorianCalendar;
-
- /**
- * @ClassName: DateUtils
- * @Description: 日期处理工具类
- * @author david
- * @date 2013-8-20 下午3:11:57
- *
- */
- public class DateUtils {
-
- /**
- * ISO8601 formatter for date without time zone. The format used is
- * <tt>yyyy-MM-dd</tt>.
- */
- public static final FastDateFormat DATE_FORMAT = FastDateFormat.getInstance("yyyy-MM-dd");
-
- /**
- * ISO8601 date format: yyyy-MM-dd
- */
- public static final String DATE_FORMAT_PATTERN = "yyyy-MM-dd";
-
- public static final String DATE_FORMAT_PATTERN_NO_SEPARATOR = "yyyyMMdd";
-
- /**
- * ISO8601 date-time format: yyyy-MM-dd HH:mm:ss
- */
- public static final String DATETIME_FORMAT_PATTERN = "yyyy-MM-dd HH:mm:ss";
-
- /**
- * for bet view format: yyyy-MM-dd HH:mm:ss
- */
- public static final String DATETIME_JSVIEW_FORMAT_PATTERN = "yyyy/MM/dd HH:mm:ss";
- public static final String DATETIME_SENDTIME_FORMAT_PATTERN = "yyyyMMddHHmmssSSS";
- /**
- * 日期时间格式:yyyy-MM-dd HH:mm,不显示秒
- */
- public static final String DATETIME_WITHOUT_SECOND_FORMAT_PATTERN = "yyyy-MM-dd HH:mm";
-
- /**
- * 日期时间格式:yyyy-MM-dd HH
- */
- public static final String DATETIME_WITHOUT_MINUTES_FORMAT_PATTERN = "yyyy-MM-dd HH";
-
- public static final String DATE_TIME_START00 = " 00:00:00";
-
- public static final String DATE_TIME_END23 = " 23:59:59";
-
- /**
- * 获得当前时间
- */
- public static Date currentDate() {
- return new Date();
- }
-
- public static Date getDate(Long time) {
- return new Date(time);
- }
-
- public static Long getCurTime() {
- return System.currentTimeMillis();
- }
-
- public static Date parse(String str) {
- return parse(str, DATE_FORMAT_PATTERN);
- }
-
- public static Date parse(String str, String pattern) {
- if (StringUtils.isBlank(str)) {
- return null;
- }
- DateFormat parser = new SimpleDateFormat(pattern);
- try {
- return parser.parse(str);
- } catch (ParseException e) {
- throw new IllegalArgumentException("Can't parse " + str + " using " + pattern);
- }
- }
-
- /**
- * 根据时间变量返回时间字符串
- */
- public static String format(Date date, String pattern) {
- if (date == null) {
- return null;
- }
- FastDateFormat df = FastDateFormat.getInstance(pattern);
- return df.format(date);
- }
-
- public static String format(Long time, String pattern) {
- if (time == null) {
- return null;
- }
- FastDateFormat df = FastDateFormat.getInstance(pattern);
- return df.format(new Date(time));
- }
-
- /**
- * return date format is <code>yyyy-MM-dd</code>
- */
- public static String format(Date date) {
- return date == null ? null : DATE_FORMAT.format(date);
- }
-
- public static Date getEndDateTimeOfCurrentYear() {
- Calendar cal = Calendar.getInstance();
- cal.setTime(currentDate());
- cal.set(Calendar.MONTH, Calendar.DECEMBER);
- cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
- cal.set(Calendar.HOUR_OF_DAY, 23);
- cal.set(Calendar.MINUTE, 59);
- cal.set(Calendar.SECOND, 59);
- return cal.getTime();
- }
-
- public static Date getStartDateTimeOfCurrentYear() {
- Calendar cal = Calendar.getInstance();
- cal.setTime(currentDate());
- cal.set(Calendar.MONTH, Calendar.JANUARY);
- cal.set(Calendar.DAY_OF_MONTH, 1);
-
- cal.set(Calendar.HOUR_OF_DAY, 0);
- cal.set(Calendar.MINUTE, 0);
- cal.set(Calendar.SECOND, 0);
- return parse(format(cal.getTime()));
-
- }
-
- public static Date getStartDateTimeOfYear(int year) {
- Calendar cal = Calendar.getInstance();
- cal.set(Calendar.YEAR, year);
- cal.set(Calendar.MONTH, Calendar.JANUARY);
- cal.set(Calendar.DAY_OF_MONTH, 1);
- cal.set(Calendar.HOUR_OF_DAY, 0);
- cal.set(Calendar.MINUTE, 0);
- cal.set(Calendar.SECOND, 0);
- return parse(format(cal.getTime()));
- }
-
- public static Date getEndDateTimeOfYear(int year) {
- Calendar cal = Calendar.getInstance();
- cal.set(Calendar.YEAR, year);
- cal.set(Calendar.MONTH, Calendar.DECEMBER);
- cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
- cal.set(Calendar.HOUR_OF_DAY, 23);
- cal.set(Calendar.MINUTE, 59);
- cal.set(Calendar.SECOND, 59);
- return cal.getTime();
- }
-
- public static Date getStartTimeOfCurrentDate() {
- return getStartTimeOfDate(currentDate());
- }
-
- public static Date getEndTimeOfCurrentDate() {
- return getEndTimeOfDate(currentDate());
- }
-
- public static Date getStartTimeOfCurrentMonth() {
- return getStartDateTimeOfMonth(DateUtils.currentDate());
- }
-
- public static Date getEndTimeOfCurrentMonth() {
- return getEndDateTimeOfMonth(DateUtils.currentDate());
- }
-
- public static Date getStartTimeOfDate(Date date) {
- Calendar cal = Calendar.getInstance();
- cal.setTime(date);
- cal.set(Calendar.HOUR_OF_DAY, 0);
- cal.set(Calendar.MINUTE, 0);
- cal.set(Calendar.SECOND, 0);
- return parse(format(cal.getTime()));
- }
-
- public static Date getEndTimeOfDate(Date date) {
- Calendar cal = Calendar.getInstance();
- cal.setTime(date);
- cal.set(Calendar.HOUR_OF_DAY, 23);
- cal.set(Calendar.MINUTE, 59);
- cal.set(Calendar.SECOND, 59);
- return cal.getTime();
- }
-
- public static Date getSpecialEndTimeOfDate(Date date) {
- Calendar cal = Calendar.getInstance();
- cal.setTime(date);
- cal.set(Calendar.HOUR_OF_DAY, 24);
- cal.set(Calendar.MINUTE, 00);
- cal.set(Calendar.SECOND, 00);
- return cal.getTime();
- }
-
- public static Date getStartDateTimeOfMonth(Date date) {
- Calendar cal = Calendar.getInstance();
- cal.setTime(date);
- cal.set(Calendar.DAY_OF_MONTH, 1);
- cal.set(Calendar.HOUR_OF_DAY, 0);
- cal.set(Calendar.MINUTE, 0);
- cal.set(Calendar.SECOND, 0);
- return parse(format(cal.getTime()));
- }
-
- public static Date getStartDateTimeOfMonth(int year, int month) {
- Calendar cal = Calendar.getInstance();
- cal.set(Calendar.YEAR, year);
- cal.set(Calendar.MONTH, month - 1);
- cal.set(Calendar.DAY_OF_MONTH, 1);
- cal.set(Calendar.HOUR_OF_DAY, 0);
- cal.set(Calendar.MINUTE, 0);
- cal.set(Calendar.SECOND, 0);
- return parse(format(cal.getTime()));
- }
-
- public static Date getEndDateTimeOfMonth(Date date) {
- Calendar cal = Calendar.getInstance();
- cal.setTime(date);
- cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
- cal.set(Calendar.HOUR_OF_DAY, 23);
- cal.set(Calendar.MINUTE, 59);
- cal.set(Calendar.SECOND, 59);
- return cal.getTime();
- }
-
- public static Date addHours(Date date, int hours) {
- return add(date, Calendar.HOUR_OF_DAY, hours);
- }
-
- public static Date addMinutes(Date date, int minutes) {
- return add(date, Calendar.MINUTE, minutes);
- }
-
- public static Date addSeconds(Date date, int seconds) {
- return add(date, Calendar.SECOND, seconds);
- }
-
- public static Date addDays(Date date, int days) {
- return add(date, Calendar.DATE, days);
- }
-
- public static Date addMonths(Date date, int months) {
- return add(date, Calendar.MONTH, months);
- }
-
- public static Date addYears(Date date, int years) {
- return add(date, Calendar.YEAR, years);
- }
-
- private static Date add(Date date, int field, int amount) {
- Calendar cal = Calendar.getInstance();
- cal.setTime(date);
- cal.add(field, amount);
- return cal.getTime();
- }
-
- public static long calcDateBetween(Date start, Date end) {
- if (start == null || end == null) {
- return 0;
- }
-
- return ((end.getTime() - start.getTime()) / 86400001) + 1;
- }
-
- public static long calcHoursBetween(Date start, Date end) {
- if (start == null || end == null) {
- return 0;
- }
-
- return ((end.getTime() - start.getTime()) / (60000 * 60));
- }
-
- public static Double calcHoursDoubleBetween(Date start, Date end) {
- if (start == null || end == null) {
- return 0d;
- }
- Double time = ((end.getTime() - start.getTime()) / (60000.0 * 60.0));
- return Double.valueOf(new java.text.DecimalFormat("#.0").format(time));
- }
-
- public static Double calcHoursDouble(Date start, Date end) {
- if (start == null || end == null) {
- return 0d;
- }
- // (new BigDecimal(end.getTime()).subtract(new
- // BigDecimal(start.getTime()))).divide(new BigDecimal(60000.0*60.0), 7,
- // BigDecimal.ROUND_HALF_UP).doubleValue();
- Double time = (new BigDecimal(end.getTime()).subtract(new BigDecimal(start.getTime())))
- .divide(new BigDecimal(60000.0 * 60.0), 7, BigDecimal.ROUND_HALF_UP).doubleValue();// ((end.getTime() -
- // start.getTime())
- // /
- // (60000.0*60.0));
- return time;
- }
-
- public static long calcMinutesBetween(Date start, Date end) {
- if (start == null || end == null) {
- return 0;
- }
-
- return ((end.getTime() - start.getTime()) / 60000);
- }
-
- public static long calcSecondsBetween(Date start, Date end) {
- if (start == null || end == null) {
- return 0;
- }
- return ((end.getTime() - start.getTime()) / 1000);
- }
-
- public static long calcSecondsBetween(Long start, Long end) {
- return (end - start) / 1000;
- }
-
- /**
- * 获得日期是否为星期天。
- *
- * @param date 日期
- * @return
- */
- public static boolean isSunday(Date date) {
- return getDate(date) == Calendar.SUNDAY;
- }
-
- /**
- * 获得日期是否为星期一。
- *
- * @param date 日期
- * @return
- */
- public static boolean isMonday(Date date) {
- return getDate(date) == Calendar.MONDAY;
- }
-
- /**
- * 获得日期是否为星期二。
- *
- * @param date 日期
- * @return
- */
- public static boolean isTuesday(Date date) {
- return getDate(date) == Calendar.TUESDAY;
- }
-
- /**
- * 获得日期是否为星期三。
- *
- * @param date 日期
- * @return
- */
- public static boolean isWednesday(Date date) {
- return getDate(date) == Calendar.WEDNESDAY;
- }
-
- /**
- * 获得日期是否为星期四。
- *
- * @param date 日期
- * @return
- */
- public static boolean isThursday(Date date) {
- return getDate(date) == Calendar.THURSDAY;
- }
-
- /**
- * 获得日期是否为星期五。
- *
- * @param date 日期
- * @return
- */
- public static boolean isFriday(Date date) {
- return getDate(date) == Calendar.FRIDAY;
- }
-
- /**
- * 获得日期是否为星期六。
- *
- * @param date 日期
- * @return
- */
- public static boolean isSaturday(Date date) {
- return getDate(date) == Calendar.SATURDAY;
- }
-
- public static int getDate(Date date) {
- Calendar cal = Calendar.getInstance();
- cal.setTime(date);
- return cal.get(Calendar.DAY_OF_WEEK);
- }
-
- /**
- * 获得相对于今天的昨天的时间。
- *
- * @return 昨天此时。
- */
- public static Date getYesterday() {
- return addDays(currentDate(), -1);
- }
-
- /**
- * 获得月份
- *
- * @param date
- * @return
- */
- public static int getMonth(Date date) {
- Calendar calendar = Calendar.getInstance();
- calendar.setTime(date);
- return calendar.get(Calendar.MONTH) + 1;
- }
-
- /**
- * 获得年份
- *
- * @param date
- * @return
- */
- public static int getYear(Date date) {
- Calendar calendar = Calendar.getInstance();
- calendar.setTime(date);
- return calendar.get(Calendar.YEAR);
- }
-
- /**
- * 获得天
- *
- * @param date
- * @return
- */
- public static int getDay(Date date) {
- Calendar calendar = Calendar.getInstance();
- calendar.setTime(date);
- return calendar.get(Calendar.DATE);
- }
-
- /**
- * 获得天
- *
- * @param date
- * @return
- */
- public static int getDayOfYear(Date date) {
- Calendar calendar = Calendar.getInstance();
- calendar.setTime(date);
- return calendar.get(Calendar.DAY_OF_YEAR);
- }
-
- /**
- * 获得小时
- *
- * @param date
- * @return
- */
- public static int getHours(Date date) {
- Calendar calendar = Calendar.getInstance();
- calendar.setTime(date);
- return calendar.get(Calendar.HOUR_OF_DAY);
- }
-
- public static int getMinutes(Date date) {
- Calendar calendar = Calendar.getInstance();
- calendar.setTime(date);
- return calendar.get(Calendar.MINUTE);
- }
-
- public static int getSeconds(Date date) {
- Calendar calendar = Calendar.getInstance();
- calendar.setTime(date);
- return calendar.get(Calendar.SECOND);
- }
-
- public static int getMILLISECOND(Date date) {
- Calendar calendar = Calendar.getInstance();
- calendar.setTime(date);
- return calendar.get(Calendar.MILLISECOND);
- }
-
- public static long getTimeDiff(Date beginDate, Date endDate) {
- return (endDate.getTime() - beginDate.getTime()) / 1000;
- }
-
- public static int getDaysOfMonth(int year, int month) {
- Calendar cal = Calendar.getInstance();
- cal.set(Calendar.YEAR, year);
- cal.set(Calendar.MONTH, month);// 7月
- return cal.getActualMaximum(Calendar.DATE);
- }
-
- public static long convertDate2Long(Date date) {
- if (date == null) {
- return 0l;
- }
- return date.getTime();
- }
-
- public static Date convertLong2Date(Long unixTimestamp) {
- if (unixTimestamp != null) {
- return new Date(unixTimestamp);
- }
- return null;
- }
-
- /**
- * 判断时间是否在指定的时间范围内。
- *
- * @param low
- * @param high
- * @return
- */
- public static boolean between(Date low, Date high) {
- return currentDate().after(low) && currentDate().before(high);
- }
-
- public static int getDayValue(Date date) {
- Calendar cal = Calendar.getInstance();
- cal.setTime(date);
- int i = cal.get(Calendar.DAY_OF_WEEK);
- if (i == 1) {
- return 7;
- } else {
- return i - 1;
- }
- }
-
- public static boolean isBefore(Date date1, Date date2) {
- return date1.getTime() < date2.getTime();
- }
-
- public static Date average(Date date1, Date date2) {
- return new Date((date1.getTime() + date2.getTime()) / 2);
- }
-
- public static int getDaysOfMonth(Date date) {
- Calendar cal = Calendar.getInstance();
- cal.setTime(date);
- return cal.get(Calendar.DAY_OF_MONTH);
- }
-
- public static int getCurrentRemainingSeconds() {
- // *** 计算过期时间 一整天 获取 时:分:秒
- Calendar calendar = Calendar.getInstance();
- int hours = calendar.get(Calendar.HOUR_OF_DAY); // 时
- int minutes = calendar.get(Calendar.MINUTE); // 分
- int seconds = calendar.get(Calendar.SECOND); // 秒
- return 24 * 60 * 60 - hours * 60 * 60 - minutes * 60 - seconds;
- }
-
- public static Long getTimeLong(String str) {
- return parse(str, DATETIME_FORMAT_PATTERN).getTime();
- }
-
- public static String getStringTodayC() {
- Date currentTime = new Date();
- SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmssSSS");
- String dateString = formatter.format(currentTime);
- return dateString;
- }
-
- ///////////////////////////////////////////////////
- // 引用s https://blog.csdn.net/tz_gx/article/details/25284237
-
- // s 上月第一天
- public static Date getPreviousMonthDayBegin() {
- Calendar lastDate = Calendar.getInstance();
- lastDate.set(Calendar.DATE, 1);// 设为当前月的1号
- lastDate.add(Calendar.MONTH, -1);// 减一个月,变为上月的1号
- return lastDate.getTime();
- }
-
- // s 获得上月最后一天的日期
- public static Date getPreviousMonthDayEnd() {
- Calendar lastDate = Calendar.getInstance();
- lastDate.add(Calendar.MONTH, -1);// 减一个月
- lastDate.set(Calendar.DATE, 1);// 把日期设置为当月第一天
- lastDate.roll(Calendar.DATE, -1);// 日期回滚一天,也就是本月最后一天
- return lastDate.getTime();
- }
-
- // s 获取当月第一天
- public static Date getCurrentMonthDayBegin() {
- Calendar lastDate = Calendar.getInstance();
- lastDate.set(Calendar.DATE, 1);// 设为当前月的1号
- return lastDate.getTime();
- }
-
- // s 计算当月最后一天,返回字符串
- public static Date getCurrentMonthDayEnd() {
- Calendar lastDate = Calendar.getInstance();
- lastDate.set(Calendar.DATE, 1);// 设为当前月的1号
- lastDate.add(Calendar.MONTH, 1);// 加一个月,变为下月的1号
- lastDate.add(Calendar.DATE, -1);// 减去一天,变为当月最后一天
- return lastDate.getTime();
- }
-
- // s 获得本周一的日期
- public static Date getCurrentWeekDayBegin() {
- int mondayPlus = getMondayPlus();
- GregorianCalendar currentDate = new GregorianCalendar();
- currentDate.add(GregorianCalendar.DATE, mondayPlus);
- return currentDate.getTime();
- }
-
- // s 获得本周星期日的日期
- public static Date getCurrentWeekDayEnd() {
- int mondayPlus = getMondayPlus();
- GregorianCalendar currentDate = new GregorianCalendar();
- currentDate.add(GregorianCalendar.DATE, mondayPlus + 6);
- Date monday = currentDate.getTime();
- return monday;
- }
-
- // s 获得当前日期与本周一相差的天数
- private static int getMondayPlus() {
- Calendar cd = Calendar.getInstance();
- // 获得今天是一周的第几天,星期日是第一天,星期二是第二天......
- int dayOfWeek = cd.get(Calendar.DAY_OF_WEEK) - 1; // s 因为按中国礼拜一作为第一天所以这里减1
- if (dayOfWeek == 1) {
- return 0;
- } else {
- return 1 - dayOfWeek;
- }
- }
-
- // s 获得上周星期一的日期
- public static Date getPreviousWeekDayBegin() {
- int mondayPlus = getMondayPlus();
- GregorianCalendar currentDate = new GregorianCalendar();
- currentDate.add(GregorianCalendar.DATE, mondayPlus - 7);
- return currentDate.getTime();
- }
-
- // s 获得上周星期日的日期
- public static Date getPreviousWeekDayEnd() {
- int mondayPlus = getMondayPlus();
- GregorianCalendar currentDate = new GregorianCalendar();
- currentDate.add(GregorianCalendar.DATE, mondayPlus - 1);
- return currentDate.getTime();
- }
-
- public static int getAge(Date birthDay){
- Calendar cal = Calendar.getInstance();
- if (cal.before(birthDay)) { //出生日期晚于当前时间,无法计算
- return 0;
- }
- int yearNow = cal.get(Calendar.YEAR); //当前年份
- int monthNow = cal.get(Calendar.MONTH); //当前月份
- int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH); //当前日期
- cal.setTime(birthDay);
- int yearBirth = cal.get(Calendar.YEAR);
- int monthBirth = cal.get(Calendar.MONTH);
- int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH);
- int age = yearNow - yearBirth; //计算整岁数
- if (monthNow <= monthBirth) {
- if (monthNow == monthBirth) {
- if (dayOfMonthNow < dayOfMonthBirth) age--;//当前日期在生日之前,年龄减一
- }else{
- age--;//当前月份在生日之前,年龄减一
- }
- }
- return age;
- }
-
-
-
-
-
-
- ///////////////////////////////////
-
- public static void main(String[] args) {
- // System.out.println(getMILLISECOND(new Date()));
-
- System.out.println(format(getPreviousWeekDayEnd()));
-
- }
-
- }
|