• Java 将日期或秒数转换为日时分秒,如:2天3小时23分32秒


    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    /**
     * 将日期和秒数转换为日时分秒,如:2天3小时23分32秒
     * @author WULEI
     *
     */
    public class TimeSwitch {
        
        public static void main(String[] args) {
            String time1 = secondToTime(85959);
            String time2 = dateToTime("2017/10/25 16:42:46", "yyyy/MM/dd HH:mm:ss");
            System.out.println(time1);
            System.out.println(time2);
        }
        
        /**
         * 将秒数转换为日时分秒,
         * @param second
         * @return
         */
        public static String secondToTime(long second){
            long days = second / 86400;            //转换天数
            second = second % 86400;            //剩余秒数
            long hours = second / 3600;            //转换小时
            second = second % 3600;                //剩余秒数
            long minutes = second /60;            //转换分钟
            second = second % 60;                //剩余秒数
            if(days>0){
                return days + "天" + hours + "小时" + minutes + "分" + second + "秒";
            }else{
                return hours + "小时" + minutes + "分" + second + "秒";
            }
        }
        
        /**
         * 将日期转换为日时分秒
         * @param date
         * @return
         */
        public static String dateToTime(String date, String dateStyle){
            SimpleDateFormat format = new SimpleDateFormat(dateStyle);
            try {
                Date oldDate = format.parse(date);
                long time = oldDate.getTime();                    //输入日期转换为毫秒数
                long nowTime = System.currentTimeMillis();        //当前时间毫秒数
                long second = nowTime - time;                    //二者相差多少毫秒
                second = second / 1000;                            //毫秒转换为妙
                long days = second / 86400;
                second = second % 86400;
                long hours = second / 3600;
                second = second % 3600;
                long minutes = second /60;
                second = second % 60;
                if(days>0){
                    return days + "天" + hours + "小时" + minutes + "分" + second + "秒";
                }else{
                    return hours + "小时" + minutes + "分" + second + "秒";
                }
            } catch (ParseException e) {
                e.printStackTrace();
            }
            return null;
        }
    }
  • 相关阅读:
    Process 'command '/Users/lidaqiang/Library/Android/sdk/build-tools/27.0.3/aapt'' finished with non-zero exit value 1
    合同诈骗无罪裁判要旨、判决理由及评析意见
    互换不同种类毒品行为如何认定
    正当防卫指导性案例以及研析
    期待可能性理论的司法适用
    共同犯罪认定方法
    刑事案件鉴定意见常用质证要点
    张明楷的100个刑法案例
    刑事律师办案必备126部法律
    正当防卫裁判要旨16条
  • 原文地址:https://www.cnblogs.com/shangrongyiweng/p/7737712.html
Copyright © 2020-2023  润新知