天天看點

日期格式化和計算工具

package com.input4hua.xxx.utils;

import com.google.common.collect.Maps;
import org.joda.time.DateTime;
import org.joda.time.LocalDate;
import org.joda.time.LocalTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.Locale;
import java.util.Map;

/**
* 時間格式轉換工具類
*
* Created by jiahua.hu on 2017/12/18.
*/
public class MyTimeFormatterUtils {

    public static final String TIME_FORMAT_PATTERN_DEFAULT = "yyyy-MM-dd HH:mm:ss";
    public static final String TIME_FORMAT_PATTERN_YEARMONTHDAY = "yyyy-MM-dd";
    public static final String TIME_FORMAT_PATTERN_HOURMINSEC = "HH:mm:ss";
    public static final String TIME_FORMAT_PATTERN_SHORTDATE_TIME = "yyyy/MM/dd HH:mm:ss";
    public static final String TIME_FORMAT_PATTERN_SHORTDATE = "yyyy/MM/dd";
    public static final String TIME_FORMAT_PATTERN_LONGDATE = "yyyy/MM/dd EE";
    private static final Map<Locale,String> TIME_FORMAT_PATTERN_LOCAL = Maps.newHashMap();

    static {
        TIME_FORMAT_PATTERN_LOCAL.put(Locale.CHINA,"yyyy年MM月dd日 EE");
    }

    /**
     * 字元轉換時間,預設轉換器
     * @param dateStr
     * @return
     */
    public static Date String2Date(String dateStr){
        return String2Date(dateStr,TIME_FORMAT_PATTERN_DEFAULT);
    }

    /**
     * 字元轉換時間,指定自定義轉換器
     * @param dateStr
     * @param formatPattern
     * @return
     */
    public static Date String2Date(String dateStr, String formatPattern){
        DateTimeFormatter format = DateTimeFormat.forPattern(formatPattern);
        DateTime dateTime = DateTime.parse(dateStr, format);
        return dateTime.toDate();
    }

    /**
     * 時間轉換字元,預設轉換器
     * @param date
     * @return
     */
    public static String Date2String(Date date){
        return Date2String(date,TIME_FORMAT_PATTERN_DEFAULT);
    }

    /**
     * 時間轉換字元,指定自定義轉換器
     * @param date
     * @param formatPattern
     * @return
     */
    public static String Date2String(Date date, String formatPattern){
        return new DateTime(date).toString(formatPattern);
    }

    /**
     * 目前時間
     * @return 時間格式
     */
    public static Date nowDate(){
        return DateTime.now().toDate();
    }

    /**
     * 目前年月日
     * @return 時間格式
     */
    public static Date nowYearMonthDayDate(){
        return LocalDate
                .now()
                .toDate();
    }


    /**
     * 目前時間
     * @return 字元
     */
    public static String nowStr(){
        return DateTime
                .now()
                .toString(TIME_FORMAT_PATTERN_DEFAULT);
    }

    /**
     * 目前年月日
     * @return 字元
     */
    public static String nowYearMonthDayStr(){
        return LocalDate
                .now()
                .toString(TIME_FORMAT_PATTERN_YEARMONTHDAY);
    }

    /**
     * 目前時分秒
     * @return 字元
     */
    public static String nowHourMinutesSecondsStr(){
        return LocalTime
                .now()
                .toString(TIME_FORMAT_PATTERN_HOURMINSEC);
    }

    /**
     * 目前短日期
     * @return 字元
     */
    public static String nowShortDate(){
        return formatShortDate(LocalDate.now().toDate());
    }

    /**
     * 指定時間短日期
     * @param date
     * @return 字元
     */
    public static String formatShortDate(Date date){
        return new LocalDate(date).toString(TIME_FORMAT_PATTERN_SHORTDATE);
    }

    /**
     * 目前長日期
     * @return 字元
     */
    public static String nowLongDate(){
        return formatLongDate(LocalDate.now().toDate());
    }

    /**
     * 指定時間長日期
     * @param date
     * @return 字元
     */
    public static String formatLongDate(Date date){
        return new LocalDate(date).toString(TIME_FORMAT_PATTERN_LONGDATE);
    }

    /**
     * 傳回本地時間
     * @param date
     * @return
     */
    public static String stringLocalDate(Date date){
        return Date2String(date,TIME_FORMAT_PATTERN_LOCAL.get(Locale.getDefault()));
    }

}      
package com.input4hua.xxx.utils;

