202.常用java時間工具類

1. 效果

 

 

2. 工具類

package ipi.common.utils;

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

public class TimeUtils {
    /**
     * 1.1
     * 將用戶傳遞的時間類型轉換爲字符串類型
     * @Title: getStringDate
     * @Description: 
     * @param date
     * @return
     * @return String
     * @throws 
       @date 2018年6月4日 上午9:17:26
     */
    public static String getStringDate(Date date) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH🇲🇲ss");
        String currentTime = formatter.format(date);
        return currentTime;
    }
    /**
     * 1.2將標準字符串轉化類型的時間 轉化爲Date類型
     * @Title: getStringToDate
     * @Description: 
     * @param time
     * @return
     * @throws ParseException
     * @return Date
     * @throws 
       @date 2018年8月8日 下午10:57:37
     */
    public static Date getStringToDate(String time) throws ParseException {
         SimpleDateFormat sdf =   new SimpleDateFormat( "yyyy-MM-dd HH🇲🇲ss" ); 
         Date date = sdf.parse(time); 
        return date;
    }
    
    /**
     *2.1 等到年月日 
     * @Title: getStringShortDate
     * @Description: 
     * @param date
     * @return
     * @return String
     * @throws 
       @date 2018年7月6日 下午1:27:15
     */
    public static String getStringShortDate(Date date) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String currentTime = formatter.format(date);
        return currentTime;
    }
    /**
     * 2.2  得到 年月
     * @Title: getStringYearMouthDate
     * @Description: 
     * @param date
     * @return
     * @return String
     * @throws 
       @date 2018年8月12日 上午9:58:49
     */
    public static String getStringYearMouthDate(Date date) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM");
        String currentTime = formatter.format(date);
        return currentTime;
    }
    /**
     * 3.計算兩個日期相差的年數
     * @Title: getYearDisparity
     * @Description: 
     * @param beginTime
     * @param endTime
     * @return
     * @throws Exception
     * @return String
     * @throws 
       @date 2018年7月6日 下午2:20:40
     */
    public static String getYearDisparity(String beginTime,String endTime) throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        Calendar bef = Calendar.getInstance();
        Calendar aft = Calendar.getInstance();
        bef.setTime(sdf.parse(beginTime));
        aft.setTime(sdf.parse(endTime));
        int year = (aft.get(Calendar.YEAR) - bef.get(Calendar.YEAR));
        System.out.println(Math.abs(year)); 
        return year+"";
    }
    /**
     * 4.計算兩個日期相差的月數
     * @Title: getMonthsDisparity
     * @Description: 
     * @param beginTime
     * @param endTime
     * @return
     * @throws Exception
     * @return String
     * @throws 
       @date 2018年7月6日 下午2:30:54
     */
    public static String getMonthsDisparity(String beginTime,String endTime) throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        String str1 = "2011-02";
        String str2 = "2010-01";
        Calendar bef = Calendar.getInstance();
        Calendar aft = Calendar.getInstance();
        bef.setTime(sdf.parse(str1));
        aft.setTime(sdf.parse(str2));
        int result = aft.get(Calendar.MONTH) - bef.get(Calendar.MONTH);
        int month = (aft.get(Calendar.YEAR) - bef.get(Calendar.YEAR)) * 12;
        String months = ( result+month)+"";
        return  months;
    }
    
    
    
    /**
     * 5.計算兩個日期相差的天數
     * @Title: getMonthsDisparity
     * @Description: 
     * @param beginTime
     * @param endTime
     * @return
     * @throws Exception
     * @return String
     * @throws 
       @date 2018年7月6日 下午2:21:41
     */
    public static String getdaysDisparity(String beginTime,String endTime) throws Exception {
         SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            Date date2 = format.parse(endTime);
            Date date = format.parse(beginTime);
         int days = (int) ((date2.getTime() - date.getTime()) / (1000*3600*24));
            return days+"";
    }
    /**
     * 6.  拿到現在時間加上(減去)  d天后的時間  d是正數加 負數減去
     * @Title: getStringAfterOperationDay
     * @Description: 
     * @param d
     * @return
     * @return String
     * @throws 
       @date 2018年7月30日 上午11:18:39
     */
    
    public static String getStringAfterOperationDay(Integer d) {
         Format f = new SimpleDateFormat("yyyy-MM-dd");
            Calendar c = Calendar.getInstance();
            c.add(Calendar.DAY_OF_MONTH, d);// 昨天-1天
            Date yesterday = c.getTime();
        String yesterdayBegin =    f.format(yesterday).toString();
        return yesterdayBegin;
    }
    /***
     * 7.1 獲取距離今天 天 的0點時間   比如2018-07-30 00:00:000 
     * @Title: getStringAfterOperationBeginDay
     * @Description: 
     * @param d
     * @return
     * @return String
     * @throws 
       @date 2018年7月30日 下午6:30:04
     */
    public static String getStringAfterOperationBeginDay(Integer d) {
        Format f = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        c.add(Calendar.DAY_OF_MONTH, d);// 正數 加
        Date yesterday = c.getTime();
        String beginTime =    f.format(yesterday).toString()+" 00:00:00";
        return beginTime;
    }
    /**
     * 7.2  獲取距離今天 天 的0點時間   比如2018-07-30 23:59:000 
     * @Title: getStringAfterOperationEndDay
     * @Description: 
     * @param d
     * @return
     * @return String
     * @throws 
       @date 2018年7月30日 下午6:33:50
     */
    public static String getStringAfterOperationEndDay(Integer d) {
        Format f = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        c.add(Calendar.DAY_OF_MONTH, d);// 負數減
        Date yesterday = c.getTime();
        String endTime =    f.format(yesterday).toString()+" 23:59:59";
        return endTime;
    }
    
}
 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章