private static Logger logger = Logger.getLogger(commonUtil.class);
public static final String YEAR = "YEAR";
public static final String MONTH = "MONTH";
public static final String DAY = "DAY";
public static final String WEEK = "WEEK";
@Autowired
private CConfigService cConfigService;
private static commonUtil commonUtil ; // 关键点1 静态初使化 一个工具类 这样是为了在spring初使化之前
public void setTestService(CConfigService cConfigService) {
this.cConfigService = cConfigService;
}
@PostConstruct //关键二 通过@PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作
public void init() {
commonUtil = this;
commonUtil.cConfigService = this.cConfigService; // 初使化时将已静态化的testService实例化
}
/**
* 将一个字符串转换成日期格式
*
* @param date
* @param pattern
* @return
*/
public static Date toDate(String date, String pattern) {
if (("" + date).equals("")) {
return null;
}
if (pattern == null) {
pattern = "yyyy-MM-dd";
}
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
Date newDate = new Date();
try {
newDate = sdf.parse(date);
} catch (Exception ex) {
ex.printStackTrace();
}
return newDate;
}
/**
* 把日期转换成字符串型
*
* @param date
* @param pattern
* @return
*/
public static String toString(Date date, String pattern) {
if (date == null) {
return "";
}
if (pattern == null) {
pattern = "yyyy-MM-dd";
}
String dateString = "";
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
try {
dateString = sdf.format(date);
} catch (Exception ex) {
ex.printStackTrace();
}
return dateString;
}
/**
* 获取当前时间年月日
*
* @param type
* @param date
* @return
*/
public static int getRes(String type, Object date) {
Calendar c = Calendar.getInstance();
try {
if (date instanceof Date) {
c.setTime((Date) date);
} else {
c.setTime(toDate(date.toString(), "yyyy-MM-dd"));
}
if (type.equals(YEAR)) {
return (int) c.get(Calendar.YEAR);
} else if (type.equals(MONTH)) {
return (int) c.get(Calendar.MONTH) + 1;
} else if (type.equals(DAY)) {
return (int) c.get(Calendar.DAY_OF_MONTH);
} else if (type.equals(WEEK)) {
return (int) c.get(Calendar.WEEK_OF_YEAR);
}
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}
/**
* 获得某个月最大天数
*
* @param
* @return 某个月最大天数
*/
public static int getDaysOfMonth(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
}
/*
* 把日期转换成*月*日
* */
public static String castDateToString(Object date) {
Calendar c = Calendar.getInstance();
if (date instanceof Date) {
c.setTime((Date) date);
} else {
c.setTime(toDate(date.toString(), "yyyy-MM-dd"));
}
int mouth = c.get(Calendar.MONTH) + 1;
int day = c.get(Calendar.DAY_OF_MONTH);
return mouth + "月" + day + "日";
}
/**
* 获取上个月最后一天
*
* @param date 当月
* @return
*/
public static String getPrevMonthLastDay(String date) {
Date curDate = toDate(date, "yyyy-MM");
Calendar calendar = Calendar.getInstance();
calendar.setTime(curDate);
calendar.set(Calendar.DAY_OF_MONTH, 0);
Date prevDate = calendar.getTime();
return toString(prevDate, "yyyy-MM-dd");
}
/**
* 获取上年最后月
*
* @param year 年份
* @return
*/
public static String getPrevYearLastMonth(String year) {
Date curDate = toDate(year, "yyyy");
Calendar calendar = Calendar.getInstance();
calendar.setTime(curDate);
calendar.set(Calendar.DAY_OF_YEAR, 0);
Date prevDate = calendar.getTime();
return toString(prevDate, "yyyy-MM");
}
/**
* 上一个月
*
* @param date
* @return
*/
public static Date getPrevMonth(Date date) {
Calendar calendar = Calendar.getInstance();//日历对象
calendar.setTime(date);//设置当前日期
calendar.add(Calendar.MONTH, -1);//月份减一
return calendar.getTime();
}
/**
* 上一天
* @param date
* @return
*/
public static Date getPrevDay(Date date) {
Calendar calendar = Calendar.getInstance();//日历对象
calendar.setTime(date);//设置当前日期
calendar.add(Calendar.DAY_OF_WEEK, -1);
return calendar.getTime();
}
/**
* 下一天
* @param date
* @return
*/
public static Date getNextDay(Date date) {
Calendar calendar = Calendar.getInstance();//日历对象
calendar.setTime(date);//设置当前日期
calendar.add(Calendar.DAY_OF_WEEK, 1);
return calendar.getTime();
}
/**
* 获取时间差
*
* @param endTime
* @param beginTime
* @return
*/
public static String getDifference(Date endTime, Date beginTime) {
if (endTime == null) {
endTime = getEndTime(beginTime);
}
long time1 = endTime.getTime();
long time2 = beginTime.getTime();
long diff;
if (time1 < time2) {
diff = time2 - time1;
} else {
diff = time1 - time2;
}
long hour = (diff / (60 * 60 * 1000));
long min = ((diff / (60 * 1000)) - hour * 60);
long sec = (diff / 1000 - hour * 60 * 60 - min * 60);
return hour + "h" + min + "m" + sec + "s";
}
/**
* 获取当天结束时间
*
* @param date
* @return
*/
public static Date getEndTime(Date date) {
Calendar todayEnd = Calendar.getInstance();
todayEnd.setTime(date);
todayEnd.set(Calendar.HOUR, 23);
todayEnd.set(Calendar.MINUTE, 59);
todayEnd.set(Calendar.SECOND, 59);
todayEnd.set(Calendar.MILLISECOND, 999);
return todayEnd.getTime();
}
public static String longToString(long currentTime, String formatType) {
Date dateOld = new Date(currentTime);
String sDateTime = toString(dateOld, formatType);
Date date = toDate(sDateTime, formatType);
return toString(date,formatType);
}