时间格式转换

SimpleDateFormat sdf2= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
SimpleDateFormat sdf = new SimpleDateFormat("MMM d, yyyy K:m:s a",Locale.ENGLISH);
Date d2 = null;
try {
    d2 = sdf.parse("Sep 18, 2017 4:17:28 PM");
} catch (ParseException e) {
    e.printStackTrace();
}
Date d3 = new Date();
LogUtil.e("d2 ====== "+d2+"   "+sdf2.format(d2));
LogUtil.e("d3 ====== "+"  "+d3+"  "+sdf.format(d3));
    /**
     * 根据时间 转换格式
     *
     * @param lSeconds
     * @return
     */
    public static String formatTimeString(long lSeconds) {
        long nHour = lSeconds / 3600;
        long nMin = lSeconds % 3600;
        long nSec = nMin % 60;
        nMin = nMin / 60;
        if (nHour != 0 && nMin != 0 && nSec != 0) {
            return String.format("%02d小时%02d分钟%02d秒", nHour, nMin, nSec);
        } else if (nHour != 0 && nMin == 0 && nSec == 0) {
            return String.format("%02d小时", nHour);
        } else if (nHour == 0 && nMin != 0 && nSec != 0) {
            return String.format("%02d分钟%02d秒", nMin, nSec);
        } else if (nHour == 0 && nMin != 0) {
            return String.format("%02d分钟", nMin);
        } else if (nHour == 0 && nSec != 0) {
            return String.format("%02d秒", nSec);
        }
        return String.format("%02d小时%02d分钟%02d秒", nHour, nMin, nSec);
    }

    /***
     * 格式化时间
     * @param pattern
     * @param milli
     * @return
     */
    public static String formatTime(String pattern, int milli) {
        int h = (int) (milli / DateUtils.HOUR_IN_MILLIS);
        int m = (int) (milli / DateUtils.MINUTE_IN_MILLIS);
        int s = (int) ((milli / DateUtils.SECOND_IN_MILLIS) % 60);
        String hh = String.format(Locale.getDefault(), "%02d", h);
        String mm = String.format(Locale.getDefault(), "%02d", m);
        String ss = String.format(Locale.getDefault(), "%02d", s);
        return pattern.replace("hh", hh).replace("mm", mm).replace("ss", ss);
    }

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值