zhoudw 7 years ago
parent
commit
baabe4992d

+ 28 - 0
applications/commons/commons-api/src/main/java/com/usoftchina/saas/commons/api/MaxnumberService.java

@@ -0,0 +1,28 @@
+package com.usoftchina.saas.commons.api;
+import com.usoftchina.saas.base.Result;
+
+import org.springframework.cloud.openfeign.FeignClient;
+import org.springframework.web.bind.annotation.PostMapping;
+
+@FeignClient("commons-server")
+public interface MaxnumberService {
+    /**
+    * @Description 更新并获取最大编号
+    * @Param: [count, code, caller]
+    * @return: com.usoftchina.saas.base.Result
+    * @Author: guq
+    * @Date: 2018/10/19
+    */
+    @PostMapping("api/commons/number/pushMaxnubmer")
+    public Result pushMaxnubmer(Integer count, String code, String caller);
+
+    /**
+    * @Description 前端获取编号
+    * @Param: [caller, update]
+    * @return: com.usoftchina.saas.base.Result
+    * @Author: guq
+    * @Date: 2018/10/19
+    */
+    @PostMapping("api/commons/number/getMaxnumber")
+    public Result getMaxnumber(String caller, boolean update);
+}

+ 30 - 0
applications/commons/commons-dto/src/main/java/com/usoftchina/saas/commons/exception/BizExceptionCode.java

@@ -0,0 +1,30 @@
+package com.usoftchina.saas.commons.exception;
+
+import com.usoftchina.saas.exception.BaseExceptionCode;
+import com.usoftchina.saas.exception.ExceptionCode;
+
+public enum BizExceptionCode implements BaseExceptionCode {
+
+
+    // 业务异常
+    MAX_NUM_OUTOF_BOUNDS(52000, "流水号长度大于最大设定的长度");
+
+
+    private int code;
+    private String message;
+
+    BizExceptionCode(int code, String message) {
+        this.code = code;
+        this.message = message;
+    }
+
+    @Override
+    public int getCode() {
+        return code;
+    }
+
+    @Override
+    public String getMessage() {
+        return message;
+    }
+}

+ 34 - 0
applications/commons/commons-server/src/main/java/com/usoftchina/saas/commons/controller/MaxnumberController.java

@@ -0,0 +1,34 @@
+package com.usoftchina.saas.commons.controller;
+
+
+import com.usoftchina.saas.base.Result;
+import com.usoftchina.saas.commons.service.MaxnumberService;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+/**
+ * @author: guq
+ * @create: 2018-10-18 17:40
+ **/
+@RequestMapping("/number")
+@RestController
+public class MaxnumberController {
+
+    @Autowired
+    private MaxnumberService maxnumberService;
+
+    @PostMapping("/getMaxnumber")
+    public Result getMaxnumber(String caller, boolean update) {
+        String code = maxnumberService.getMaxnumner(caller, update);
+        return Result.success(code);
+    }
+
+    @PostMapping("/pushMaxnubmer")
+    public Result pushMaxnubmer(Integer count, String code, String caller) {
+        String maxnubmer = maxnumberService.pushMaxnubmer(count, code, caller);
+        return Result.success(maxnubmer);
+    }
+}

+ 15 - 0
applications/commons/commons-server/src/main/java/com/usoftchina/saas/commons/service/MaxnumberService.java

@@ -0,0 +1,15 @@
+package com.usoftchina.saas.commons.service;
+
+import com.usoftchina.saas.base.service.CommonBaseService;
+import com.usoftchina.saas.commons.mapper.MaxnumbersMapper;
+import com.usoftchina.saas.commons.po.Maxnumbers;
+
+/**
+ * @author: guq
+ * @create: 2018-10-18 18:01
+ **/
+public interface MaxnumberService extends CommonBaseService<MaxnumbersMapper, Maxnumbers> {
+    String getMaxnumner(String caller, boolean update);
+
+    String pushMaxnubmer(Integer count, String code, String caller);
+}

