Util-工具类

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.apache.commons.lang3.StringUtils;
/**
 * 日期工具类
 */
public class CalendarUtil {

    public static final String sf = "yyyyMMdd";

    /**
     * 将Date 转换为String类型
     * @param date
     * @return
     * @throws ParseException
     */
    public static String getDateToString(Date date, String sdf) throws ParseException {
        SimpleDateFormat ft = new SimpleDateFormat(StringUtils.isBlank(sdf) ? sf : sdf);
        return ft.format(date);
    }

    /**
     * 将String 时间转成Date
     * @param time
     * @param sdf
     * @return
     * @throws ParseException
     */
    public static Date getStringToDate(String time,String sdf) throws ParseException{
        SimpleDateFormat ft = new SimpleDateFormat(StringUtils.isBlank(sdf) ? sf : sdf);
         return ft.parse(time);
    }
    /**
     * 将当前时间转化成String类型
     * @param sdf
     * @return
     */
    public static String getCurrentTime(String sdf) {
        SimpleDateFormat ft = new SimpleDateFormat(StringUtils.isBlank(sdf) ? sf : sdf);
        Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(System.currentTimeMillis());
        String t = ft.format(cal.getTime());
        return t;
    }

    /**
     * 获取当年的第一天
     * @param year
     * @return
     */
    public static Date getCurrentYearFirstDay() {
        Calendar currCal = Calendar.getInstance();
        int currentYear = currCal.get(Calendar.YEAR);
        return getYearFirstDay(currentYear);
    }

    /**
     * 获取某年第一天日期
     * @param year 年份
     * @return Date
     */
    public static Date getYearFirstDay(int year) {
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(Calendar.YEAR, year);
        Date currYearFirst = calendar.getTime();
        return currYearFirst;
    }
    /**
     * 当前时间点
     * @return
     */
    public static  int getCurrentClock(){
            Calendar calendar = Calendar.getInstance();
            int hour = calendar.get(Calendar.HOUR_OF_DAY);
            return hour;
        }
    public static void main(String[] args) {
        System.out.println(CalendarUtil.getCurrentTime("yyyy"));
    }
}

public enum DataTypeEnum {
    INT("1", "整型"), NUM("2", "数值类型"), PERCENT("3", "百分比类型"), STRING("4", "字符类型");

    private String code;
    private String msg;

    private DataTypeEnum(String code, String msg) {
        this.code = code;
        this.msg = msg;
    }
/**
 * 数据类型

*/
    public String getCode() {
        return code;
    }

    public String getMsg() {
        return msg;
    }

}

 

package com.paic.commcc.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.quartz.CronExpression;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.support.CronSequenceGenerator;

/**
 */
public class DateUtil {

    public static final String sf = "yyyyMMdd";

    public static final String FORMAT_1 = "yyyyMMddHHmmss";

    public static final String FORMAT_2 = "YYYY-MM-DD";

    public static final String FORMAT_3 = "yyyy年MM月dd日";

    public static final String FORMAT_4 = "yyyyMMdd";

    public static final String FORMAT_5 = "yyyy-MM-dd";

    public static final String FORMAT_6 = "yyyy-MM-dd HH:mm:ss";

    public static final String FORMAT_7 = "HH:mm:ss";
    public static final String FORMAT_8 = "YYYY/MM/DD";

    public static final String FORMAT_9 = "yyyy/MM/dd HH:mm:ss";

    public static final String FORMAT_10 = "yyyyMM";
    
    public static final String FORMAT_11 = "yyyyMMdd HH:mm:ss";
    
    public static final String FORMAT_12 = "ss mm HH dd MM ? yyyy";

    public static final int CALENDAR_DEFAULT_YEAR = 1990;

    public static final int CALENDAR_DEFAULT_MONTH = 1;

    public static final int CALENDAR_DEFAULT_DAY = 1;

    private static final Logger logger = LoggerFactory.getLogger(DateUtil.class);

    private DateUtil() {
    }

    /**
     * 格式化指定的时间
     *
     * @return String
     */
    public static String formatDate(Date date, String format) {
        return new SimpleDateFormat(format).format(date);
    }

    /**
     * 将format格式的字符串时间格式化为yyyyMMdd的
     *
     * @param date
     * @param format
     * @return
     * @throws java.text.ParseException
     */
    public static String formatDate(String date, String format) throws ParseException {
        return formatDate(parseDate(date, format), FORMAT_4);
    }

