5.0关于时间的处理工具类joda-time

DateTimeUtil

package cn.firstpro.util;

import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

import java.util.Date;

/**
 * @Author LiuHe
 * @Date 2017/11/14 9:11
 */
public class DateTimeUtil {

    //使用joda-time这个开源包完成

    //做个默认格式的转化
    public static final String STANDARD_FORMAT = "yyyy-MM-dd HH:mm:ss";

    //str->date
    public static Date strToDate(String dateTimeStr, String formatStr) {
        DateTimeFormatter dateTimeFormater = DateTimeFormat.forPattern(formatStr);
        DateTime dateTime = dateTimeFormater.parseDateTime(dateTimeStr);
        return dateTime.toDate();
    }

    //date->str
    public static String dateToStr(Date date, String formatStr) {
        if (date == null) {
            return StringUtils.EMPTY;
        }
        DateTime dateTime = new DateTime(date);
        return dateTime.toString(formatStr);
    }

    //默认时间格式转化的重载方法
    public static Date strToDate(String dateTimeStr) {
        DateTimeFormatter dateTimeFormater = DateTimeFormat.forPattern(STANDARD_FORMAT);
        DateTime dateTime = dateTimeFormater.parseDateTime(dateTimeStr);
        return dateTime.toDate();
    }
    public static String dateToStr(Date date) {
        if (date == null) {
            return StringUtils.EMPTY;
        }
        DateTime dateTime = new DateTime(date);
        return dateTime.toString(STANDARD_FORMAT);
    }


}

你可能感兴趣的:(5.0关于时间的处理工具类joda-time)