+ 227 - 0
applications/commons/commons-server/src/main/java/com/usoftchina/saas/commons/service/impl/MaxnumberServiceImpl.java

@@ -0,0 +1,227 @@
+package com.usoftchina.saas.commons.service.impl;
+
+import com.usoftchina.saas.base.service.CommonBaseServiceImpl;
+import com.usoftchina.saas.commons.exception.BizExceptionCode;
+import com.usoftchina.saas.commons.mapper.MaxnumbersMapper;
+import com.usoftchina.saas.commons.mapper.MaxnumbersdetailMapper;
+import com.usoftchina.saas.commons.po.Maxnumbers;
+import com.usoftchina.saas.commons.po.MaxnumbersExample;
+import com.usoftchina.saas.commons.po.Maxnumbersdetail;
+import com.usoftchina.saas.commons.po.MaxnumbersdetailExample;
+import com.usoftchina.saas.commons.service.MaxnumberService;
+import com.usoftchina.saas.context.BaseContextHolder;
+
+import com.usoftchina.saas.exception.BizException;
+import com.usoftchina.saas.utils.DateUtils;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+import java.util.Date;
+
+
+/**
+ * @author: guq
+ * @create: 2018-10-18 18:08
+ **/
+@Service
+public class MaxnumberServiceImpl extends CommonBaseServiceImpl<MaxnumbersMapper, Maxnumbers> implements MaxnumberService{
+
+    @Autowired
+    private MaxnumbersMapper maxnumbersMapper;
+    @Autowired
+    private MaxnumbersdetailMapper maxnumbersdetailMapper;
+
+    /**
+    * @Description
+    * 获取单据编号
+    * @Param: [caller]
+    * @return: java.lang.String
+    * @Author: guq
+    * @Date: 2018/10/18
+    */
+    @Override
+    public String getMaxnumner(String caller, boolean update) {
+        if (null == caller) {
+            return null;
+        }
+        //单号
+        String maxcode = null;
+        //前缀+日期
+        String rulecode = null;
+        //日期
+        String date = null;
+        //流水位数
+        Integer length = 0;
+        //流水最大数
+        Integer maxno = 1;
+        Long companyId = BaseContextHolder.getCompanyId();
+        Long userId = BaseContextHolder.getUserId();
+        //查询编号规则
+        Maxnumbers maxnumbers = getMaxnumbers(caller);
+        if (null != maxnumbers) {
+           rulecode = getRulecode(maxnumbers);
+           length = maxnumbers.getMn_number();
+            //查询从表是否存在该规则数据
+            Maxnumbersdetail detail = getMaxDetail(caller, rulecode);
+            if (null == detail) {
+                //不存在则插入
+                detail = new Maxnumbersdetail();
+                detail.setMd_caller(caller);
+                detail.setMd_leadcode(rulecode);
+                detail.setMd_maxno(maxno);
+                detail.setCompanyId(companyId);
+                detail.setCreateTime(new Date());
+                detail.setCreatorId(userId);
+                maxnumbersdetailMapper.insertSelective(detail);
+            } else {
+                //存在则取最大值
+                maxno = detail.getMd_maxno() + 1;
+            }
+            maxcode = rulecode + createSerialNum(length, maxno);
+            //如果以更新方式取编号
+            if (update) {
+                detail.setUpdateTime(new Date());
+                detail.setUpdaterId(userId);
+                detail.setMd_maxno(maxno);
+                maxnumbersdetailMapper.updateByPrimaryKeySelective(detail);
+            }
+        }
+        return maxcode;
+    }
+
+    /**
+    * @Description 更新并返回单据最大编号
+    * @Param: [count, code, caller]
+    * @return: java.lang.String
+    * @Author: guq
+    * @Date: 2018/10/18
+    */
+    @Override
+    public String pushMaxnubmer(Integer count, String code, String caller) {
+        if (count > 0) {
+            return getMaxnumner(caller, true);
+        }
+        //单号
+        String maxcode =  code;
+        //公司Id
+        Long companyId = BaseContextHolder.getCompanyId();
+        //人员Id
+        Long userId = BaseContextHolder.getUserId();
+        //前缀+日期
+        String rulecode = null;
+        //流水位数
+        Integer length = 0;
+        //查询编号规则
+        Maxnumbers maxnumbers = getMaxnumbers(caller);
+        if (null == maxnumbers) {
+            return maxcode;
+        }
+        rulecode = getRulecode(maxnumbers);
+        //编号与前缀+日期不匹配,直接返回
+        if (maxcode.indexOf(rulecode) != 0) {
+            return maxcode;
+        }
+        //查询从表是否存在该规则数据
+        Maxnumbersdetail detail = getMaxDetail(caller, rulecode);
+        if (null != detail) {
+            //存在则更新表中最大流水号
+            length = maxnumbers.getMn_number();
+            Integer serialNum = praseSerialNum(maxcode, length);
+            if (null != serialNum) {
+                detail.setMd_maxno(serialNum);
+                detail.setUpdaterId(userId);
+                detail.setUpdateTime(new Date());
+                maxnumbersdetailMapper.updateByPrimaryKeySelective(detail);
+            }
+
+        }
+        return maxcode;
+    }
+
+    /**
+    * @Description
+    * 根据长度跳流水号
+    * @Param: [len, maxno]
+    * @return: java.lang.String
+    * @Author: guq
+    * @Date: 2018/10/18
+    */
+    private String createSerialNum(Integer len, Integer maxno) {
+        if (maxno.toString().length() > len) {
+            throw new BizException(BizExceptionCode.MAX_NUM_OUTOF_BOUNDS);
+        }
+        String prefix = "";
+        for (int i = maxno.toString().length(); i < len; i++) {
+            prefix += "0";
+        }
+        return prefix + maxno;
+    }
+
+    /**
+    * @Description 跟据长度解析最大流水
+    * @Param: [code, len]
+    * @return: java.lang.Integer
+    * @Author: guq
+    * @Date: 2018/10/18
+    */
+    private Integer praseSerialNum(String code, Integer len) {
+        Integer serialNum = null;
+        //截取流水号
+        String num = code.substring(code.length() -len);
+        //转换
+        try {
+            serialNum = Integer.valueOf(num);
+        } catch (NumberFormatException ex) {
+        }
+        return serialNum;
+    }
+
+    /**
+    * @Description 根据caller查编号配置
+    * @Param: [companyId, caller]
+    * @return: com.usoftchina.saas.commons.po.Maxnumbers
+    * @Author: guq
+    * @Date: 2018/10/19
+    */
+    private Maxnumbers getMaxnumbers(String caller) {
+        MaxnumbersExample example = new MaxnumbersExample();
+        MaxnumbersExample.Criteria cta = example.createCriteria();
+        cta.andCompanyIdEqualTo(BaseContextHolder.getCompanyId());
+        cta.andMn_callerEqualTo(caller);
+        return maxnumbersMapper.selectByExample(example);
+    }
+    /**
+    * @Description
+    * 根据公司id,caller,前缀查找明细
+    * @Param: [companyId, caller, rulecode]
+    * @return: com.usoftchina.saas.commons.po.Maxnumbersdetail
+    * @Author: guq
+    * @Date: 2018/10/18
+    */
+    private Maxnumbersdetail getMaxDetail(String caller, String rulecode) {
+        MaxnumbersdetailExample detailexample = new MaxnumbersdetailExample();
+        MaxnumbersdetailExample.Criteria detailcta = detailexample.createCriteria();
+        detailcta.andCompanyIdEqualTo(BaseContextHolder.getCompanyId());
+        detailcta.andMd_callerEqualTo(caller);
+        detailcta.andMd_leadcodeEqualTo(rulecode);
+        return maxnumbersdetailMapper.selectByExample(detailexample);
+    }
+
+    /**
+    * @Description 根据配置,获取前缀+日期
+    * @Param: [maxnumbers]
+    * @return: java.lang.String
+    * @Author: guq
+    * @Date: 2018/10/18
+    */
+    private String getRulecode(Maxnumbers maxnumbers) {
+        String rulecode = maxnumbers.getMn_leadcode();
+        Integer ength = maxnumbers.getMn_number();
+        String mn_rule = maxnumbers.getMn_rule();
+        String date = "YYYYMM".equals(mn_rule) ? DateUtils.format(new Date(), "yyyyMM") :
+                DateUtils.format(new Date(), "yyyyMMdd");
+        return rulecode += date;
+    }
+
+}

