java date tool class DateUtils

package com.ck.labmsys.web.util;

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

/**
* 日期工具类
*
* @author CK
*
*/
public class DateUtils implements Serializable {
    /**
*
*/
    private static final long serialVersionUID = -7968262747262488161L;
    public final static String YYYYMMDDHHMMSS = "yyyy-MM-dd HH:mm:ss";
    public final static String YYYYMMDDHHMM = "yyyy-MM-dd HH:mm";
    public final static String YYYYMMDDHHMMSS_CONTINUS = "yyyyMMddHHmmss";
    public static final String YYYYMMDD = "yyyy-MM-dd";
    public static final String YYYY = "yyyy";
    public static final String YYYYMM = "yyyy-MM";
    public static final String YYMM = "yyMM";

    public static final String HHmm = "HH:mm";

    /**
     * Convert the date to a string, the converted format is: yyyy-MM-dd HH:mm:ss, if the date is null, return ""
     *
     * @param date
     * @return
     */
    public static String convertToYYYYMMDDHHMMSS(Date date) {
        if (date == null) {
            return "";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(YYYYMMDDHHMMSS);
        return sdf.format(date);
    }

    /**
     * Convert the date to a string, the converted format is: yyyyMMddHHmmss, if the date is null, return ""
     *
     * @param date
     * @return
     */
    public static String convertToYYYYMMDDHHMMSSContinus(Date date) {
        if (date = = null) {
            return "";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(YYYYMMDDHHMMSS_CONTINUS);
        return sdf.format(date);
    }

    /**
     * Convert the date to a string, the converted format is: yyyy-MM-dd, Return "" if date is null
     *
     * @param date
     * @return
     */
    public static String convertToYYYYMMDD(Date date) {
        if (date == null) {
            return "";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(YYYYMMDD);
        return sdf.format(date);
    }

    /**
     * Convert the date to a string, the converted format is: yyMM, if the date is null, return ""
     *
     * @param date
     * @return
     */
    public static String convertToYYMM(Date date) {
        if (date == null) {
            return "";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(YYMM);
        return sdf.format(date);
    }

    /**
     * Convert the current date to a string, the converted format is: yyyy-MM-dd, if the date is null, return ""
     *
     * @return
     */
    public static String getCurrentDateYYYYMMDD() {
        return convertToYYYYMMDD(new Date());
    }

    /**
     * Convert the current date to a string, the converted format is: yyyy-MM-dd HH:mm:ss, if the date If it is null, return ""
     *
     * @return
     */
    public static String getCurrentDateYYYYMMDDHHMMSS() {
        return convertToYYYYMMDDHHMMSS(new Date());
    }

    /**
     * Convert the current date to a string, the converted format is: yyyyMMddHHmmss, if If the date is null, return ""
     *
     * @return
     */
    public static String getCurrentDateYYYYMMDDHHMMSSContinus() {
        return convertToYYYYMMDDHHMMSSContinus(new Date());
    }

    /**
     * Convert the current date to a string, the converted format is: yyMM, if the date is null, return ""
     *
     * @return
     */
    public static String getCurrentDateYYMM() {
        return convertToYYMM(new Date());
    }

    // public static void main(String[] args){
    // String currentDateYYMM = getCurrentDateYYMM();
    // System.out.println(currentDateYYMM);
    // }

    /**
     * Convert the date to a string, the converted format is : HH:mm, if the date is null then return "" HH:mm format eg. 10:20
     *
     * @param date
     * @return
     */
    public static String convertToHHmm(Date date) {
        if (date == null) {
            return "";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(HHmm);
        return sdf.format(date);
    }

    /**
     * Convert a string (format: yyyy-MM-dd HH:mm:ss) to a date, if the format is incorrect, return null
     *
     * @param original
     * @return
     * @throws ParseException
     */
    public static Date convertToDateyyyyMMddHHmmss(String original) throws ParseException {
        Date result = null;
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(YYYYMMDDHHMMSS);
            result = sdf.parse(original);

        } catch (ParseException e) {
            return null;
        }

        return result;

    }

    /**
     * Convert string (format: yyyy-MM-dd HH:mm) to date, if the format is incorrect, return null
     *
     * @param original
     * @return
     * @throws ParseException
     */
    public static Date convertToDateyyyyMMddHHmm( String original) throws ParseException {
        Date result = null;

        try {
            SimpleDateFormat sdf = new SimpleDateFormat(YYYYMMDDHHMM);
            result = sdf.parse(original);

        } catch (ParseException e) {
            return null;
        }

        return result;

    }

    /**
     * Convert a string (format: yyyy-MM-dd) to a date, if the format is incorrect, return null
     *
     * @param original
     * @return
     * @throws ParseException
     */
    public static Date convertToDateyyyyMMdd(String original) throws ParseException {

        Date result = null;

        try {
            SimpleDateFormat sdf = new SimpleDateFormat(YYYYMMDD);
            result = sdf.parse(original);

        } catch (ParseException e) {
            return null;
        }

        return result;

    }

    /**
     * Convert the string (format: yyyy-MM) to a date, if the format is incorrect, return null
     *
     * @param original
     * @return
     * @throws ParseException
     */
    public static Date convertToDateyyyyMM(String original) throws ParseException {

        Date result = null;

        try {
            SimpleDateFormat sdf = new SimpleDateFormat(YYYYMM);
            result = sdf.parse(original);

        } catch (ParseException e) {
            return null;
        }

        return result;

    }

    /**
     * Convert string (format: yyyy) to date, if the format is incorrect, return null
     *
     * @param original
     * @return
     * @throws ParseException
     */
    public static Date convertToDateyyyy(String original) throws ParseException {

        Date result = null;

        try {
            SimpleDateFormat sdf = new SimpleDateFormat(YYYY);
            result = sdf.parse(original);

        } catch (ParseException e) {
            return null;
        }

        return result;

    }

    /**
     *
     * 得到本月的第一天
     */

    public static Date getCurrentMonthFirstDay() {

        Calendar calendar = Calendar.getInstance();

        calendar.set(Calendar.DAY_OF_MONTH, calendar

        .getActualMinimum(Calendar.DAY_OF_MONTH));

        // calendar.set( Calendar.DATE, 1);
        return calendar.getTime();

    }

    /**
     *
     * Get the last day of the month
     */

    public static Date getCurrentMonthLastDay() {

        Calendar calendar = Calendar.getInstance();

        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum

        (Calendar.DAY_OF_MONTH));

        return calendar.getTime();

    }

    /**
     *
     * Get the last day of the specified month
     */

    public static Date getMonthLastDay(Long ms) {

        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(ms);

        // calendar.set(Calendar.DAY_OF_MONTH, 1 );

        calendar.add(Calendar.MONTH, 1);
        calendar.add(Calendar.DAY_OF_MONTH, -1);
        return calendar.getTime();

    }

    /**
     *
     * 得到指定月的第一天
     */

    public static Date getMonthFirstDay(Long ms) {

        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(ms);

        calendar.set(Calendar.DAY_OF_MONTH, 1);

        // calendar.add(Calendar.MONTH, 1);
        // calendar.add(Calendar.DAY_OF_MONTH, -1);
        return calendar.getTime();

    }

    /**
     * 获得明天日期
     *
     * @param currentday
     * @return
     */
    public static Date getNextDay(Date currentday) {

        // 明天日期
        Date nextday = new Date();
        long nexttime = (currentday.getTime() / 1000) + 60 * 60 * 24;
        nextday.setTime(nexttime * 1000);
        return nextday;
    }

    /***
     * 时间戳转换为Date
     *
     * @param date
     * @return
     */
    public static Date getLongDate(Long date) {
        Date dd = new Date(date);
        return dd;
    }

    public static void main(String[] args) throws ParseException {
        System.out.print(getMonthLastDay(new Date().getTime()));
    }

}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326384183&siteId=291194637