    /**
     * 判断时间格式是否是"yyyyMM"或者"yyyyMMdd"
     *
     * @param date
     *            字符串类型时间
     * @param format
     *            时间格式
     * @return
     */
    public static boolean isDate(String date, String format) {
        if(StringUtils.isBlank(date) || StringUtils.isBlank(format)) {
            return false;
        }
        
        if(format.length() != date.length()) {
            return false;
        }
        
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            Date d = sdf.parse(date); // 把字符串转化为日期(可以过滤掉奇怪格式的日期格式)
            String newDate = sdf.format(d);// 日期转化为字符

            /*
             * 1,把字符串的日期转化为Date类型的日期。 2,把Date类型日期转化为字符串类型日期。 3,如果两者相等,说明日期格式符合要求
             */
            if (null != newDate && newDate.equals(date)) {
                return true;
            } else {
                return false;
            }
        } catch (ParseException e) {
            // 出现异常,说明时间格式有误
            logger.error("时间格式错误:{}", e.getMessage(), e);
            return false;
        }

    }
    
    /**
     * 格式化当前时间
     *
     * @return String
     */
    public static String formatCurrentDate(String format) {
        return formatDate(new Date(), format);
    }

    /**
     * 获取当前时间戳
     *
     * @return String
     */
    public static String generateSystemTimestamp() {
        Date currentDate = new Date();
        return String.valueOf(currentDate.getTime());
    }

    /**
     * 获取当前时间戳
     *
     * @return String
     */
    public static String generateCurrentTimeMillis() {
        return String.valueOf(System.currentTimeMillis());
    }

    /**
     * 判断firstTime是否在secondTime之后,忽略日期部分
     *
     * @param firstTime
     * @param secondTime
     * @return
     */
    public static boolean isTimeAfter(Date firstTime, Date secondTime) {

        Calendar calendar1 = Calendar.getInstance();
        calendar1.setTime(firstTime);
        calendar1.set(CALENDAR_DEFAULT_YEAR, CALENDAR_DEFAULT_MONTH, CALENDAR_DEFAULT_DAY);
        Calendar calendar2 = Calendar.getInstance();
        calendar2.setTime(secondTime);
        calendar2.set(CALENDAR_DEFAULT_YEAR, CALENDAR_DEFAULT_MONTH, CALENDAR_DEFAULT_DAY);
        return calendar1.after(calendar2);
    }

    /**
     * 判断firstTime是否在secondTime之前,忽略日期部分
     *
     * @param firstTime
     * @param secondTime
     * @return
     */
    public static boolean isTimeBefore(Date firstTime, Date secondTime) {
        return isTimeAfter(secondTime, firstTime);
    }

    /**
     * 判断firstTime是否在secondTime之后,不忽略日期部分
     *
     * @param firstTime
     * @param secondTime
     * @return
     */
    public static boolean isTimeAfterIncludeDate(Date firstTime, Date secondTime) {
        Calendar calendar1 = Calendar.getInstance();
        calendar1.setTime(firstTime);
        Calendar calendar2 = Calendar.getInstance();
        calendar2.setTime(secondTime);
        return calendar1.after(calendar2);
    }

    /**
     * 判断firstTime是否在secondTime之前,不忽略日期部分
     *
     * @param firstTime
     * @param secondTime
     * @return
     */
    public static boolean isTimeBeforeIncludeDate(Date firstTime, Date secondTime) {
        return isTimeAfterIncludeDate(secondTime, firstTime);
    }

    /**
     * 获取date添加minutes分钟后的时间
     *
     * @param date
     * @param minutes
     * @return
     */
    public static Date addMinutes(Date date, int minutes) {
        return DateUtils.addMinutes(date, minutes);
    }

    /**
     * 格式化字符串为时间类型
     *
     * @param date
     * @param format
     * @return
     */
    public static Date parseDate(String date, String format) throws ParseException {
        return new SimpleDateFormat(format).parse(date);
    }

    /**
     * 格式化字符串为时间类型(严格模式)
     *
     * @param date
     * @param format
     * @return
     */
    public static Date parseDateStrict(String date, String format) throws ParseException {
        SimpleDateFormat dateFormat = new SimpleDateFormat(format);
        dateFormat.setLenient(false);
        return dateFormat.parse(date);
    }

    /**
     * 判断参数的格式是否为“yyyyMMdd”格式的合法日期字符串
     *
     */
    public static boolean isValidDate(String str) {
        try {
            if (str != null && !"".equals(str)) {
                if (str.length() == 8) {
                    // 闰年标志
                    boolean isLeapYear = false;
                    String year = str.substring(0, 4);
                    String month = str.substring(4, 6);
                    String day = str.substring(6, 8);
                    int vYear = Integer.parseInt(year);
                    // 判断年份是否合法
                    if (vYear < 1900 || vYear > 2200) {
                        return false;
                    }
                    // 判断是否为闰年
                    if (vYear % 4 == 0 && vYear % 100 != 0 || vYear % 400 == 0) {
                        isLeapYear = true;
                    }
                    // 判断月份
                    // 1.判断月份
                    if (month.startsWith("0")) {
                        String units4Month = month.substring(1, 2);
                        int vUnits4Month = Integer.parseInt(units4Month);
                        if (vUnits4Month == 0) {
                            return false;
                        }
                        if (vUnits4Month == 2) {
                            // 获取2月的天数
                            int vDays4February = Integer.parseInt(day);
                            if (isLeapYear) {
                                if (vDays4February > 29)
                                    return false;
                            } else {
                                if (vDays4February > 28)
                                    return false;
                            }
                        }
                    } else {
                        // 2.判断非0打头的月份是否合法
                        int vMonth = Integer.parseInt(month);
                        if (vMonth != 10 && vMonth != 11 && vMonth != 12) {
                            return false;
                        }
                    }
                    // 判断日期
                    // 1.判断日期
                    if (day.startsWith("0")) {
                        String units4Day = day.substring(1, 2);
                        int vUnits4Day = Integer.parseInt(units4Day);
                        if (vUnits4Day == 0) {
                            return false;
                        }
                    } else {
                        // 2.判断非0打头的日期是否合法
                        int vDay = Integer.parseInt(day);
                        if (vDay < 10 || vDay > 31) {
                            return false;
                        }
                    }
                    return true;
                } else {
                    return false;
                }
            } else {
                return false;
            }
        } catch (Exception e) {
            logger.error("异常:{}", e.getMessage(), e);
            return false;
        }
    }

    /**
     * 判断参数的格式是否为“yyyyMMdd”格式的合法日期字符串
     *
     */
    public static boolean isValidMonth(String str) {
        try {
            if (str != null && !"".equals(str)) {
                if (str.length() == 6) {
                    // 闰年标志
                    String year = str.substring(0, 4);
                    String month = str.substring(4, 6);
                    int vYear = Integer.parseInt(year);
                    // 判断年份是否合法
                    if (vYear < 1900 || vYear > 2200) {
                        return false;
                    }
                    // 判断月份
                    // 1.判断月份
                    if (month.startsWith("0")) {
                        String units4Month = month.substring(1, 2);
                        int vUnits4Month = Integer.parseInt(units4Month);
                        if (vUnits4Month == 0) {
                            return false;
                        }
                    } else {
                        // 2.判断非0打头的月份是否合法
                        int vMonth = Integer.parseInt(month);
                        if (vMonth != 10 && vMonth != 11 && vMonth != 12) {
                            return false;
                        }
                    }
                    return true;
                } else {
                    return false;
                }
            } else {
                return false;
            }
        } catch (Exception e) {
            logger.error("异常:{}", e.getMessage(), e);
            return false;
        }
    }

    /**
     * 将Date 转换为String类型
     *
     * @param date
     * @return
     * @throws ParseException
     */
    public static String getDate2String(Date date, String sdf) {
        SimpleDateFormat ft = new SimpleDateFormat(StringUtils.isBlank(sdf) ? sf : sdf);
        return ft.format(date);
    }

    public static Date toDate(String str) {
        if (str == null) {
            return null;
        }
        try {
            str = StringUtils.trim(str);

            if (str.indexOf('/') != -1) {
                return new SimpleDateFormat("yyyy/MM/dd").parse(str);
            }
            return new SimpleDateFormat("yyyy-MM-dd").parse(str);
        } catch (Exception e) {
            // throw new RuntimeException(StringUtils.join("[", str, "] ",
            // "DateUtil toDate parse exception"), e);
            logger.error(StringUtils.join("[", str, "] ", "DateUtil toDate parse exception"), e.getMessage(), e);
            return null;
        }
    }

    /**
     * 时间戳转换成日期格式字符串
     *
     * @param seconds
     *            精确到秒的字符串
     * @param format
     * @return
     */
    public static String timeStamp2Date(String seconds, String format) {
        if (seconds == null || seconds.isEmpty() || "null".equals(seconds)) {
            return "";
        }
        SimpleDateFormat sdf;
        if (format == null || format.isEmpty()) {
            sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        } else {
            sdf = new SimpleDateFormat(format);
        }
        return sdf.format(new Date(Long.parseLong(seconds + "000")));
    }

    /**
     * 日期格式字符串转换成时间戳
     *
     * @param date_str
     *            字符串日期
     * @param format
     *            如:yyyy-MM-dd HH:mm:ss
     * @return
     */
    public static String date2TimeStamp(String date_str, String format) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            return String.valueOf(sdf.parse(date_str).getTime() / 1000);
        } catch (Exception e) {
            logger.error("字符串转时间戳异常:{}", e.getMessage(), e);
        }
        return "";
    }

    /**
     * quartz框架时间转换为timstamp
     *
     * @param date_str
     *            字符串日期
     * @param format
     *            如:yyyy-MM-dd HH:mm:ss
     * @return
     */
    public static String date2timeStampForQuartz(String date_str, String format) {
        try {
            String str = date2TimeStamp(date_str, format);
            return str + "000";
        } catch (Exception e) {
            logger.error("框架时间转换异常:{}", e.getMessage(), e);
        }
        return "";
    }

    /**
     * 取得当前时间戳(精确到秒)
     *
     * @return
     */
    public static String timeStamp() {
        long time = System.currentTimeMillis();
        String t = String.valueOf(time / 1000);
        return t;
    }

    /**
     * @param d1
     * @param d2
     * @return 时间比较, d1 > d2 返回1,d1 < d2 返回-1, d1 = d2 返回0
     */
    public static int compareDate(Date d1, Date d2) {
        if (d1.getTime() > d2.getTime()) {
            return 1;
        } else if (d1.getTime() < d2.getTime()) {
            return -1;
        } else {
            return 0;
        }
    }

    /**
     * @param m1
     * @param m2
     * @return 月份比较,返回m1-m2
     */
    public static int compareMonth(String m1, String m2) {
        if (!isValidMonth(m1) || !isValidMonth(m2)) {
            // 不是合法月份,返回999999
            return 999999;
        } else {
            try {
                SimpleDateFormat format = new SimpleDateFormat(FORMAT_10);
                Calendar c1 = Calendar.getInstance();
                Calendar c2 = Calendar.getInstance();
                c1.setTime(format.parse(m1));
                c2.setTime(format.parse(m2));
                int month = c1.get(Calendar.MONTH) - c2.get(Calendar.MONTH);
                int yearMonth = (c1.get(Calendar.YEAR) - c2.get(Calendar.YEAR)) * 12;
                return month + yearMonth;
            } catch (ParseException e) {
                logger.error("解析异常:{}", e.getMessage(), e);
                return 999999;
            }
        }
    }

    /**
     * @param afterTime
     * @param cronExpression
     * @return 根据cron表达式获取某时间之后的下次执行时间
     */
    public static Date getTimeAfter(Date afterTime, String cronExpression) {
        CronExpression cronEx;
        try {
            cronEx = new CronExpression(cronExpression);
            return cronEx.getTimeAfter(afterTime);
        } catch (ParseException e) {
            logger.error("根据cron表达式获取时间异常:{}", e);
            return null;
        }
    }
    
    /**获取某月的自然天数,2月28天,3月31天
     * @taskId <br>
     * @param yyyyMM 格式的 validMonth
     * @return <br>
     */
    public static  int countDaysOfMonth(String validMonth){
        int year  = Integer.parseInt(validMonth.substring(0,4));
        int month = Integer.parseInt(validMonth.substring(4));
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, month-1);
        return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
    }
    
    /**
     * Description:获取cron表达式的下次时间 <br>
     *  
     * @author LIYOUHUI784<br>
     * @taskId <br>
     * @param cronExpression
     * @return <br>
     */
    public static Date getNextFireTime(String cron) {
        if(StringUtils.isBlank(cron)) {
            return null;
        }
        CronSequenceGenerator cronSequenceGenerator = new CronSequenceGenerator(cron);
        return cronSequenceGenerator.next(new Date());
    }
    
    /**
     * Description:获取当前月某天某时某分某秒的日历 <br>
     *  
     * @author liyouhui784<br>
     * @taskId <br>
     * @param day
     * @param hour
     * @param minute
     * @param second
     * @return <br>
     */
    public static Calendar getCurrentMonthSomeTime(int day, int hour, int minute, int second) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.DAY_OF_MONTH, day);
        calendar.set(Calendar.HOUR_OF_DAY, hour);
        calendar.set(Calendar.MINUTE, minute);
        calendar.set(Calendar.SECOND, second);
        return calendar;
    }
    
}

