| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442 |
- package com.xcgl.utils;
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- import java.util.ArrayList;
- import java.util.Calendar;
- import java.util.Date;
- import java.util.List;
- import org.apache.log4j.Logger;
- import org.jeecgframework.core.util.DataUtils;
- import org.jeecgframework.core.util.DateUtils;
- public class XcglDateUtils {
- private static final Logger logger = Logger.getLogger(XcglDateUtils.class);
-
- // 获得当天0点时间
- public static Date getTimesmorning() {
- Calendar cal = Calendar.getInstance();
- cal.set(Calendar.HOUR_OF_DAY, 0);
- cal.set(Calendar.SECOND, 0);
- cal.set(Calendar.MINUTE, 0);
- cal.set(Calendar.MILLISECOND, 0);
- return cal.getTime();
-
-
- }
- // 获得昨天0点时间
- public static Date getYesterdaymorning() {
- Calendar cal = Calendar.getInstance();
- cal.setTimeInMillis(getTimesmorning().getTime()-3600*24*1000);
- return cal.getTime();
- }
- // 获得当天近7天时间
- public static Date getWeekFromNow() {
- Calendar cal = Calendar.getInstance();
- cal.setTimeInMillis( getTimesmorning().getTime()-3600*24*1000*7);
- return cal.getTime();
- }
-
- // 获得当天24点时间
- public static Date getTimesnight() {
- Calendar cal = Calendar.getInstance();
- cal.set(Calendar.HOUR_OF_DAY, 24);
- cal.set(Calendar.SECOND, 0);
- cal.set(Calendar.MINUTE, 0);
- cal.set(Calendar.MILLISECOND, 0);
- return cal.getTime();
- }
-
- // 获得本周一0点时间
- public static Date getTimesWeekmorning() {
- Calendar cal = Calendar.getInstance();
- cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
- cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
- return cal.getTime();
- }
-
- // 获得本周日24点时间
- public static Date getTimesWeeknight() {
- Calendar cal = Calendar.getInstance();
- cal.setTime(getTimesWeekmorning());
- cal.add(Calendar.DAY_OF_WEEK, 7);
- return cal.getTime();
- }
-
- // 获得本月第一天0点时间
- public static Date getTimesMonthmorning() {
- Calendar cal = Calendar.getInstance();
- cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
- cal.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.DAY_OF_MONTH));
- return cal.getTime();
- }
-
- // 获得本月最后一天24点时间
- public static Date getTimesMonthnight() {
- Calendar cal = Calendar.getInstance();
- cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
- cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
- cal.set(Calendar.HOUR_OF_DAY, 24);
- return cal.getTime();
- }
-
- public static Date getLastMonthStartMorning() {
- Calendar cal = Calendar.getInstance();
- cal.setTime(getTimesMonthmorning());
- cal.add(Calendar.MONTH, -1);
- return cal.getTime();
- }
-
- public static Date getCurrentQuarterStartTime() {
- Calendar c = Calendar.getInstance();
- int currentMonth = c.get(Calendar.MONTH) + 1;
- SimpleDateFormat longSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- SimpleDateFormat shortSdf = new SimpleDateFormat("yyyy-MM-dd");
- Date now = null;
- try {
- if (currentMonth >= 1 && currentMonth <= 3)
- c.set(Calendar.MONTH, 0);
- else if (currentMonth >= 4 && currentMonth <= 6)
- c.set(Calendar.MONTH, 3);
- else if (currentMonth >= 7 && currentMonth <= 9)
- c.set(Calendar.MONTH, 6);
- else if (currentMonth >= 10 && currentMonth <= 12)
- c.set(Calendar.MONTH, 9);
- c.set(Calendar.DATE, 1);
- now = longSdf.parse(shortSdf.format(c.getTime()) + " 00:00:00");
- } catch (Exception e) {
- e.printStackTrace();
- }
- return now;
- }
-
- /**
- * 当前季度的结束时间,即2012-03-31 23:59:59
- *
- * @return
- */
- public static Date getCurrentQuarterEndTime() {
- Calendar cal = Calendar.getInstance();
- cal.setTime(getCurrentQuarterStartTime());
- cal.add(Calendar.MONTH, 3);
- return cal.getTime();
- }
-
-
- public static Date getCurrentYearStartTime() {
- Calendar cal = Calendar.getInstance();
- cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
- cal.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.YEAR));
- return cal.getTime();
- }
-
- public static Date getCurrentYearEndTime() {
- Calendar cal = Calendar.getInstance();
- cal.setTime(getCurrentYearStartTime());
- cal.add(Calendar.YEAR, 1);
- return cal.getTime();
- }
-
- public static Date getLastYearStartTime() {
- Calendar cal = Calendar.getInstance();
- cal.setTime(getCurrentYearStartTime());
- cal.add(Calendar.YEAR, -1);
- return cal.getTime();
- }
- //返回的是字符串型的时间,输入的
- //是String day, int x
- public static String addDateMinut(String day, int x)
- {
- SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- // 24小时制
- // 引号里面个格式也可以是 HH:mm:ss或者HH:mm等等,很随意的,不过在主函数调用时,要和输入的变
- // 量day格式一致
- Date date = null;
- try {
- date = format.parse(day);
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- if (date == null)
- return "";
- Calendar cal = Calendar.getInstance();
- cal.setTime(date);
- cal.add(Calendar.MINUTE, x);// 24小时制
- date = cal.getTime();
- cal = null;
- return format.format(date);
- }
-
- //返回的是字符串型的时间,输入的
- // 是String day, int x
- public static String addDateDay(String day, int x ,SimpleDateFormat format)
- {
- if(format == null) {
- format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- }
- // 24小时制
- // 引号里面个格式也可以是 HH:mm:ss或者HH:mm等等,很随意的,不过在主函数调用时,要和输入的变
- // 量day格式一致
- Date date = null;
- try {
- date = format.parse(day.length() > 10 ? day : day+" 00:00:00");
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- if (date == null)
- return "";
- Calendar cal = Calendar.getInstance();
- cal.setTime(date);
- cal.add(Calendar.DATE, x);// 24小时制
- date = cal.getTime();
- cal = null;
- return format.format(date);
- }
- // 返回的是字符串型的时间,输入的
- // 是String day, int x
- public static Date addDateDay(Date date, int x) {
- // 24小时制
- // 引号里面个格式也可以是 HH:mm:ss或者HH:mm等等,很随意的,不过在主函数调用时,要和输入的变
- // 量day格式一致
- if (date == null)
- return null;
- Calendar cal = Calendar.getInstance();
- cal.setTime(date);
- cal.add(Calendar.DATE, x);// 24小时制
- date = cal.getTime();
- cal = null;
- return date;
- }
- public static String addDateMonth(Date date, int x ,SimpleDateFormat format)
- {
- if(format == null) {
- format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- }
- if (date == null)
- return "";
- Calendar cal = Calendar.getInstance();
- cal.setTime(date);
- cal.add(Calendar.MONTH, x);
- date = cal.getTime();
- cal = null;
- return format.format(date);
- }
-
- public static List<String> getWeekMonAndSun(String indexday)
- {
- List<String> retday = new ArrayList<String>();
- SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); //设置时间格式
- Calendar cal = Calendar.getInstance();
- Date time;
- try {
- time = sdf.parse(indexday);
- cal.setTime(time);
- } catch (ParseException e) {
- e.printStackTrace();
- }
-
- //判断要计算的日期是否是周日,如果是则减一天计算周六的,否则会出问题,计算到下一周去了
-
- int dayWeek = cal.get(Calendar.DAY_OF_WEEK);//获得当前日期是一个星期的第几天
-
- if(1 == dayWeek) { cal.add(Calendar.DAY_OF_MONTH, -1); }
-
- cal.setFirstDayOfWeek(Calendar.MONDAY);
-
- //设置一个星期的第一天,按中国的习惯一个星期的第一天是星期一
-
- int day = cal.get(Calendar.DAY_OF_WEEK);//获得当前日期是一个星期的第几天
-
- cal.add(Calendar.DATE, cal.getFirstDayOfWeek()-day);
- //根据日历的规则,给当前日期减去星期几与一个星期第一天的差值 System.out.println("所在周星期一的日期:"+sdf.format(cal.getTime()));
- retday.add(sdf.format(cal.getTime()));
- // System.out.println(cal.getFirstDayOfWeek()+"-"+day+"+6="+(cal.getFirstDayOfWeek()-day+6));
-
- cal.add(Calendar.DATE, 6);
- // System.out.println("所在周星期日的日期:"+sdf.format(cal.getTime()));
- retday.add(sdf.format(cal.getTime()));
- return retday;
- }
- public static List<String> getWeekdays(String indexday)
- {
- List<String> retday = new ArrayList<String>();
- SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); //设置时间格式
- Calendar cal = Calendar.getInstance();
- Date time;
- try {
- time = sdf.parse(indexday);
- cal.setTime(time);
- } catch (ParseException e) {
- e.printStackTrace();
- }
- //判断要计算的日期是否是周日,如果是则减一天计算周六的,否则会出问题,计算到下一周去了
- int dayWeek = cal.get(Calendar.DAY_OF_WEEK);//获得当前日期是一个星期的第几天
- if(1 == dayWeek) { cal.add(Calendar.DAY_OF_MONTH, -1); }
- cal.setFirstDayOfWeek(Calendar.MONDAY);
- //设置一个星期的第一天,按中国的习惯一个星期的第一天是星期一
- int day = cal.get(Calendar.DAY_OF_WEEK);//获得当前日期是一个星期的第几天
-
- cal.add(Calendar.DATE, cal.getFirstDayOfWeek()-day);
- retday.add(sdf.format(cal.getTime()));
- //周二
- cal.add(Calendar.DATE, 1);
- retday.add(sdf.format(cal.getTime()));
- //周三
- cal.add(Calendar.DATE, 1);
- retday.add(sdf.format(cal.getTime()));
- //周四
- cal.add(Calendar.DATE, 1);
- retday.add(sdf.format(cal.getTime()));
- //周五
- cal.add(Calendar.DATE, 1);
- retday.add(sdf.format(cal.getTime()));
- //周六
- cal.add(Calendar.DATE, 1);
- retday.add(sdf.format(cal.getTime()));
- //周日
- cal.add(Calendar.DATE, 1);
- retday.add(sdf.format(cal.getTime()));
- return retday;
- }
- /**
- * 获取星期几
- * @param 日期字符串,”yyyy-MM-dd“格式 or ”yyyy-MM-dd HH:mm:ss“
- * */
- public static String getWeekOfDate(String dateStr) {
- // 24小时制
- // 引号里面个格式也可以是 HH:mm:ss或者HH:mm等等,很随意的,不过在主函数调用时,要和输入的变
- // 量day格式一致
- Date date = null;
- try {
- date = DataUtils.date_sdf.parse(dateStr.length() > 10 ? dateStr : dateStr+" 00:00:00");
- } catch (Exception ex) {
- logger.error(ex);
- ex.printStackTrace();
- }
- String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
- Calendar cal = Calendar.getInstance();
- cal.setTime(date);
- int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
- if (w < 0)
- w = 0;
- return weekDays[w];
- }
-
- /**
- * 通过时间秒毫秒数判断两个时间的间隔
- * @param date1
- * @param date2
- * @return
- */
- public static int differentDaysByMillisecond(Date date1,Date date2)
- {
- int days = (int) ((date2.getTime() - date1.getTime()) / (1000*3600*24));
- return days;
- }
- /**
- * date2比date1多的天数
- * @param date1
- * @param date2
- * @return
- */
- public static int differentDays(Date date1,Date date2)
- {
- Calendar cal1 = Calendar.getInstance();
- cal1.setTime(date1);
-
- Calendar cal2 = Calendar.getInstance();
- cal2.setTime(date2);
- int day1= cal1.get(Calendar.DAY_OF_YEAR);
- int day2 = cal2.get(Calendar.DAY_OF_YEAR);
-
- int year1 = cal1.get(Calendar.YEAR);
- int year2 = cal2.get(Calendar.YEAR);
- if(year1 != year2) //不同一年
- {
- int timeDistance = 0 ;
- if(year2 > year1) {
- for(int i = year1 ; i < year2 ; i ++)
- {
- if(i%4==0 && i%100!=0 || i%400==0) //闰年
- {
- timeDistance += 366;
- }
- else //不是闰年
- {
- timeDistance += 365;
- }
- }
- return timeDistance + (day2-day1) ;
- }else {
- for(int i = year2 ; i < year1 ; i ++)
- {
- if(i%4==0 && i%100!=0 || i%400==0) //闰年
- {
- timeDistance += 366;
- }
- else //不是闰年
- {
- timeDistance += 365;
- }
- }
- return 0 - (timeDistance + (day1-day2)) ;
- }
- }
- else //同年
- {
- return day2-day1;
- }
- }
- /**
- *
- * 计算当前日期离当年最后一天相差天数
- * */
- public static int differentDays2YearEnd(Date date) {
- Date yearEnd = new Date();
- try {
- yearEnd = DateUtils.date_sdf.parse(DateUtils.date_sdf.format(date).substring(0,4)+"-12-31");
- } catch (ParseException e) {
- logger.error(e.getMessage());
- }
-
- return differentDays(date,yearEnd);
- }
-
- /**
- *
- * 计算当前日期离当年最后一天相差天数
- * */
- public static int differentDays2YearStart(Date date) {
- Date yearStart = new Date();
- try {
- yearStart = DateUtils.date_sdf.parse(DateUtils.date_sdf.format(date).substring(0,4)+"-01-01");
- } catch (ParseException e) {
- logger.error(e.getMessage());
- }
-
- return differentDays(yearStart,date);
- }
-
- public static Date getLastDayByMonth(Date date) {
- //获取当前月最后一天
- Calendar ca = Calendar.getInstance();
- ca.setTime(date);
- ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));
- return ca.getTime();
- }
- public static Date getFirstDayByMonth(Date date) {
- Calendar c = Calendar.getInstance();
- c.setTime(date);
- c.add(Calendar.MONTH, 0);
- c.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天
- return c.getTime();
- }
- }
|