import org.joda.time.*;

import java.util.Date;
import java.util.Locale;

/**
*
* 時間計算工具類
*
* Created by jiahua.hu on 2017/12/18.
*/
public class MyTimeCalcUtils {

    /**
     * 明天的目前時間
     * @return
     */
    public static Date tomorrow(){
       return nextDays(1);
    }

    /**
     * 後i天
     * @param i
     * @return
     */
    public static Date nextDays(int i){
        DateTime dateTime = DateTime
                .now()
                .plusDays(i);
        return dateTime.toDate();
    }

    /**
     * 昨天的目前時間
     * @return
     */
    public static Date yesterday(){
        return prevDays(1);
    }

    /**
     * 前i天
     * @param i
     * @return
     */
    public static Date prevDays(int i){
        DateTime dateTime = DateTime
                .now()
                .minusDays(i);
        return dateTime.toDate();
    }

    /**
     * 今天的開始時刻
     * @return
     */
    public static Date todayBegin(){
        return DateTime
                .now()
                .withTimeAtStartOfDay().toDate();
    }

    /**
     * 今天的結束時刻
     * @return
     */
    public static Date todayEnd(){
        return DateTime
                .now()
                .millisOfDay()
                .withMaximumValue()
                .toDate();
    }

    /**
     * 目前周的第一天
     * @return
     */
    public static Date firstDayOfWeek(){
        LocalDate dateTime = LocalDate.now().dayOfWeek().withMinimumValue();
        return dateTime.toDate();
    }

    /**
     * 目前周的最後一天
     * @return
     */
    public static Date lastDayOfWeek(){
        LocalDate dateTime = LocalDate.now().dayOfWeek().withMaximumValue();
        return dateTime.toDate();
    }

    /**
     * 當月的第一天
     * @return
     */
    public static Date firstDayOfMonth(){
        LocalDate dateTime = LocalDate.now().dayOfMonth().withMinimumValue();
        return dateTime.toDate();
    }

    /**
     * 當月的最後一天
     * @return
     */
    public static Date lastDayOfMonth(){
        LocalDate dateTime = LocalDate.now().dayOfMonth().withMaximumValue();
        return dateTime.toDate();
    }

    /**
     * 當年的第一天
     * @return
     */
    public static Date firstDayOfYear(){
        LocalDate dateTime = LocalDate.now().dayOfYear().withMinimumValue();
        return dateTime.toDate();
    }

    /**
     * 當年的最後一天
     * @return
     */
    public static Date lastDayOfYear(){
        LocalDate dateTime = LocalDate.now().dayOfYear().withMaximumValue();
        return dateTime.toDate();
    }

    /**
     * 相差天數
     * @param begin
     * @param end
     * @return
     */
    public static long daysOfDiffer(Date begin, Date end){
        return new Duration(new DateTime(begin), new DateTime(end)).getStandardDays();
    }

    /**
     * 相差小時數
     * @param begin
     * @param end
     * @return
     */
    public static long hoursOfDiffer(Date begin, Date end){
        return new Duration(new DateTime(begin), new DateTime(end)).getStandardHours();
    }

    /**
     * 相差分鐘數
     * @param begin
     * @param end
     * @return
     */
    public static long minutesOfDiffer(Date begin, Date end){
        return new Duration(new DateTime(begin), new DateTime(end)).getStandardMinutes();
    }

    /**
     * 相差秒數
     * @param begin
     * @param end
     * @return
     */
    public static long secondsOfDiffer(Date begin, Date end){
        return new Duration(new DateTime(begin), new DateTime(end)).getStandardSeconds();
    }

    /**
     * 指定日期是否在標明日期裡
     * @param index
     * @param begin
     * @param end
     * @return
     */
    public static boolean indexOfDays(Date index, Date begin, Date end){
        return new Interval(new DateTime(begin),new DateTime(end)).contains(new DateTime(index));
    }

    /**
     * 指定時間月是否是閏月
     * @param date
     * @return
     */
    public static boolean leapOfMonth(Date date){
        return new DateTime(date).monthOfYear().isLeap();
    }

    /**
     * 指定時間年是否是閏年
     * @param date
     * @return
     */
    public static boolean leapOfYear(Date date){
        return new DateTime(date).year().isLeap();
    }
}