Android 【日期工具类】

曾经的单位用过的时间工具类,我觉得挺好用的,分享出来。

package com.mysignin.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;



public class DateUtil {
    /**
     * 日期格式
     */
    private final static ThreadLocal<SimpleDateFormat> dateFormat = new ThreadLocal<SimpleDateFormat>() {
        protected SimpleDateFormat initialValue() {
            return new SimpleDateFormat("yyyy-MM-dd");
        }
    };

    /**
     * 时间格式
     */
    private final static ThreadLocal<SimpleDateFormat> timeFormat = new ThreadLocal<SimpleDateFormat>() {
        protected SimpleDateFormat initialValue() {
            return new SimpleDateFormat("yyyy-MM-dd HH:mm");
        }
    };

    /**
     * 获取当前时间:Date
     */
    public static Date getDate() {
        return new Date();
    }

    /**
     * 获取当前时间:Calendar
     */
    public static Calendar getCal() {
        return Calendar.getInstance();
    }

    /**
     * 日期转换为字符串:yyyy-MM-dd
     */
    public static String dateToStr(Date date) {
        if (date != null)
            return dateFormat.get().format(date);
        return null;
    }

    /**
     * 日期转换为字符串:yyyy-MM-dd HH:mm:ss
     */
    public static String dateToTime(Date date) {
        if (date != null)
            return timeFormat.get().format(date);
        return null;
    }

    /**
     * 时间转换为字符串:yyyy-MM-dd HH:mm:ss
     */
    public static String timeToStr(Date date) {
        if (date != null)
            return timeFormat.get().format(date);
        return null;
    }

    /**
     * 字符串转换为日期:yyyy-MM-dd
     */
    public static Date strToDate(String str) {
        Date date = null;
        try {
            date = dateFormat.get().parse(str);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * 字符串转换为时间:yyyy-MM-dd HH:mm:ss
     */
    public static Date strToTime(String str) {
        Date date = null;
        try {
            date = timeFormat.get().parse(str);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * 友好的方式显示时间
     */
    public static String friendlyFormat(String str) {
        Date date = strToTime(str);
        if (date == null)
            return ":)";
        Calendar now = getCal();
        String time = new SimpleDateFormat("HH:mm").format(date);

        // 第一种情况,日期在同一天
        String curDate = dateFormat.get().format(now.getTime());
        String paramDate = dateFormat.get().format(date);
        if (curDate.equals(paramDate)) {
            int hour = (int) ((now.getTimeInMillis() - date.getTime()) / 3600000);
            if (hour > 0)
                return time;
            int minute = (int) ((now.getTimeInMillis() - date.getTime()) / 60000);
            if (minute < 2)
                return "刚刚";
            if (minute > 30)
                return "半个小时以前";
            return minute + "分钟前";
        }

        // 第二种情况,不在同一天
        int days = (int) ((getBegin(getDate()).getTime() - getBegin(date).getTime()) / 86400000);
        if (days == 1)
            return "昨天 " + time;
        if (days == 2)
            return "前天 " + time;
        if (days <= 7)
            return days + "天前";
        return dateToTime(date);
    }

    public static String dateFormat(String str) {
        Date date = strToTime(str);
        if (date == null)
            return ":";
        Calendar now = getCal();
        String time = new SimpleDateFormat("HH:mm").format(date);

        // 第一种情况,日期在同一天
        String curDate = dateFormat.get().format(now.getTime());
        String paramDate = dateFormat.get().format(date);
        if (curDate.equals(paramDate)) {
            int hour = (int) ((now.getTimeInMillis() - date.getTime()) / 3600000);
            if (hour > 0)
                return time;
            int minute = (int) ((now.getTimeInMillis() - date.getTime()) / 60000);
            if (minute < 2)
                return "刚刚";
            if (minute > 30)
                return "半个小时以前";
            return minute + "分钟前";
        }

        // 第二种情况,不在同一天
        int days = (int) ((getBegin(getDate()).getTime() - getBegin(date).getTime()) / 86400000);
        if (days == 1)
            return "昨天 " + time;
        if (days == 2)
            return "前天 " + time;
        if (days <= 101)
            return days + "天前";
        return dateToStr(date);
    }

    /**
     * 返回日期的0点:2012-07-07 20:20:20 --> 2012-07-07 00:00:00
     */
    public static Date getBegin(Date date) {
        return strToTime(dateToStr(date) + " 00:00:00");
    }

    //把String类型的时间转变成long类型
    public static Long Convert(String datetine) {
        long songtime = 0;
        try {
            Calendar c = Calendar.getInstance();
            SimpleDateFormat format = new SimpleDateFormat();
            c.setTime(format.parse(datetine));
            songtime = c.getTimeInMillis();
        } catch (java.text.ParseException e) {
            e.printStackTrace();
        }

        return songtime;
    }

    //把秒转变成时分秒类型
    public static String secToTime(int time) {
        String timeStr = null;
        int hour = 0;
        int minute = 0;
        int second = 0;
        if (time <= 0)
            return "00:00";
        else {
            minute = time / 60;
            if (minute < 60) {
                second = time % 60;
                timeStr = unitFormat(minute) + ":" + unitFormat(second);
            } else {
                hour = minute / 60;
                if (hour > 99)
                    return "99:59:59";
                minute = minute % 60;
                second = time - hour * 3600 - minute * 60;
                timeStr = unitFormat(hour) + ":" + unitFormat(minute) + ":" + unitFormat(second);
            }
        }
        return timeStr;
    }

    public static String unitFormat(int i) {
        String retStr = null;
        if (i >= 0 && i < 10)
            retStr = "0" + Integer.toString(i);
        else
            retStr = "" + i;
        return retStr;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值