+ 487 - 0
framework/core/src/main/java/com/usoftchina/saas/utils/DateUtils.java

@@ -0,0 +1,487 @@
+package com.usoftchina.saas.utils;
+
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.*;
+
+/**
+ * @author: guq
+ * @create: 2018-10-18 19:17
+ **/
+public class DateUtils {
+
+    static final SimpleDateFormat ym = new SimpleDateFormat("yyyyMM");
+    static final SimpleDateFormat YM = new SimpleDateFormat("yyyy-MM");
+    static final SimpleDateFormat YMD = new SimpleDateFormat("yyyy-MM-dd");
+    static final SimpleDateFormat YMD_HMS = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+
+    static final String YMD_STR = "yyyy-MM-dd";
+    static final String ORACLE_YMD_HMS_STR = "yyyy-MM-dd HH24:mi:ss";
+    static final String ORACLE_YMD_STR = "yyyy-MM-dd";
+    static final String YMD_HMS_STR = "yyyy-MM-dd HH:mm:ss";
+
+    public static String format(Date date, String f) {
+        if (date == null) {
+            date = new Date();
+        }
+        if (f == null) {
+            return YMD.format(date);
+        }
+        SimpleDateFormat sdf = new SimpleDateFormat(f);
+        return sdf.format(date);
+    }
+
+    public static Date parse(String date, String f) {
+        if (date == null) {
+            return new Date();
+        }
+        if (f == null) {
+            try {
+                return date.contains(" ") ? YMD_HMS.parse(date) : YMD.parse(date);
+            } catch (ParseException e) {
+                return new Date();
+            }
+        }
+        SimpleDateFormat sdf = new SimpleDateFormat(f);
+        try {
+            return sdf.parse(date);
+        } catch (ParseException e) {
+            return new Date();
+        }
+    }
+
+    /**
+     * 获取日期年份
+     *
+     * @param date
+     * @return
+     * @throws ParseException
+     */
+    public static int getYear(String date) throws ParseException {
+        date = date == null ? format(null, null) : date;
+        Calendar calendar = Calendar.getInstance();
+        calendar.setTime(YMD.parse(date));
+        return calendar.get(Calendar.YEAR);
+    }
+
+    /**
+     * 获取日期年份
+     *
+     * @param date
+     * @return
+     * @throws ParseException
+     */
+    public static int getYear(Date date) throws ParseException {
+        date = date == null ? parse(null, null) : date;
+        Calendar calendar = Calendar.getInstance();
+        calendar.setTime(date);
+        return calendar.get(Calendar.YEAR);
+    }
+
+    /**
+     * 获取日期月份
+     *
+     * @param date
+     * @return
+     * @throws ParseException
+     */
+    public static int getMonth(String date) throws ParseException {
+        date = date == null ? format(null, null) : date;
+        Calendar calendar = Calendar.getInstance();
+        calendar.setTime(YMD.parse(date));
+        return (calendar.get(Calendar.MONTH) + 1);
+    }
+
+    /**
+     * 获取日期月份
+     *
+     * @param date
+     * @return
+     * @throws ParseException
+     */
+    public static int getMonth(Date date) throws ParseException {
+        date = date == null ? parse(null, null) : date;
+        Calendar calendar = Calendar.getInstance();
+        calendar.setTime(date);
+        return (calendar.get(Calendar.MONTH) + 1);
+    }
+
+    /**
+     * 获取日期号
+     *
+     * @param date
+     * @return
+     * @throws ParseException
+     */
+    public static int getDay(String date) throws ParseException {
+        date = date == null ? format(null, null) : date;
+        Calendar calendar = Calendar.getInstance();
+        calendar.setTime(YMD.parse(date));
+        return calendar.get(Calendar.DAY_OF_MONTH);
+    }
+
+    /**
+     * 获取日期号
+     *
+     * @param date
+     * @return
+     * @throws ParseException
+     */
+    public static int getDay(Date date) throws ParseException {
+        date = date == null ? parse(null, null) : date;
+        Calendar calendar = Calendar.getInstance();
+        calendar.setTime(date);
+        return calendar.get(Calendar.DAY_OF_MONTH);
+    }
+
+    /**
+     * 获取日期前一年日期
+     *
+     * @param date
+     * @return
+     * @throws ParseException
+     */
+    public static String getLastYearDay(String date) throws ParseException {
+        date = date == null ? format(null, null) : date;
+        Calendar calendar = Calendar.getInstance();
+        try {
+            calendar.setTime(YMD.parse(date));
+        } catch (ParseException e) {
+            calendar.setTime(new Date());
+        }
+        calendar.set(Calendar.YEAR, calendar.get(Calendar.YEAR) - 1);
+        return YMD.format(calendar.getTime());
+
+    }
+
+    /**
+     * 获取月份起始日期
+     *
+     * @param date
+     * @return
+     * @throws ParseException
+     */
+    public static String getMinMonthDate(String date) {
+        date = date == null ? format(null, null) : date;
+        Calendar calendar = Calendar.getInstance();
+        try {
+            calendar.setTime(YMD.parse(date));
+        } catch (ParseException e) {
+            calendar.setTime(new Date());
+        }
+        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
+        return YMD.format(calendar.getTime());
+    }
+
+    /**
+     * 获取月份起始日期
+     *
+     * @param date
+     * @return
+     * @throws ParseException
+     */
+    public static String getMinMonthDate(Date date) {
+        date = date == null ? parse(null, null) : date;
+        Calendar calendar = Calendar.getInstance();
+        calendar.setTime(date);
+        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
+        return YMD.format(calendar.getTime());
+    }
+
+    /**
+     * 获取月份最后日期
+     *
+     * @param date
+     * @return
+     * @throws ParseException
+     */
+    public static String getMaxMonthDate(String date) {
+        date = date == null ? format(null, null) : date;
+        Calendar calendar = Calendar.getInstance();
+        try {
+            calendar.setTime(YMD.parse(date));
+        } catch (ParseException e) {
+            calendar.setTime(new Date());
+        }
+        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
+        return YMD.format(calendar.getTime());
+    }
+
+    /**
+     * 获取月份最后日期
+     *
+     * @param date
+     * @return
+     * @throws ParseException
+     */
+    public static String getMaxMonthDate(Date date) {
+        date = date == null ? parse(null, null) : date;
+        Calendar calendar = Calendar.getInstance();
+        calendar.setTime(date);
+        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
+        return YMD.format(calendar.getTime());
+    }
+
+    /**
+     * 当前时间
+     *
+     * @return <h3>String</h3>
+     */
+    public static String getCurrentDate() {
+        return format(new Date(), null);
+    }
+
+    /**
+     * 截取指定日期的年月
+     *
+     * @param date
+     * @return
+     */
+    public static Integer getYearmonth(Date date) {
+        if (date == null) {
+            date = new Date();
+        }
+        return Integer.parseInt(ym.format(date));
+    }
+
+    /**
+     * 截取指定日期的年月
+     *
+     * @param date
+     * @return
+     */
+    public static Integer getYearmonth(String date) {
+        return Integer.parseInt(ym.format(parse(date, null)));
+    }
+
+    /**
+     * 获取当前年月
+     *
+     * @return
+     */
+    public static Integer getYearmonth() {
+        return Integer.parseInt(ym.format(new Date()));
+    }
+
+    public static int compare(String date1, String date2) {
+        try {
+            Date dt1 = YMD.parse(date1);
+            Date dt2 = YMD.parse(date2);
+            if (dt1.getTime() > dt2.getTime()) {
+                return 1;
+            } else if (dt1.getTime() < dt2.getTime()) {
+                return -1;
+            } else {
+                return 0;
+            }
+        } catch (Exception exception) {
+            exception.printStackTrace();
+        }
+        return 0;
+    }
+
+    /**
+     * 获取特定日期是星期几
+     *
+     * @return
+     */
+    public static String getWeekDay(String DateStr) {
+        SimpleDateFormat formatYMD = new SimpleDateFormat("yyyy-MM-dd");// formatYMD表示的是yyyy-MM-dd格式
+        SimpleDateFormat formatD = new SimpleDateFormat("E");// "E"表示"day in week"
+        Date d = null;
+        String weekDay = "";
+        try {
+            d = formatYMD.parse(DateStr);// 将String 转换为符合格式的日期
+            weekDay = formatD.format(d);
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+        // System.out.println("日期:"+DateStr+" : "+weekDay);
+        return weekDay;
+    }
+
+    public static int getWeekDay1(String DateStr) {// 返回日期对应数字
+        SimpleDateFormat formatYMD = new SimpleDateFormat("yyyy-MM-dd");// formatYMD表示的是yyyy-MM-dd格式
+        Date d = null;
+        int weekDay = 0;
+        try {
+            d = formatYMD.parse(DateStr);// 将String 转换为符合格式的日期
+            Calendar calendar = Calendar.getInstance();
+            calendar.setTime(d);
+            weekDay = calendar.get(Calendar.DAY_OF_WEEK) - 1;
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+        return weekDay;
+    }
+
+    /**
+     * 获取特定时间段内有哪几日
+     *
+     * @return
+     */
+    public static List<Object> findDates(String start_time, String end_time) throws ParseException {
+        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
+        Date date1 = df.parse(start_time);
+        Date date2 = df.parse(end_time);
+        int s = (int) ((date2.getTime() - date1.getTime()) / (24 * 60 * 60 * 1000));
+        List<Object> objects = new ArrayList<Object>();
+        String value = "";
+        if (s + 1 > 0) {
+            for (int i = 0; i <= s; i++) {
+                long todayDate = date1.getTime() + (long) i * 24 * 60 * 60 * 1000;
+                Date tmDate = new Date(todayDate);
+                value = new SimpleDateFormat("yyyy-MM-dd").format(tmDate);
+                objects.add(value);
+            }
+        }
+        return objects;
+    }
+
+    /**
+     * 获取特定时间段内有多少天
+     *
+     * @return
+     */
+    public static int countDates(String start_time, String end_time) throws ParseException {
+        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
+        Date date1 = df.parse(start_time);
+        Date date2 = df.parse(end_time);
+        int s = (int) ((date2.getTime() - date1.getTime()) / (24 * 60 * 60 * 1000));
+        return s;
+    }
+
+    /**
+     * 日期转化成oracle格式to_date('2012-12-12', 'yyyy-MM-dd')
+     *
+     * @param format
+     *            format type,for example: 'yyyy-MM-dd', 'yyyy-MM-dd HH:mm:ss'
+     * @param date
+     *            date{type=java.util.Date}
+     */
+    public static String parseDateToOracleString(String format, Date date) {
+        if (format == null) {
+            format = YMD_STR;
+        }
+        SimpleDateFormat sdf = new SimpleDateFormat(format);
+        if (date == null) {
+            date = new Date();
+        }
+        if (YMD_HMS_STR.toUpperCase().equals(format.toUpperCase())) {
+            format = ORACLE_YMD_HMS_STR;
+        } else {
+            format = ORACLE_YMD_STR;
+        }
+        return "to_date('" + sdf.format(date) + "','" + format + "')";
+    }
+
+
+    /**
+     * 借贷合同月份处理
+     */
+    public static int getMonthSpace(Date date1, Date date2) throws ParseException {
+        int iMonth = 0;
+        int flag = 0;
+        try {
+            Calendar objCalendarDate1 = Calendar.getInstance();
+            objCalendarDate1.setTime(date1);
+
+            Calendar objCalendarDate2 = Calendar.getInstance();
+            objCalendarDate2.setTime(date2);
+
+            if (objCalendarDate2.equals(objCalendarDate1))
+                return 0;
+            if (objCalendarDate1.after(objCalendarDate2)) {
+                Calendar temp = objCalendarDate1;
+                objCalendarDate1 = objCalendarDate2;
+                objCalendarDate2 = temp;
+            }
+            if (objCalendarDate2.get(Calendar.DAY_OF_MONTH) > objCalendarDate1.get(Calendar.DAY_OF_MONTH))
+                flag = 1;
+
+            if (objCalendarDate2.get(Calendar.YEAR) > objCalendarDate1.get(Calendar.YEAR)) {
+                iMonth = ((objCalendarDate2.get(Calendar.YEAR) - objCalendarDate1.get(Calendar.YEAR)) * 12
+                        + objCalendarDate2.get(Calendar.MONTH) + flag)
+                        - objCalendarDate1.get(Calendar.MONTH);
+            } else {
+                iMonth = objCalendarDate2.get(Calendar.MONTH) + flag - objCalendarDate1.get(Calendar.MONTH);
+            }
+
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+        return iMonth;
+    }
+
+    /**
+     * 月份往前或往后n天
+     */
+    public static Date overDate(Date date, int num) {
+        date = date == null ? new Date() : date;
+        Calendar calendar = new GregorianCalendar();
+        calendar.setTime(date);
+        calendar.add(Calendar.DATE, num);
+        date = calendar.getTime();
+        return date;
+    }
+
+    /**
+     * 月份加减
+     *
+     * @param date
+     * @param increase
+     * @return
+     */
+    public static Integer addMonth(Date date, int increase) {
+        if (date == null)
+            date = new Date();
+        Calendar calendar = new GregorianCalendar();
+        calendar.setTime(date);
+        calendar.add(Calendar.MONTH, increase);
+        return getYearmonth(calendar.getTime());
+    }
+
+    /**
+     * 月份加减
+     *
+     * @param month
+     * @param increase
+     * @return
+     */
+    public static Integer addMonth(Integer month, int increase) {
+        Calendar calendar = new GregorianCalendar();
+        try {
+            calendar.setTime(ym.parse(String.valueOf(month)));
+        } catch (ParseException e) {
+            e.printStackTrace();
+        }
+        calendar.add(Calendar.MONTH, increase);
+        return getYearmonth(calendar.getTime());
+    }
+
+    /**
+     * 时间添加小时数
+     * */
+    public static Date addHours(Date date, float hours) {
+        Calendar ca = Calendar.getInstance();
+        ca.setTime(date);
+        ca.add(Calendar.MINUTE, (int) (hours * 60));
+        return ca.getTime();
+    }
+
+    /**
+     * 判断日期是否合法
+     */
+    public static boolean isValidDate(String dateString, String f) {
+        SimpleDateFormat sdf = new SimpleDateFormat(f);
+        sdf.setLenient(false);
+        try {
+            sdf.parse(dateString);
+        } catch (ParseException e) {
+            e.printStackTrace();
+            return false;
+        }
+        return true;
+    }
+
+}