DateUtil--日期转换工具

本文介绍了一个日期转换工具DateUtil,该工具能够实现日期格式的灵活转换,并提供了根据当前时间计算相对时间的功能。通过实例展示了如何使用这个工具来改变日期显示格式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

DateUtil–日期转换工具

DateUtil.java

public class DateUtil {
    private static final long S = 1000L;//一秒
    private static final long MIN = 60 * S;//一分钟
    private static final long H = 60 * MIN;//一小时
    private static final long D = 24 * H;//一天
    private static final long Y = 365 * D;//365天

    //@by zhiqiang: 2016/5/27 定义日期的格式
    private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private static SimpleDateFormat yearFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    private static SimpleDateFormat monthFormat = new SimpleDateFormat("MM-dd HH:mm");


    /**
     * @by zhiqiang 2016/5/27 转换date数据格式    可以写在工具类中
     */
    public static String changeDateFormat(String date, String type) {

        //将传过来的字符串转化一次
        Date old = null;
        try {
            //解析字符串
            sdf.setTimeZone(TimeZone.getTimeZone(type));
            old = sdf.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        //@by zhiqiang: 2016/5/27 获取当前日期
        Date newD = new Date();
        //@by zhiqiang: 2016/5/27 如果当前时间在传递过来的时间之前
        if (newD.before(old)) {
            return yearFormat.format(old);
        }

        return parseDate(old, newD);

    }

    /**
     * @by zhiqiang 2016/6/13  根据当前时间的毫秒值计算
     */
    private static String parseDate(Date old, Date newD) {
        if (old != null && newD != null) {
            final long oldTime = old.getTime();
            final long newTime = newD.getTime();
            final long time = newTime - oldTime;

            if (time <= MIN) {    //1分钟之内 just
                return "just";
            } else if (time <= H) {    //1小时之内 几分钟之前
                int minter = (int) (time / MIN);
                return minter + " " + "分钟前";
            } else if (time <= D) {//1天之内 几小时之前
                int hour = (int) (time / H);
                return hour + " 小时前";
            } else if (time <= 7 * D) {//7天之内
                int day = (int) (time / D);
                if (day == 1) {//一天前
                    return "昨天";
                } else {
                    return day + " 天前";
                }
            } else if (time <= Y) {//365天之内
                sdf.setTimeZone(TimeZone.getDefault());
                return monthFormat.format(old);
            } else {
                sdf.setTimeZone(TimeZone.getDefault());
                return yearFormat.format(old);
            }
        }
        return "";
    }
}


显示日期

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TextView tv_old = (TextView) findViewById(R.id.tv_old);
        TextView tv_new = (TextView) findViewById(R.id.tv_new);

        String oldDateString = "2016-06-20 16:09:08";

        String change = DateUtil.changeDateFormat(oldDateString,"GMT:+08:00");

        tv_old.setText(oldDateString);
        tv_new.setText(change);

    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值