• Android Joda-time工具类


      Joda-Time提供了一组Java类包用于处理包括ISO8601标准在内的date和time。可以利用它把JDK Date和Calendar类完全替换掉,而且仍然能够提供很好的集成。

      Joda-Time主要的特点包括:

       1. 易于使用:Calendar让获取"正常的"的日期变得很困难,使它没办法提供简单的方法,而Joda-Time能够 直接进行访问域并且索引值1就是代表January。
       2. 易于扩展:JDK支持多日历系统是通过Calendar的子类来实现,这样就显示的非常笨重而且事实 上要实现其它日历系统是很困难的。Joda-Time支持多日历系统是通过基于Chronology类的插件体系来实现。

       3. 提供一组完整的功能:它打算提供 所有关系到date-time计算的功能.Joda-Time当前支持8种日历系统,而且在将来还会继续添加,有着比JDK Calendar更好的整体性能等等。


      在开发过程中,使用Joda-Time可以更多方便、快捷的对日期时间进行相关的处理操作。 下面,将Joda-Time部分功能(字符串解析为Date对象和Date对象格式化输出为字符串)封装起来,作为一个工具类,方便直接使用:

    import android.text.TextUtils;
    
    import org.joda.time.DateTime;
    import org.joda.time.format.DateTimeFormat;
    import org.joda.time.format.DateTimeFormatter;
    
    import java.util.Date;
    
    /**
     * 日期时间工具类
     * <p/>
     * 主要用于:
     * 1. 格式化日期时间输出
     * 2. 获取日期时间对象
     * <p/>
     * 注: 关于日期时间的计算,具体参照文档资料
     */
    public class DateUtil {
    
        private static final String DATE_TIME_PATTERN1 = "yyyy-MM-dd HH:mm:ss";
    
        private static final String DATE_TIME_PATTERN2 = "yyyy-MM-dd HH:mm";
    
    
        private static DateTimeFormatter dateTimeFormatter1 = DateTimeFormat.forPattern(DATE_TIME_PATTERN1);
    
        private static DateTimeFormatter dateTimeFormatter2 = DateTimeFormat.forPattern(DATE_TIME_PATTERN2);
    
        /**
         * 获取当前时间的DateTime对象
         *
         * @return
         */
        public static DateTime getCurrentDateTime() {
    
            DateTime dateTime = new DateTime(System.currentTimeMillis());
    
            return dateTime;
        }
    
        /**
         * 获取Date : yyyy-MM-dd HH:mm:ss
         *
         * @param datetime
         * @return
         */
        public static Date parseDateTime1(String datetime) {
    
            if (TextUtils.isEmpty(datetime)) {
    
                return null;
            }
    
            DateTime dateTime = null;
    
            try {
                dateTime = DateTime.parse(datetime, dateTimeFormatter1);
            } catch (Exception e) {
                e.printStackTrace();
            }
    
            if (dateTime != null) {
                return dateTime.toDate();
            }
    
            return null;
    
        }
    
        /**
         * 获取Date : yyyy-MM-dd HH:mm
         *
         * @param datetime
         * @return
         */
        public static Date parseDateTime2(String datetime) {
    
            if (TextUtils.isEmpty(datetime)) {
    
                return null;
            }
    
            DateTime dateTime = null;
    
            try {
                dateTime = DateTime.parse(datetime, dateTimeFormatter2);
            } catch (Exception e) {
                e.printStackTrace();
            }
    
            if (dateTime != null) {
                return dateTime.toDate();
            }
    
            return null;
        }
    
        /**
         * 格式化输出日期: 2016-1-16 13:04:11
         *
         * @param date
         * @return
         */
        public static String formatDateTime1(Date date) {
    
            if (date == null) {
                return null;
            }
    
            DateTime dateTime = new DateTime(date);
    
            return dateTime.toString(DATE_TIME_PATTERN1);
        }
    
        /**
         * 格式化输出日期: 2016-1-16 13:04:11
         *
         * @param instant
         * @return
         */
        public static String formatDateTime1(long instant) {
    
            DateTime dateTime = new DateTime(instant);
    
            return dateTime.toString(DATE_TIME_PATTERN1);
        }
    
        /**
         * 格式化输出日期: 2016-1-16 13:04
         *
         * @param date
         * @return
         */
        public static String formatDateTime2(Date date) {
    
            if (date == null) {
                return null;
            }
    
            DateTime dateTime = new DateTime(date);
    
            return dateTime.toString(DATE_TIME_PATTERN2);
        }
    
        /**
         * 格式化输出日期: 2016-1-16 13:04:11
         *
         * @param instant
         * @return
         */
        public static String formatDateTime2(long instant) {
    
            DateTime dateTime = new DateTime(instant);
    
            return dateTime.toString(DATE_TIME_PATTERN2);
        }
    
    }
    


      Joda-Time地址:https://github.com/JodaOrg/joda-time

      文档资料:  http://persevere.iteye.com/blog/1755237

      

      如此这般,就OK啦!欢迎互相学习!
      如有疑问,欢迎进QQ群:487786925( Android研发村 )

  • 相关阅读:
    nacos 命名空间
    Error Code: 1175. You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column. To disable safe mode, toggle the option in Preferences
    gitee
    maven引入junit 4.12,项目import org.junit.Test 还是报错.
    gitflow
    202011
    idea 忽略显示不需要的文件
    服务熔断 & 降级区别
    各种微服务框架对比
    zookeeper not connected
  • 原文地址:https://www.cnblogs.com/hehe520/p/6329944.html
Copyright © 2020-2023  润新知