java常用日期工具类

近来比较空闲,想着整理一波代码啦。记录下日常开发较为常见的工具类,用来处理日期转换。

DateUtils.java

package com.example.demo.utils;

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

import com.sun.org.apache.xerces.internal.impl.xpath.regex.ParseException;

/**
 *
 * 项目名称:testClass        
 * 类名称:DateUtils    
 * 类描述:
 * 创建人:yexuan
 * 创建时间:2017-12-4 下午04:20:02
 */
public class DateUtils {


    /**
     * 字符串格式:yyyy-MM-dd HH:mm:ss.sss
     */
    public static final String formatter_yyyyMMddHHmmsssss = "yyyy-MM-dd HH:mm:ss.sss";
    /**
     * 字符串格式:yyyy-MM-dd HH:mm:ss
     */
    public static final String formatter_yyyyMMddHHmmss = "yyyy-MM-dd HH:mm:ss";
    /**
     * 字符串格式:yyyy-MM-dd HH:mm
     */
    public static final String formatter_yyyyMMddHHmm = "yyyy-MM-dd HH:mm";
    /**
     * 字符串格式:yyyy-MM-dd
     */
    public static final String formatter_yyyyMMdd = "yyyy-MM-dd";
    /**
     * 字符串格式:yyyy-MM
     */
    public static final String formatter_yyyyMM = "yyyy-MM";
    /**
     * 字符串格式:yyyy
     */
    public static final String formatter_yyyy = "yyyy";
    /**
     * 字符串格式:MM-dd
     */
    public static final String formatter_MMdd = "MM-dd";
    /**
     * 字符串格式:HH:mm:ss
     */
    public static final String formatter_HHmmss = "HH:mm:ss";
    /**
     * 字符串格式:HH:mm
     */
    public static final String formatter_HHmm = "HH:mm";
    /**
     * 字符串格式:返回星期X(一、二、三、四、五、六、日)
     */
    public static final String formatter_EEEE = "EEEE";


//    public static void main(String args[])
//    {
//		System.out.println(dateToString(new Date(), formatter_yyyyMMddHHmmss));
//		System.out.println(stringtoDate("2017-10-01 12:00", formatter_yyyyMMddHHmm));
//		System.out.println(getCurrentDate(formatter_yyyyMMddHHmmss));
//		System.out.println(getFirstDayOfMonth(new Date(), formatter_yyyyMMddHHmmss, 0));
//		System.out.println(getFirstDayOfMonth(new Date(), formatter_yyyyMMddHHmmss, -12));
//		System.out.println(getFirstDayOfMonth(new Date(), formatter_yyyyMMddHHmmss, 1));
//		System.out.println(getLastDayOfMonth(new Date(), formatter_yyyyMMddHHmmss, 0));
//		System.out.println(getLastDayOfMonth(new Date(), formatter_yyyyMMddHHmmss, -12));
//		System.out.println(getLastDayOfMonth(new Date(), formatter_yyyyMMddHHmmss, 1));
//		System.out.println(getLastDayOfMonth(new Date(), formatter_yyyyMMddHHmmss,2));
//		System.out.println(getHourByDate(new Date()));
//		System.out.println(getDayByDate(new Date()));
//		System.out.println(getMonthByDate(new Date()));
//		System.out.println(getYearByDate(new Date()));
//    }

    /**
     * @Description: 将日期转换为指定格式的字符串
     * @param @param date
     * @param @param format
     * @param @return   
     * @return String
     * @author yexuan
     * @date 2017-12-4  下午03:58:17
     */
    public static String dateToString(java.util.Date date, String format){
        String result = "";
        SimpleDateFormat formater = new SimpleDateFormat(format);
        try {
            result = formater.format(date);
        } catch (Exception e) {
        }
        return result;
    }


