String字符串和时间的转换以及时间的计算

String字符串和时间的转换以及时间的计算

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

public class DateTimeUtils {
    
    

    public static String getCurrentTimeMS(){
    
    
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
        String currentTime = format.format(Calendar.getInstance().getTime());
        return currentTime;
    }

    public static long compareTime(String beginTime,String endTime){
    
    
        SimpleDateFormat dfs = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
        long between = 0;
        try {
    
    
            Date begin = dfs.parse(beginTime);
            Date end = dfs.parse(endTime);
            between = (end.getTime() - begin.getTime());// 得到两者的毫秒数
        } catch (Exception ex) {
    
    
            ex.printStackTrace();
        }
        return between;
    }

    /**
     * 日期转字符串只保留年月日使用-分隔
     * yyyy-MM-dd
     */
    public static String dayFormat(Date date) {
    
    
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(date);
    }

    /**
     * 日期转字符串只保留年月日不分隔
     * yyyyMMdd
     */
    public static String dayFormatSimple(Date date) {
    
    
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        return sdf.format(date);
    }

    /**
     * 日期转字符串yyyy-MM-dd HH:mm:ss格式
     * @return
     */
    public static String secondFormat(Date date) {
    
    
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(date);
    }

    /**
     * 日期转字符串yyyyMMddHHmmssSSS格式
     */
    public static String msecFormat(Date date) {
    
    
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        return sdf.format(date);
    }

    /**
     * 日期转字符串yyyyMMddmmHHss格式
     */
    public static String msecSimpleFormat(Date date) {
    
    
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        return sdf.format(date);
    }

    /**
     * 日期转字符串HHmmss格式
     */
    public static String hmsFormat(Date date) {
    
    
        SimpleDateFormat sdf = new SimpleDateFormat("HHmmss");
        return sdf.format(date);
    }

