Android 中常用的时间转换集合

作者: _执_念__ | 来源:发表于2017-11-08 10:19 被阅读41次

    在我们的日常开发中经常会碰到需要时间转换,这里我把常用的时间转换方法封装成一个TimeUtils,做项目的时候放进项目里直接调用其中方法就行了,很方便。
    废话不多说,直接上代码

    package com.***.***.util
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Calendar;
    import java.util.Date;
    import java.util.TimeZone;
    
    /**
     *
     * ClassName:TimeUtils 通用时间类
     *
     * @author walle
     * @version 1.0
     * @since Ver 1.0
     *
     */
    public class TimeUtils {
        public static final SimpleDateFormat DEFAULT_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        public static final SimpleDateFormat DATE_FORMAT_DATE = new SimpleDateFormat("yyyy-MM-dd");
        public static final SimpleDateFormat File_DATE_FORMAT = new SimpleDateFormat("yyyyMMddHHmmss");
        public static final SimpleDateFormat DATE_FORMAT_DATE_NO_YEAR = new SimpleDateFormat("MM-dd");
    
        /**
         * 将毫秒时间转换为String类型
         *
         * @param timeInMillis
         *            毫秒时间
         * @param dateFormat
         *            时间格式
         * @return
         */
        public static String getTime(long timeInMillis, SimpleDateFormat dateFormat) {
            return dateFormat.format(new Date(timeInMillis));
        }
    
        /**
         * 将毫秒时间转换为String类型 默认时间格式为(yyyy-MM-dd HH:mm:ss)
         *
         * @param timeInMillis
         *            毫秒时间
         * @return
         */
        public static String getTime(long timeInMillis) {
            return getTime(timeInMillis, DEFAULT_DATE_FORMAT);
        }
    
        /**
         * 获取当前的毫秒时间
         *
         * @return 毫秒时间
         */
        public static long getCurrentTimeInLong() {
            return System.currentTimeMillis();
        }
    
        /**
         * 获取当前时间,默认时间格式为 yyyy-MM-dd HH:mm:ss
         *
         * @return 当前时间
         */
        public static String getCurrentTimeInString() {
            return getTime(getCurrentTimeInLong());
        }
    
        /**
         * 获取当前日期,默认日期格式为 yyyy-MM-dd
         *
         * @return 当前日期
         */
        public static String getCurrentDateInString() {
            return getTime(getCurrentTimeInLong(), DATE_FORMAT_DATE);
        }
    
        /**
         *
         * getCurrentTimeInString:(根据毫秒时间获取当前时间)
         *
         * @param dateFormat
         *            时间格式
         * @return String 时间
         * @throws @since
         *             CodingExample Ver 1.0
         */
        public static String getCurrentTimeInString(SimpleDateFormat dateFormat) {
            return getTime(getCurrentTimeInLong(), dateFormat);
        }
    
        /**
         * 获取时间毫秒数
         *
         * @param time
         * @return
         */
        public static long getTimeInLong(String time) {
            try {
                Date date = DEFAULT_DATE_FORMAT.parse(time);
                return date.getTime();
            } catch (ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    
            return 0;
        }
    
        public static long getTimeInLong(String time, SimpleDateFormat format) {
            try {
                Date date = format.parse(time);
                return date.getTime();
            } catch (ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    
            return 0;
        }
    
        /**
         * 计算两个时间段相差毫秒数
         *
         * @param dateBefore
         * @param dateAfter
         * @return
         */
        public static long getSubTimeInMillionSecond(String dateBefore, String dateAfter) {
            try {
                long before = getTimeInLong(dateBefore);
                long after = getTimeInLong(dateAfter);
                long subValue = after - before;
                return subValue;
            } catch (Exception e) {
                return 0;
            }
    
        }
    
        /**
         * 计算两个时间段相差几天
         *
         * @param dateBefore
         * @param dateAfter
         * @return
         */
        public static long getSubDays(String dateBefore, String dateAfter) {
            long milliSecond = getSubTimeInMillionSecond(dateBefore, dateAfter);
            long subDays = milliSecond / (1000 * 60 * 60 * 24);
            return subDays;
        }
    
        
    
        public static String getCurDate() {
            final Calendar c = Calendar.getInstance();
            c.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
            String mYear = String.valueOf(c.get(Calendar.YEAR)); // 获取当前年份
            String mMonth = String.valueOf(c.get(Calendar.MONTH) + 1);// 获取当前月份
            String mDay = String.valueOf(c.get(Calendar.DAY_OF_MONTH));// 获取当前月份的日期号码
            return mYear + "年" + mMonth + "月" + mDay + "日";
        }
    
        public static String getWeekOfDate() {
            String[] weekDays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
            Calendar cal = Calendar.getInstance();
            Date curDate = new Date(System.currentTimeMillis());
            cal.setTime(curDate);
            int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
            if (w < 0) {
                w = 0;
            }
            return weekDays[w];
        }
    
        public static String getWeekIndexOfDate() {
            Calendar cal = Calendar.getInstance();
            Date curDate = new Date(System.currentTimeMillis());
            cal.setTime(curDate);
            int w = cal.get(Calendar.WEEK_OF_YEAR);
            if (w < 0) {
                w = 0;
            }
            return String.valueOf(w);
        }
    
    }
    

    在做项目的时候创建一个TimeUtils类,将以上代码复制进去,在需要的地方调用,非常方便,节约了重复查找的时间。

    相关文章

      网友评论

        本文标题:Android 中常用的时间转换集合

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