1、代码
package com.jh.jcs.attence.calendar.util;
import com.jh.jcs.framework.common.util.DateUtil;
import com.jh.jcs.framework.common.util.SpringContextUtil;
import com.jh.jcs.platform.sys.user.service.IUserService;
import org.apache.poi.hssf.record.formula.functions.T;
import javax.servlet.ServletRequest;
import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
/*
* @Description: 常用工具类
* @Author: Jk_kang
* @CreateDate: 2020/11/24 9:59
* @Param:
* @Return:
**/
public class DailyUtil {
private static IUserService userService = SpringContextUtil.getBean ("userService");
// 工具方法2:用于部门和个人统计,根据模块填充 processInstanceIdd
public static <T, E> List<T> addProcessInstanceIdModel(List<T> list, Class<E> clazz) {
if (DailyUtil.isCollectionEmpty (list)) {
for (int i = 0; i < list.size ( ); i++) {
try {
T t = list.get (i);
Field processInstanceIddField = clazz.getDeclaredField ("processInstanceIdd");
Field processInstanceIddField2 = t.getClass ( ).getDeclaredField ("processInstanceIdd");
Field idField = t.getClass ( ).getDeclaredField ("id");
if (idField.getGenericType ( ) == String.class) {
idField.setAccessible (true);
// 拿到 t 对象中的属性值
String id = (String) idField.get (t);
Object o = userService.get (clazz, id);
if (DailyUtil.isObject (o)) {
if (processInstanceIddField.getGenericType ( ) == String.class) {
processInstanceIddField.setAccessible (true);
String idd = (String) processInstanceIddField.get (o);
// 反射进去
processInstanceIddField2.setAccessible (true);
processInstanceIddField2.set (t, idd);
}
}
}
list.set (i, t);
} catch (Exception e) {
e.printStackTrace ( );
}
}
}
return list;
}
/**
* 用于 OA分割的机构单位,多选搜索查询
* @return
*/
public static String currencySplitDeptOrOrg(String name){
if (isStringEmpty (name)){
String[] split = name.split (",");
String pids= "";
for (String pid : split) {
pids+="'"+pid+"',";
}
return pids.substring (0,pids.length ()-1);
}
return null;
}
/**
* 计算生日
* @param bir 格式必须为 yyyy-mm-dd
* @return
*/
public static String algorithmBir(String bir){
try{
if (DailyUtil.isStringEmpty (bir)){
String currentDateShortStyle = DateUtil.getCurrentDateShortStyle ( );
int year1 = Integer.valueOf (currentDateShortStyle.substring (0,4));
int month1 = Integer.valueOf (currentDateShortStyle.substring (5,7));
int day1 = Integer.valueOf (currentDateShortStyle.substring (8,10));
int year2 = Integer.valueOf (bir.substring (0,4));
int month2 = Integer.valueOf (bir.substring (5,7));
int day2 = Integer.valueOf (bir.substring (8,10));
int age = year1 - year2;
int month = month1 - month2;
int day = day1 - day2;
if (month>0){
age+=1;
}
if (day>0&&month==0){
age+=1;
}
return age+"";
}
}catch (Exception e){
e.getStackTrace ();
}
return "";
}
/**
* 计算天数
* @param day 背修改的天数
* @param dayNum + 增加 ,- 减 天数
* @return
*/
public static String algorithmDay(String day,int dayNum){
try {
SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd");
Date parse = sdf.parse (day);
Calendar calendar = Calendar.getInstance();
calendar.setTime (parse);
calendar.add(Calendar.DAY_OF_MONTH, dayNum);
return sdf.format (calendar.getTime ());
} catch (ParseException e) {
e.printStackTrace ( );
}
return null;
}
// 根据传入的时间,比较大小
// 只允许 时分
// eg:s1 10:12 s2 12:33 ,则返回 -1
// return 返回 -1 左比右小 ,反之 ,返回 0则相等
// 扩展思想:可以返回对应的秒差值
public static Integer timeBranchComparison(String time1 ,String time2){
try {
int time1One = Integer.valueOf (time1.substring (0, 2));
int time1Two = Integer.valueOf (time1.substring (3));
int time2One = Integer.valueOf (time2.substring (0, 2));
int time2Two = Integer.valueOf (time2.substring (3));
if (time1One >time2One){
return 1;
}else if (time1One ==time2One){
if (time1Two>time2Two){
return 1;
}else if (time1Two==time2Two){
return 0;
}else {
return -1;
}
}else if (time1One <time2One){
return -1;
}
}catch (Exception e){
e.getStackTrace ();
}
return null;
}
// 获取当月和上个月分的区间
public static List<String> getlastMothNow() throws Exception {
SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd");
Calendar cal = Calendar.getInstance ( );
cal.setTime (new Date ( )); // 设置为当前时间
cal.set (Calendar.MONTH, cal.get (Calendar.MONTH) - 1); // 设置为上一个月
cal.set (Calendar.DAY_OF_MONTH, 1);//设置为1号,当前日期既为本月第一天 // 设置为上月的一号
String lastDay1 = sdf.format (cal.getTime ( ));
//获取前月的最后一天
Calendar cal2 = Calendar.getInstance ( );
cal2.set (Calendar.MONTH, cal2.get (Calendar.MONTH) + 1);
cal2.set (Calendar.DAY_OF_MONTH, 0);//设置为1号,当前日期既为本月第一天
String lastDay2 = sdf.format (cal2.getTime ( ));
return findDates (lastDay1, lastDay2);
}
// 获取上个月的月份
public static String lastMonth() {
SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM");
Calendar cal = Calendar.getInstance ( );
cal.setTime (new Date ( ));
cal.add (cal.MONTH, -1);
return sdf.format (cal.getTime ( ));
}
// 获取下个月的月份
public static String nextMonth() {
SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM");
Calendar cal = Calendar.getInstance ( );
cal.setTime (new Date ( ));
cal.add (cal.MONTH, 1);
return sdf.format (cal.getTime ( ));
}
// 获取这个月的月份
public static String sameMonth() {
SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM");
Calendar cal = Calendar.getInstance ( );
cal.setTime (new Date ( ));
// cal.add (cal.MONTH, -1);
return sdf.format (cal.getTime ( ));
}
// 获取上个月的月份2
public static String lastMonth2() {
SimpleDateFormat sdf = new SimpleDateFormat ("MM");
Calendar cal = Calendar.getInstance ( );
cal.setTime (new Date ( ));
cal.add (cal.MONTH, -1);
return sdf.format (cal.getTime ( ));
}
/*
* @Description: 获取当天前后的区间日历
* @Author: Jk_kang
* @CreateDate: 2021/6/9 16:20
* @Param: [num] 天数
* @Return: java.util.List<java.lang.String>
eg: 传入 -30 则返回前30天所有的日期
**/
public static List<String> findDates(int num) throws Exception {
SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd");
Calendar cal = Calendar.getInstance ( );
cal.setTime (new Date ( ));
cal.add (cal.DATE, num);
String format = sdf.format (cal.getTime ( ));
if (num == 0) {
return null;
}
if (num < 0) {
return findDates (format, sdf.format (new Date ( )));
}
if (num > 0) {
return findDates (sdf.format (new Date ( )), format);
}
return null;
}
/**
*
* @param state 1为第一天,-1为当月最后一天
* @return
*/
public static String getCurrentMonthfirstDayOrlastDay(int state){
SimpleDateFormat format = new SimpleDateFormat ("yyyy-MM-dd");
if (state==1){
//获取前月的第一天
Calendar cal_1=Calendar.getInstance();//获取当前日期
//cal_1.add(Calendar.MONTH, -1);
cal_1.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天
String firstDay = format.format(cal_1.getTime());
return firstDay;
}
else if (state==-1){
//获取前月的最后一天
Calendar cale = Calendar.getInstance();
cale.add(Calendar.MONTH, 1);
cale.set(Calendar.DAY_OF_MONTH,0);//设置为1号,当前日期既为本月第一天
String lastDay = format.format(cale.getTime());
return lastDay;
}
return "";
}
/**
*
* @param state 1为第一天,-1为当月最后一天
* @return
*/
public static String getCurrentMonthfirstDayOrlastDayToNextmonth(int state){
SimpleDateFormat format = new SimpleDateFormat ("yyyy-MM-dd");
if (state==1){
//获取前月的第一天
Calendar cal_1=Calendar.getInstance();//获取当前日期
cal_1.add(Calendar.MONTH, 1);
cal_1.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天
String firstDay = format.format(cal_1.getTime());
return firstDay;
}
else if (state==-1){
//获取前月的最后一天
Calendar cale = Calendar.getInstance();
cale.add(Calendar.MONTH, 2);
cale.set(Calendar.DAY_OF_MONTH,0);//设置为1号,当前日期既为本月第一天
String lastDay = format.format(cale.getTime());
return lastDay;
}
return "";
}
/**
* @param
* @return java.util.List<java.lang.String>
* @title 根据开始时间,结束时间获取期间所有日期yyyy-MM-dd
* 例:开始时间:2019-05-01 结束时间:2019-05-05
*/
public static List<String> findDates(String stime, String etime) throws Exception {
SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd");
SimpleDateFormat sdf1 = new SimpleDateFormat ("yyyy-MM-dd");
Date dBegin = sdf.parse (stime);
Date dEnd = sdf.parse (etime);
List<String> allDate = new ArrayList ( );
allDate.add (sdf1.format (dBegin));
Calendar calBegin = Calendar.getInstance ( );
// 使用给定的 Date 设置此 Calendar 的时间
calBegin.setTime (dBegin);
Calendar calEnd = Calendar.getInstance ( );
// 使用给定的 Date 设置此 Calendar 的时间
calEnd.setTime (dEnd);
// 测试此日期是否在指定日期之后
while (dEnd.after (calBegin.getTime ( ))) {
// 根据日历的规则,为给定的日历字段添加或减去指定的时间量
calBegin.add (Calendar.DAY_OF_MONTH, 1);
allDate.add (sdf1.format (calBegin.getTime ( )));
}
return allDate;
}
// equals增强
public static boolean equals(String s1,String s2){
if (s1==null||s2==null||"".equals (s1)||"".equals (s2)){
return false;
}
return s1.equals (s2);
}
// 对象判空,true 返回不是空(符合)
public static boolean isObject(Object o) {
if (o != null) return true;
return false;
}
// 对象判空,true 返回不是空(符合)
public static boolean isObject(Object... o) {
for (Object oo : o) {
if (oo == null) return false;
}
return true;
}
// 字符串判空,true 返回不是空(符合)有值
public static boolean isStringEmpty(String o) {
if (o != null && !"".equals (o)) {
return true;
}
return false;
}
// 2021扩展:多字符串判空,true 返回不是空(符合)有值
public static boolean isStringEmpty(String... s) {
int count = 0;
for (String s1 : s) {
if (s1 != null && !"".equals (s1)) {
count += 1;
}
}
return count==s.length;
}
// 集合判空,true 返回不是空(符合)
public static boolean isCollectionEmpty(Collection col) {
if (col != null && col.size ( ) > 0) {
return true;
}
return false;
}
// Map判空,true 返回不是空(符合)
public static boolean isMapEmpty(Map map) {
return map != null && map.size ( ) > 0;
}
// 数组判空,true 返回不是空(符合)
public static boolean isArray(Object[] os) {
if (os != null && os.length > 0) return true;
return false;
}
// 实体注入工厂:根据实体对应属性匹配请求过来的属性
// 返回匹配到有值得属性
// 在BeseAction 上重载,作为增强
public static <T> T getBeanAction(Class<T> clazz, ServletRequest request) {
// try {
// Map parameterMap = request.getParameterMap ( );
// Field[] declaredFields = clazz.getClass ( ).getDeclaredFields ( );
// Class resultBean = clazz.getClass ().newInstance ( );
//
// for (Field field : declaredFields) {
// String name = field.getName ( );
// if (parameterMap.containsKey (name)) {
// field.setAccessible (true);
// field.set (field,parameterMap.get (name));
// }
// }
//
// return (T) resultBean;
//
// } catch (InstantiationException e) {
// e.printStackTrace ( );
// } catch (IllegalAccessException e) {
// e.printStackTrace ( );
// }
return null;
}
/**
* 将Object对象里面的属性和值转化成Map对象 --- 针对 easyUI 考勤中的
*
* @param obj
* @return
* @throws IllegalAccessException
*/
public static Map<String, String> objectToMap(Object obj) {
if (obj == null) {
return null;
}
Map<String, String> map = new HashMap<> ( );
try {
BeanInfo beanInfo = Introspector.getBeanInfo (obj.getClass ( ));
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors ( );
for (PropertyDescriptor property : propertyDescriptors) {
String key = property.getName ( );
if (key.compareToIgnoreCase ("class") == 0) {
continue;
}
Method getter = property.getReadMethod ( );
String value = (String) (getter != null ? getter.invoke (obj) : null);
map.put (key, value);
}
} catch (Exception e) {
e.printStackTrace ( );
}
return map;
}
/**
* 将Map转成javaBean对象
*
* @param map
* @param hfxy_merchant
* @return
* @throws Exception
*/
public static Object mapToObject(Map<String, Object> map, Class<?> hfxy_merchant) throws Exception {
if (map == null)
return null;
Object obj = hfxy_merchant.newInstance ( );
java.lang.reflect.Field[] fields = obj.getClass ( ).getDeclaredFields ( );
for (java.lang.reflect.Field field : fields) {
int mod = field.getModifiers ( );
if (Modifier.isStatic (mod) || Modifier.isFinal (mod)) {
continue;
}
field.setAccessible (true);
field.set (obj, map.get (field.getName ( )));
}
return obj;
}
/**
* 利用反射将map集合封装成bean对象
*
* @param map
* @param clazz
* @return
*/
public static <T> T mapToBean(Map<String, Object> map, Class<?> clazz) throws Exception {
Object obj = clazz.newInstance ( );
if (map != null && !map.isEmpty ( ) && map.size ( ) > 0) {
for (Map.Entry<String, Object> entry : map.entrySet ( )) {
String propertyName = entry.getKey ( ); // 属性名
Object value = entry.getValue ( ); // 属性值
String setMethodName = "set" + propertyName.substring (0, 1).toUpperCase ( ) + propertyName.substring (1);
Field field = getClassField (clazz, propertyName); //获取和map的key匹配的属性名称
if (field == null) {
continue;
}
Class<?> fieldTypeClass = field.getType ( );
value = convertValType (value, fieldTypeClass);
try {
clazz.getMethod (setMethodName, field.getType ( )).invoke (obj, value);
} catch (NoSuchMethodException e) {
e.printStackTrace ( );
}
}
}
return (T) obj;
}
/**
* 根据给定对象类匹配对象中的特定字段
*
* @param clazz
* @param fieldName
* @return
*/
private static Field getClassField(Class<?> clazz, String fieldName) {
if (Object.class.getName ( ).equals (clazz.getName ( ))) {
return null;
}
Field[] declaredFields = clazz.getDeclaredFields ( );
for (Field field : declaredFields) {
if (field.getName ( ).equals (fieldName)) {
return field;
}
}
Class<?> superClass = clazz.getSuperclass ( ); //如果该类还有父类,将父类对象中的字段也取出
if (superClass != null) { //递归获取
return getClassField (superClass, fieldName);
}
return null;
}
/**
* 将map的value值转为实体类中字段类型匹配的方法
*
* @param value
* @param fieldTypeClass
* @return
*/
private static Object convertValType(Object value, Class<?> fieldTypeClass) {
Object retVal = null;
if (Long.class.getName ( ).equals (fieldTypeClass.getName ( ))
|| long.class.getName ( ).equals (fieldTypeClass.getName ( ))) {
retVal = Long.parseLong (value.toString ( ));
} else if (Integer.class.getName ( ).equals (fieldTypeClass.getName ( ))
|| int.class.getName ( ).equals (fieldTypeClass.getName ( ))) {
retVal = Integer.parseInt (value.toString ( ));
} else if (Float.class.getName ( ).equals (fieldTypeClass.getName ( ))
|| float.class.getName ( ).equals (fieldTypeClass.getName ( ))) {
retVal = Float.parseFloat (value.toString ( ));
} else if (Double.class.getName ( ).equals (fieldTypeClass.getName ( ))
|| double.class.getName ( ).equals (fieldTypeClass.getName ( ))) {
retVal = Double.parseDouble (value.toString ( ));
} else {
retVal = value;
}
return retVal;
}
}
2、使用
网友评论