package com.paic.commcc.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.apache.commons.lang3.StringUtils;

public class DateUtils {
    public static final String sf = "yyyyMMdd";
    public static final String time_sf = "yyyyMMddHHmmssSSS";
    public static final String DATE_FORMAT = "yyyy-MM-dd";
    public static final String TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";

    public static final String after = "AFTER";
    public static final String before = "BEFORE";

    /**
     * 将linux时间戳 转换为String类型 date
     *
     * @param date
     * @return
     */
    public static Date getLinuxTimeToDate(String dateTime) {
        if (dateTime == null) {
            return null;
        }
        Date date = new Date(Long.parseLong(dateTime));
        return date;
    }

    /**
     * 将Date 转换为String类型
     *
     * @param date
     * @return
     * @throws ParseException
     */
    public static String getDateToString(Date date, String sdf) throws ParseException {
        SimpleDateFormat ft = new SimpleDateFormat(StringUtils.isBlank(sdf) ? sf : sdf);
        return ft.format(date);
    }

    /**
     * 将String 时间转成Date
     *
     * @param time
     * @param sdf
     * @return
     * @throws ParseException
     */
    public static Date getStringToDate(String time, String sdf) throws ParseException {
        SimpleDateFormat ft = new SimpleDateFormat(StringUtils.isBlank(sdf) ? sf : sdf);
        return ft.parse(time);
    }

