자바 날짜 도움말 클래스(포맷 날짜,그 달 첫날 획득,올해 한 달의 마지막 날 획득,이번 분기 획득,윤년 여부,올 해 를 포함 한 최근 5 년 획득,지 정 된 문자열 이 날짜 형식 인지 판단 하 는 등 도움말 방법,사용 및 지적 을 환영 합 니 다)

32217 단어 자바도움말date
package com.cnksi.utils;

import com.cnksi.app.model.WechatWorkDate;
import org.apache.commons.lang.time.DateUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class DateUtil {
    private DateUtil() {
    }

    public static Date subDate(Date date, int minutes) {
        long times = date.getTime();
        times = times - minutes * 60 * 1000;
        return new Date(times);
    }

    /**
     * yyyy-MM-dd
     */
    static SimpleDateFormat sdfYmd = new SimpleDateFormat("yyyy-MM-dd");

    public static SimpleDateFormat timeFormater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    /**
     * yyyy-MM-dd HH:mm:ss
     */
    static SimpleDateFormat sdfYmdhms = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    /**
     * yyyy MM dd 
     */
    static SimpleDateFormat sdfNyr = new SimpleDateFormat("yyyy MM dd ");

    /**
     *         ,  :yyyy-MM-dd
     *
     * @param stringDate
     * @return
     */
    public static Date string2Date(String stringDate) {
        Date date = null;
        try {
            date = sdfYmd.parse(stringDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     *         ,  :yyyy-MM-dd HH:mm:ss
     *
     * @param stringDate
     * @return
     */
    public static Date toDateYmdHms(String stringDate) {
        Date date = null;
        try {
            date = sdfYmdhms.parse(stringDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     *           Date  
     *
     * @param operDate
     * @return
     */
    public static Date turnStr2Date(String operDate) {

        if (StringUtil.isEmpty(operDate)) {
            return null;
        }

        //   excel     2016-8-23,     42606,      1900 1 1    ,      2016-8-23    
        Date operDateTime;
        if (StringUtil.isNumeric(operDate)) {
            int operDateInt = 0;
            try {
                operDateInt = Integer.valueOf(operDate).intValue();
            } catch (NumberFormatException e) {
                e.printStackTrace();
            }
            Calendar c = new GregorianCalendar(1900, 0, -1);
            Date time = c.getTime();
            operDateTime = DateUtils.addDays(time, operDateInt); // 42605   1900 1 1    

        } else {
            try {
                operDateTime = StringUtil.notBlank(operDate) ? DateUtil.string2Date(operDate, "yyyy MM dd ") : null;
            } catch (Exception e) {
                operDateTime = StringUtil.notBlank(operDate) ? DateUtil.string2Date(operDate, "yyyy-MM-dd") : null;
            }
        }
        return operDateTime;
    }

    /**
     *        ,      
     *
     * @param stringDate
     * @param formatString
     * @return
     */
    public static Date string2Date(String stringDate, String formatString) {
        SimpleDateFormat dd = new SimpleDateFormat(formatString);
        Date date = null;
        try {
            date = dd.parse(stringDate);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        return date;
    }

    /**
     *     ,       
     *
     * @param date
     * @param formatStr     
     * @return
     */
    public static String date2String(Date date, String formatStr) {
        SimpleDateFormat sdf = new SimpleDateFormat(formatStr);
        return sdf.format(date);
    }

    /**
     * yyyy-MM-dd
     *
     * @param date
     * @return yyyy-MM-dd
     */
    public static String date2String(Date date) {
        return sdfYmd.format(date);
    }

    /**
     * @param date
     * @return yyyy-MM-dd_HH-mm-ss-sss
     */
    public static String date2Stringhms(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss-sss");
        return sdf.format(date);
    }

    /**
     * @param date
     * @return yyyy-MM-dd HH:mm:ss
     */
    public static String date2Stringhms2(Date date) {
        return sdfYmdhms.format(date);
    }

    /**
     *    :yyyy MM dd 
     *
     * @param date
     * @return
     */
    public static String date2String2(Date date) {
        return sdfNyr.format(date);
    }

    //        
    public static String getFirstDayOfCurrentMonth() {
        return getFirstDayOfMonth(null);
    }

    /**
     *         
     *
     * @param month   ,      
     * @return
     */
    public static String getFirstDayOfMonth(Integer month) {
        String str = "";

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        Calendar lastDate = Calendar.getInstance();

        lastDate.set(Calendar.DATE, 1);//       1 

        if (month != null) {

            lastDate.set(Calendar.MONTH, month - 1); // Calendar.MONTH 0  
        }

        str = sdf.format(lastDate.getTime());

        return str;
    }

    /**
     *            
     *
     * @param month   ,      
     * @return
     */
    public static String getLastDayOfMonth(Integer month) {

        String str = "";

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        Calendar lastDate = Calendar.getInstance();

        lastDate.set(Calendar.DATE, 1);//       1 

        if (month != null) {

            lastDate.set(Calendar.MONTH, month - 1); // Calendar.MONTH 0  
        }

        lastDate.add(Calendar.MONTH, 1);//     ,     1 

        lastDate.add(Calendar.DATE, -1);//     ,        

        str = sdf.format(lastDate.getTime());

        return str;

    }

    /**
     *            
     *
     * @param year   
     * @param month  
     * @return
     */
    public static String getLastDayStrOfMonth(int year, int month) {
        Calendar cal = Calendar.getInstance();
        //    
        cal.set(Calendar.YEAR, year);
        //    
        cal.set(Calendar.MONTH, month - 1);
        //        
        int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        //            
        cal.set(Calendar.DAY_OF_MONTH, lastDay);
        //     
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(cal.getTime());
    }

    //         ,     
    public static String getLastOfCurrentMonth() {
        return getLastDayOfMonth(null);
    }

    //           
    public static String getCurrentYearFirst() {
        int yearPlus = DateUtil.getYearPlus();
        GregorianCalendar currentDate = new GregorianCalendar();
        currentDate.add(GregorianCalendar.DATE, yearPlus);
        Date yearDay = currentDate.getTime();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");//            
        String preYearDay = dateFormat.format(yearDay);
        return preYearDay;
    }

    //             *
    public static String getCurrentYearEnd() {
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy");//            
        String years = dateFormat.format(date);
        return years + "-12-31";
    }

    public static int getCurrentYear() {
        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
        return year;
    }

    public static int getCalendarCurrentMonth() {
        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.MONTH);
        return year;
    }

    private static int getYearPlus() {
        Calendar cd = Calendar.getInstance();
        int yearOfNumber = cd.get(Calendar.DAY_OF_YEAR);//             
        cd.set(Calendar.DAY_OF_YEAR, 1);//           
        cd.roll(Calendar.DAY_OF_YEAR, -1);//        。
        int MaxYear = cd.get(Calendar.DAY_OF_YEAR);
        if (yearOfNumber == 1) {
            return -MaxYear;
        } else {
            return 1 - yearOfNumber;
        }
    }

    //      
    public static String getThisSeasonTime(int month) {
        int array[][] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {10, 11, 12}};
        int season = 1;
        if (month >= 1 && month <= 3) {
            season = 1;
        }
        if (month >= 4 && month <= 6) {
            season = 2;
        }
        if (month >= 7 && month <= 9) {
            season = 3;
        }
        if (month >= 10 && month <= 12) {
            season = 4;
        }
        int start_month = array[season - 1][0];
        int end_month = array[season - 1][2];

        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy");//            
        String years = dateFormat.format(date);
        int years_value = Integer.parseInt(years);

        int start_days = 1;// years+"-"+String.valueOf(start_month)+"-1";//getLastDayOfMonth(years_value,start_month);
        int end_days = DateUtil.getLastDayOfMonth(years_value, end_month);
        String seasonDate = years_value + "-" + start_month + "-" + start_days + ";" + years_value + "-" + end_month + "-" + end_days;
        return seasonDate;

    }

    /**
     *            
     *
     * @param year   
     * @param month  
     * @return     
     */
    public static int getLastDayOfMonth(int year, int month) {
        if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
            return 31;
        }
        if (month == 4 || month == 6 || month == 9 || month == 11) {
            return 30;
        }
        if (month == 2) {
            if (DateUtil.isLeapYear(year)) {
                return 29;
            } else {
                return 28;
            }
        }
        return 0;
    }

    /**
     *     
     *
     * @param year  
     * @return
     */
    public static boolean isLeapYear(int year) {
        return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
    }

    /**
     *     , yyyy-MM-dd
     *
     * @return
     */
    public static String getFirstDayOfWeek() {
        Calendar cal = Calendar.getInstance();
        //             (      )
        cal.setFirstDayOfWeek(Calendar.MONDAY);
        cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        return sdfYmd.format(cal.getTime());
    }

    public static String getLastDayOfWeek() {
        Calendar cal = Calendar.getInstance();
        //             (      )
        cal.setFirstDayOfWeek(Calendar.MONDAY);
        cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
        return sdfYmd.format(cal.getTime());
    }

    //            5 
    public static List getLastFiveYears() {
        List list = new ArrayList();
        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
        list.add(year - 2);
        list.add(year - 1);
        list.add(year);
        list.add(year + 1);
        list.add(year + 2);
        return list;

    }

    //            5 
    public static List getMonthOfYear() {
        List list = new ArrayList();
        for (int i = 1; i <= 12; i++) {
            list.add(i);
        }
        return list;

    }

    public static String getCurrentMonth() {
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("MM");//            
        String month = dateFormat.format(date);
        return month;
    }

    /**
     *                
     *
     * @param str
     * @return
     */
    public static boolean isValidDate(String str) {
        if (str == null || str.isEmpty()) {
            return false;
        }
        str = str.trim();
        boolean convertSuccess = true;
        //           /    /    ,  yyyy/MM/dd     ;
        SimpleDateFormat formatOne = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat formatTwo = new SimpleDateFormat("yyyy/MM/dd");
        try {
            //   lenient false.   SimpleDateFormat          ,  2007/02/29    ,    2007/03/01
            formatOne.setLenient(false);
            formatOne.parse(str);
        } catch (ParseException e) {
            try {
                formatTwo.setLenient(false);
                formatTwo.parse(str);
            } catch (ParseException pe) {
                // e.printStackTrace();
                //   throw java.text.ParseException  NullPointerException,       
                convertSuccess = false;
            }
        }
        return convertSuccess;
    }

    /**
     *            
     *
     * @param date
     * @return
     */
    public static boolean timeOut(String date) {
        if (date.length() >= 10) {
            date = date.substring(0, 10);
        }
        date = date.trim();
        if (date.contains("/")) {
            date = date.replaceAll("/", "-");
        }
        java.sql.Date logDate = java.sql.Date.valueOf(date);
        //   Calendar      
        Date now;
        Calendar c = Calendar.getInstance();
        now = new Date(c.get(Calendar.YEAR), c.get(Calendar.MONTH) + 1, c.get(Calendar.DAY_OF_MONTH));
        return logDate.after(now);
    }

    /**
     *                   
     *
     * @param dBegin   
     * @param dEnd     
     * @return     
     */
    public static List findDates(Date dBegin, Date dEnd) {
        SimpleDateFormat formatDate = new SimpleDateFormat("yyyy-MM-dd");
        List lDate = new ArrayList<>();
        lDate.add(formatDate.format(dBegin.getTime()));
        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);
            lDate.add(DateUtil.date2String(calBegin.getTime()));
        }
        return lDate;
    }

    /**
     *        
     *
     * @return   2018-05-14
     */
    public static String getYesterdayRange() {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        calendar.add(Calendar.DATE, -1);
        return dateFormat.format(calendar.getTime());
    }

    /**
     *           
     *
     * @param specifiedDay      
     * @return           
     */
    public static String getSpecifiedDayBefore(String specifiedDay) {
        Calendar c = Calendar.getInstance();
        Date date = null;
        try {
            date = new SimpleDateFormat("yyyy-MM-dd").parse(specifiedDay);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        c.setTime(date);
        int day = c.get(Calendar.DATE);
        c.set(Calendar.DATE, day - 1);
        String dayBefore = new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
        return dayBefore;
    }

    //     
    public static void main(String[] args) {
    }
}

좋은 웹페이지 즐겨찾기