美文网首页
工具类--时间格式转换

工具类--时间格式转换

作者: wbpailxt | 来源:发表于2020-01-11 17:26 被阅读0次
    package com.mmall.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;
    
    /**
     * Created by geely
     */
    public class DateTimeUtil {
    
        //joda-time
    
        //str->Date
        //Date->str
        public static final String STANDARD_FORMAT = "yyyy-MM-dd HH:mm:ss";
    
        public static Date strToDate(String dateTimeStr,String formatStr){
            DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern(formatStr);
            DateTime dateTime = dateTimeFormatter.parseDateTime(dateTimeStr);
            return dateTime.toDate();
        }
    
        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 dateTimeFormatter = DateTimeFormat.forPattern(STANDARD_FORMAT);
            DateTime dateTime = dateTimeFormatter.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);
        }
    
        public static void main(String[] args) {
            System.out.println(DateTimeUtil.dateToStr(new Date(),"yyyy-MM-dd HH:mm:ss"));
            System.out.println(DateTimeUtil.strToDate("2010-01-01 11:11:11","yyyy-MM-dd HH:mm:ss"));
        }
    }
    

    相关文章

      网友评论

          本文标题:工具类--时间格式转换

          本文链接:https://www.haomeiwen.com/subject/bvkbactx.html