    /**
     * 获取当天使用指定的格式
     */
    public static String today(String sdf) {
        SimpleDateFormat ft = new SimpleDateFormat(StringUtils.isBlank(sdf) ? sf : sdf);
        return ft.format(new Date());
    }

    /**
     * 获取当天使用指定的格式
     */
    public static String today() {
        return today(sf);
    }

    /**
     * 将当前时间转化成String类型
     *
     * @param sdf
     * @return
     */
    public static String getCurrentTime(String sdf) {
        SimpleDateFormat ft = new SimpleDateFormat(StringUtils.isBlank(sdf) ? sf : sdf);
        Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(System.currentTimeMillis());
        String t = ft.format(cal.getTime());
        return t;
    }

    /**
     * 获取当年的第一天
     *
     * @param year
     * @return
     */
    public static Date getCurrentYearFirstDay() {
        Calendar currCal = Calendar.getInstance();
        int currentYear = currCal.get(Calendar.YEAR);
        return getYearFirstDay(currentYear);
    }

    /**
     * 获取某年第一天日期
     *
     * @param year
     *            年份
     * @return Date
     */
    public static Date getYearFirstDay(int year) {
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(Calendar.YEAR, year);
        Date currYearFirst = calendar.getTime();
        return currYearFirst;
    }

    /**
     * 当前时间点
     *
     * @return
     */
    public static int getCurrentClock() {
        Calendar calendar = Calendar.getInstance();
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        return hour;
    }

