Java格式化时间

Java格式化时间

将秒或者毫秒值格式化成指定格式的时间

效果图

这里写图片描述

工具类

工具类里我只列出了一种格式的格式化方式,可以根据自己的需求,修改“yyyy-MM-dd hh:mm:ss”,改成自己想要的时间格式就可以了。

符号 描述
y
M
d
h
m
s
package ……;

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

/**
 * Created by kongqw on 2015/12/4.
 */
public final class TimeUtil {

    // 私有化
    private TimeUtil() {
    }

    /**
     * 将毫秒转换成指定时间
     *
     * @param milliseconds 要格式化的时间毫秒值
     * @return
     */
    public static String formatTime(long milliseconds) {
        String time = null;
        try {
            // 初始化时间
            Date date = new Date(milliseconds);
            // 要格式化的时间格式
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            time = format.format(date);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return time;
        }
    }
}

posted on 2015-12-17 15:05  封起De日子  阅读(305)  评论(0编辑  收藏  举报

导航