    /**
     *
     * @Description: 将指定格式的字符串转换为日期
     * @param dateStr
     * @param format
     * @return java.util.Date
     * @author yexuan
     * @date 2017-12-4  下午04:18:06
     */
    public static java.util.Date stringtoDate(String dateStr, String format) {
        SimpleDateFormat formater = new SimpleDateFormat(format);
        try {
            formater.setLenient(false);
            return formater.parse(dateStr);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     *
     * @Description: 获取当前时间的指定格式的字符串
     * @param format
     * @return String
     * @author yexuan
     * @date 2017-12-4  下午05:22:04
     */
    public static String getCurrentDate(String format) {
        return dateToString(new Date(), format);
    }



    /**
     *
     * @Description: 获取给定时间第几个月第一天的指定格式字符串
     * @param date
     * @param format
     * @param month
     * @return String
     * @author yexuan
     * @date 2017-12-5  上午08:18:27
     */
    public static String getFirstDayOfMonth(Date date, String format, int month) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.MONTH, month);
        cal.set(Calendar.DATE, 1);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        return dateToString(cal.getTime(), format);
    }

    /**
     *
     * @Description: 获取给定时间第几个月最后一天的指定格式字符串
     * @param date
     * @param format
     * @return String
     * @author yexuan
     * @date 2017-12-5  上午08:29:48
     */
    public static String getLastDayOfMonth(Date date, String format,int month) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.DATE, 1);
        cal.add(Calendar.MONTH, 1);
        cal.add(Calendar.DATE, -1);
        cal.add(Calendar.MONTH, month);
        cal.set(Calendar.HOUR_OF_DAY, 23);
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.SECOND, 59);
        return dateToString(cal.getTime(), format);
    }


    /**
     *
     * @Description: 求指定日期的小时数(0到23)
     * @param date
     * @return int
     * @author yexuan
     * @date 2017-12-5  上午08:51:03
     */
    public static int getHourByDate(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.HOUR_OF_DAY);
    }

    /**
     *
     * @Description: 求指定日期是一个月中第几天(1到31)
     * @param date
     * @return int
     * @author yexuan
     * @date 2017-12-5  上午08:46:32
     */
    public static int getDayByDate(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.DATE);
    }

    /**
     *
     * @Description: 求指定日期是一年中第几月(1到12)
     * @param date
     * @return int
     * @author yexuan
     * @date 2017-12-5  上午08:48:39
     */
    public static int getMonthByDate(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.MONTH) + 1;
    }

    /**
     *
     * @Description: 求指定日期的年
     * @param date
     * @return int
     * @author yexuan
     * @date 2017-12-5  上午08:49:50
     */
    public static int getYearByDate(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.YEAR);
    }

    /**
     *
     * @Description: 返回两个指定字符串格式的日期相差天数
     * @param startDate
     * @param endDate
     * @param format
     * @return int
     * @author yexuan
     * @date 2017-12-5  上午09:30:58
     */
    public static int daysBetween(String startDate,String endDate,String format){
        Calendar cal = Calendar.getInstance();
        cal.setTime(stringtoDate(startDate, format));
        long time1 = cal.getTimeInMillis();
        cal.setTime(stringtoDate(endDate, format));
        long time2 = cal.getTimeInMillis();
        long betweenDays=Math.abs((time2-time1)/(1000*3600*24));
        return Integer.parseInt(String.valueOf(betweenDays));
    }



    /**
     *
     * @Description: 获取两个时间之间的小时差(时间格式必须是yyyy-MM-dd HH yyyy-MM-dd HH:mm yyyy-MM-dd HH:mm:ss yyyy-MM-dd HH:mm:ss.sss)
     * @param startDate
     * @param endDate
     * @param format
     * @return float
     * @author yexuan
     * @date 2017-12-5  上午10:06:35
     */
    public static float hoursBetween(String startDate,String endDate,String format){
        Calendar cal = Calendar.getInstance();
        cal.setTime(stringtoDate(startDate, format));
        long time1 = cal.getTimeInMillis();
        cal.setTime(stringtoDate(endDate, format));
        long time2 = cal.getTimeInMillis();
        float between_hours=(float) ((time2-time1)/(1000*3600.0));
        return between_hours;
    }


    /**
     *
     * @Description: 获取两个时间之间的分钟差(时间格式必须是yyyy-MM-dd HH:mm yyyy-MM-dd HH:mm:ss yyyy-MM-dd HH:mm:ss.sss)
     * @param startDate
     * @param endDate
     * @param format
     * @return float
     * @author yexuan
     * @date 2017-12-5  上午10:04:11
     */
    public static float minutesBetweenFloat(String startDate,String endDate,String format){
        Calendar cal = Calendar.getInstance();
        cal.setTime(stringtoDate(startDate, format));
        long time1 = cal.getTimeInMillis();
        cal.setTime(stringtoDate(endDate, format));
        long time2 = cal.getTimeInMillis();
        float betweenMinutes=(float) ((time2-time1)/(1000*60.0));
        return betweenMinutes;
    }

    /**
     *
     * @Description: 获取两个时间之间的分钟差(时间格式必须是yyyy-MM-dd HH:mm yyyy-MM-dd HH:mm:ss yyyy-MM-dd HH:mm:ss.sss)
     * @param startDate
     * @param endDate
     * @param format
     * @return int
     * @author yexuan
     * @date 2017-12-5  上午10:02:59
     */
    public static int minutesBetweenInt(String startDate,String endDate,String format){
        Calendar cal = Calendar.getInstance();
        cal.setTime(stringtoDate(startDate, format));
        long time1 = cal.getTimeInMillis();
        cal.setTime(stringtoDate(endDate, format));
        long time2 = cal.getTimeInMillis();
        long betweenMinutes=Math.abs((time2-time1)/(1000*60));
        return Integer.parseInt(String.valueOf(betweenMinutes));
    }





    /**
     *
     * @Description: 求指定字符串格式的天数或月份加减
     * @param dateStr
     * @param format  常用格式:yyyy-MM-dd HH:mm:ss&&yyyy-MM-dd
     * @param counts
     * @param type  month or day
     * @return String
     * @author yexuan
     * @date 2017-12-5  上午10:14:25
     */
    public static String getNewDateStr(String dateStr,String format,int counts,String type){
        Calendar cal = Calendar.getInstance();
        cal.setTime(stringtoDate(dateStr, format));
        if(type!=null&&type.equals("day"))
        {
            cal.add(Calendar.DAY_OF_YEAR, counts);
        }
        if(type!=null&&type.equals("month"))
        {
            cal.add(Calendar.MONTH, counts);
        }
        String  newDateStr= dateToString(cal.getTime(), format);
        return  newDateStr;
    }




    /**
     *
     * @Description: 比较字符串是否和格式完全相同
     * @param str
     * @param format
     * @return boolean
     * @author yexuan
     * @date 2017-12-4  下午05:07:09
     */
    public static boolean isSameFormat(String str,String format) {
        boolean convertSuccess=true;
        SimpleDateFormat formater = new SimpleDateFormat(format);
        try {
            formater.setLenient(false);
            try {
                formater.parse(str);
            } catch (java.text.ParseException e) {
                convertSuccess=false;
            }
        } catch (ParseException e) {
            convertSuccess=false;
        }
        return convertSuccess;
    }

    /**
     *
     * @Description: 比较字符串是否和格式类似相同
     * @param str
     * @param format
     * @return boolean
     * @author yexuan
     * @date 2017-12-4  下午05:07:09
     */
    public static boolean isLikeFormat(String str,String format) {
        boolean convertSuccess=true;
        SimpleDateFormat formater = new SimpleDateFormat(format);
        try {
            formater.setLenient(true);
            try {
                formater.parse(str);
            } catch (java.text.ParseException e) {
                convertSuccess=false;
            }
        } catch (ParseException e) {
            convertSuccess=false;
        }
        return convertSuccess;
    }

}

此工具类包含转换为指定格式的日期,获取前一天后一天相隔天数的方法。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值