    /**
     * 时间字符串转时间戳
     *
     * @throws ParseException
     */

    public static String dateToStamp(String s) throws ParseException {
        return dateToStamp(s, "yyyy-MM-dd HH:mm:ss");
    }

    public static String dateToStamp(String s, String sdf) throws ParseException {
        String str;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(sdf);
        Date date = simpleDateFormat.parse(s);
        long ts = date.getTime();
        str = String.valueOf(ts);
        return str;
    }

    /**
     * 当前时间转时间戳
     *
     * @throws ParseException
     */
    public static String dateToStamp() {
        Date date = new Date();
        long ts = date.getTime();
        String str = String.valueOf(ts);
        return str;
    }

    /**
     * 时间戳转时间
     */
    public static String stampToDate(String s) {
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long ts = Long.parseLong(s);
        Date date = new Date(ts);
        res = simpleDateFormat.format(date);
        return res;

    }

    /**
     * 任意正确时间转换成为想要的格式字符串
     */
    public static String getWantDate(String dateString, String reg) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        SimpleDateFormat sb = new SimpleDateFormat(reg);

        String s = dateString.replace("-", "").replace(":", "").replace("/", "").replace(" ", "");
        String a = s.substring(0, 8);
        Date date;
        String ss = null;
        try {
            date = sdf.parse(a);
            ss = sb.format(date);
        } catch (ParseException e) {

            e.printStackTrace();
        }
        return ss;

    }

    /**
     * 任意正确时间转换成为想要的日期
     *
     * @throws ParseException
     */
    public static Date getWantDates(String dateString, String reg) throws ParseException {

        SimpleDateFormat sb = new SimpleDateFormat("yyyyMMdd");

        String str = dateString.replace("-", "").replace(":", "").replace("/", "").replace(" ", "");
        String a = str.substring(0, 8);
        Date date = sb.parse(a);

        return date;

    }

    /**
     * 将String 时间转成Date
     *
     * @param time
     * @param sdf
     * @return
     */
    public static Date getString2Date(String time, String sdf) {
        SimpleDateFormat ft = new SimpleDateFormat(StringUtils.isBlank(sdf) ? sf : sdf);
        try {
            return ft.parse(time);
        } catch (ParseException e) {
            // logger.error("字符串转日期出错", e);
        }

        return null;
    }

    /**
     * 将Date 转换为String类型
     *
     * @param date
     * @return
     * @throws ParseException
     */
    public static String getDate2String(Date date, String sdf) {
        SimpleDateFormat ft = new SimpleDateFormat(StringUtils.isBlank(sdf) ? sf : sdf);
        return ft.format(date);
    }

    /**
     * 获取指定日期的前一天
     *
     * @param date
     * @return
     * @throws ParseException
     */
    public static Date getYesterDay(Date date, String sdf) throws ParseException {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, -1);
        SimpleDateFormat ft = new SimpleDateFormat(sdf);
        return ft.parse(ft.format(calendar.getTime()));
    }

    /**
     * 日期计算 目标日期加target天,target为正数表示加,为负数表示减
     *
     * @param date
     * @return Date
     * @throws ParseException
     */
    public static Date dateCalculate(Date date, String sdf, Integer target) throws ParseException {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, target);
        SimpleDateFormat ft = new SimpleDateFormat(sdf);
        return ft.parse(ft.format(calendar.getTime()));
    }

    /**
     * 将常用时间格式字符串转化为时间 - / . 以及没有分隔都可以
     */
    public static Date transformDateStyle(String dateString) {
        Date date = null;
        if (dateString != null) {
            try {
                if (dateString.indexOf('-') > 0) {
                    date = new SimpleDateFormat("yyyy-MM-dd").parse(dateString);
                } else if (dateString.indexOf('/') > 0) {
                    date = new SimpleDateFormat("yyyy/MM/dd").parse(dateString);
                } else if (dateString.indexOf('.') > 0) {
                    date = new SimpleDateFormat("yyyy.MM.dd").parse(dateString);
                } else {
                    date = new SimpleDateFormat("yyyyMMdd").parse(dateString);
                }
            } catch (ParseException e) {
                date = new Date();
            }
        }
        return date;
    }

    /**
     * 判断输入的时间是否大于或小于当前日期
     *
     * @param date
     *            需要判断的日期
     * @param str
     *            AFTER 验证在当前之前或等于当前日期,BEFORE验证在当前日期之后或等于当前日期
     * @return 1==验证通过 0==验证失败
     * @throws ParseException
     */
    public static int isDateBeforeOrAfter(Date date, String str) throws ParseException {
        Date sdate = new Date();
        int dint;
        boolean falg;
        if (after.equals(str)) {
            falg = date.before(sdate);
            if (falg) {
                return 1;
            }
        }
        if (before.equals(str)) {
            dint = date.compareTo(getYesterDay(sdate, sf));
            if (dint == 1) {
                return 1;
            }
        }
        return 0;

    }

    /**
     * 获得当月1号零时零分零秒
     *
     * @return
     */
    public static Date initDateByMonth() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        return calendar.getTime();
    }

}

 

posted on 2019-01-14 15:29  风有衡  阅读(2263)  评论(0编辑  收藏  举报