计算时间戳后转时分秒格式或者年月日,日期时间转时间戳,附实验结果

计算获得时间戳后转时分秒格式,附实验结果

 

/**
 * @Author: Be.insighted
 * Description:
 * @date Create on 2020/7/14 16:35
 **/
​
public class TimeUtils{  
​
/**
     * 把时间戳转换为:时分秒
     *
     * @param millisecond :毫秒,传入单位为毫秒
     */
        public static String getTimeString(final long millisecond) {
        if (millisecond < 1000) {
            return "0" + "秒";
        }
        long second = millisecond / 1000;
        long seconds = second % 60;
        long minutes = second / 60;
        long hours = 0;
        if (minutes >= 60) {
            hours = minutes / 60;
            minutes = minutes % 60;
        }
        String timeString = "";
        String secondString = "";
        String minuteString = "";
        String hourString = "";
        if (seconds < 10) {
            secondString = "0" + seconds + "秒";
        } else {
            secondString = seconds + "秒";
        }
        if (minutes < 10 && hours < 1) {
            minuteString = minutes + "分";
        } else if (minutes < 10){
            minuteString =  "0" + minutes + "分";
        } else {
            minuteString = minutes + "分";
        }
        if (hours < 10) {
            hourString = hours + "时";
        } else {
            hourString = hours + "" + "时";
        }
        if (hours != 0) {
            timeString = hourString + minuteString + secondString;
        } else {
            timeString = minuteString + secondString;
        }
        return timeString;
    }

    public static Date convertTimeStamp2Date(long timeStamp) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date(timeStamp);
        try {
            date = df.parse(df.format(date));
        } catch (ParseException e) {
            log.error("时间格式化错误");
        }
        return date;
    }

    public static long dateToStamp(Date date, int hour, int minute, int second) {
            LocalDateTime timestamp =         date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
            return timestamp.withHour(hour).withMinute(minute).withSecond(second).atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
        }
}

实验结果

 

 

 

 

©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页