    /**
     * yyyy-MM-dd字符串转日期
     */
    public static Date strToDate(String text) {
    
    
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
    
    
            date = sdf.parse(text);
        } catch (ParseException e) {
    
    
            e.printStackTrace();
        }
        return date;
    }

    public static String strToDateToStr(String text,String pattern){
    
    
        SimpleDateFormat sdfPattern = new SimpleDateFormat(pattern);
        Date date = null;
        try {
    
    
            date = sdfPattern.parse(text);
        } catch (ParseException e) {
    
    
            e.printStackTrace();
        }


        String result = sdfPattern.format(date);
        return result;
    }


    /**
     * yyyy-MM-dd字符串转日期
     */
    public static java.sql.Date strToSqlDate(String text) {
    
    
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
    
    
            date = sdf.parse(text);
        } catch (ParseException e) {
    
    
            e.printStackTrace();
        }
        java.sql.Date date1 = new java.sql.Date(date.getTime());

        return date1;
    }

    /**
     * yyyy-MM-dd HH:mm:ss字符串转日期
     */
    public static Date strToDates(String text) {
    
    
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
        Date date = null;
        try {
    
    
            date = sdf.parse(text);
        } catch (ParseException e) {
    
    
            e.printStackTrace();
        }
        return date;
    }

    /**
     * yyyyMMddHHmmss字符串转日期
     */
    public static Date yyyyMMddHHmmssToDate(String text) {
    
    
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        Date date = null;
        try {
    
    
            date = sdf.parse(text);
        } catch (ParseException e) {
    
    
            e.printStackTrace();
        }
        return date;
    }

    /**
     * yyyy-MM-dd HH:mm:ss字符串转日期
     */
    public static java.sql.Timestamp strToDatesSql(String text) {
    
    
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        java.sql.Timestamp date1 = null;
        try {
    
    
            date = sdf.parse(text);
            date1 = new java.sql.Timestamp(date.getTime());
        } catch (ParseException e) {
    
    
            e.printStackTrace();
        }
        return date1;
    }

    /**
     * 计算当前时间n天后的时间
     */
    public static Date getDateAfter(Date d, int day) {
    
    
        Calendar now = Calendar.getInstance();
        now.setTime(d);
        now.set(Calendar.DATE, now.get(Calendar.DATE) + day);
        return now.getTime();
    }

    /**
     * 计算当前时间n天前的时间
     */
    public static Date getDateBefore(Date d, int day) {
    
    
        Calendar now = Calendar.getInstance();
        now.setTime(d);
        now.set(Calendar.DATE, now.get(Calendar.DATE) - day);
        return now.getTime();
    }

    /**
     * 计算两个日期相差的天数
     */
    public static int subtractDate(Date endTime,Date startTime){
    
    
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(startTime);

        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(endTime);
        int day1= cal1.get(Calendar.DAY_OF_YEAR);
        int day2 = cal2.get(Calendar.DAY_OF_YEAR);

        int year1 = cal1.get(Calendar.YEAR);
        int year2 = cal2.get(Calendar.YEAR);
        if(year1 != year2)   //同一年
        {
    
    
            int timeDistance = 0 ;
            for(int i = year1 ; i < year2 ; i ++)
            {
    
    
                if(i%4==0 && i%100!=0 || i%400==0)    //闰年
                {
    
    
                    timeDistance += 366;
                }
                else    //不是闰年
                {
    
    
                    timeDistance += 365;
                }
            }

            return timeDistance + (day2-day1) ;
        }
        else    //不同年
        {
    
    
            System.out.println("判断day2 - day1 : " + (day2-day1));
            return day2-day1;
        }

    }

    /**
     * 获得指定日期的前一天   接收字符串,返回字符串
     * @param specifiedDay
     * @return
     * @throws Exception
     */
    public static String getSpecifiedDayBefore(String specifiedDay){
    
    
        //SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        Date date=null;
        try {
    
    
            date = new SimpleDateFormat("yy-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;
    }
    /**
     * 获得指定日期的后一天   接收字符串,返回字符串
     * @param specifiedDay
     * @return
     */
    public static String getSpecifiedDayAfter(String specifiedDay){
    
    
        Calendar c = Calendar.getInstance();
        Date date=null;
        try {
    
    
            date = new SimpleDateFormat("yy-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 dayAfter=new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
        return dayAfter;
    }


    public static String dateToString(Date date, String pattern) {
    
    
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
        return simpleDateFormat.format(date);
    }

    public static Date strToDate(String dateStr, String pattern) {
    
    
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
        Date date = null;
        try {
    
    
            date = simpleDateFormat.parse(dateStr);
        } catch (ParseException e) {
    
    
            e.printStackTrace();
        }
        return date;
    }

    /**
     * 计算时间相差的毫秒数
     *
     * @param beginTime
     * @param endTime
     * @return
     */
    public static long compareTime(Date beginTime, Date endTime) {
    
    
        long between = 0;
        try {
    
    
            between = (endTime.getTime() - beginTime.getTime());// 得到两者的毫秒数
        } catch (Exception ex) {
    
    
            ex.printStackTrace();
        }
        return between;
    }

    /**
     * 求指定时间相差n天的日期
     * @param date
     * @return
     */
    public static String theDayBefore(Date date,int n,String pattern){
    
    
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        Calendar calendar = Calendar.getInstance();//得到一个Calendar的实例
        calendar.setTime(date); //设置时间为当前时间
        calendar.add(Calendar.DATE, n);
        Date lastDay = calendar.getTime(); //结果
        return sdf.format(lastDay);
    }

    /**
     * 求指定时间相差n月的日期
     * @param date
     * @return
     */
    public static String theMonthBefore(Date date,int n,String pattern){
    
    
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        Calendar calendar = Calendar.getInstance();//得到一个Calendar的实例
        calendar.setTime(date);
        calendar.add(Calendar.MONTH, n);
        Date lastDay = calendar.getTime(); //结果
        return sdf.format(lastDay);
    }

    /**
     * 求指定时间相差n年日期
     * @param date
     * @return
     */
    public static String theYearBefore(Date date, int n, String pattern){
    
    
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        Calendar calendar = Calendar.getInstance();//得到一个Calendar的实例
        calendar.setTime(date);
        calendar.add(Calendar.YEAR, n);
        Date lastDay = calendar.getTime(); //结果
        return sdf.format(lastDay);
    }


    /**
     * 把数据转换成 long
     * @param date
     * @return
     */
    public synchronized static long aLong(Date date){
    
    
        long beginMillisecond =date.getTime();
        return beginMillisecond;
    }


    /**
     * 获取本月第一天
     * @return
     */
    public static Date getMonthFirstDay(){
    
    
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.DAY_OF_MONTH,1);
        calendar.add(Calendar.MONTH,0);
        return calendar.getTime();
    }

}

猜你喜欢

转载自blog.csdn.net/weixin_43861283/article/details/105565770