zoukankan      html  css  js  c++  java
  • Android下的几种时间格式转换

    更多更全的工具类,请参考github上的Blankj/AndroidUtilCode

    将毫秒转换为小时:分钟:秒格式

    public static String ms2HMS(int _ms){
            String HMStime;
            _ms/=1000;
            int hour=_ms/3600;
            int mint=(_ms%3600)/60;
            int sed=_ms%60;
            String hourStr=String.valueOf(hour);
            if(hour<10){
                hourStr="0"+hourStr;
            }
            String mintStr=String.valueOf(mint);
            if(mint<10){
                mintStr="0"+mintStr;
            }
            String sedStr=String.valueOf(sed);
            if(sed<10){
                sedStr="0"+sedStr;
            }
            HMStime=hourStr+":"+mintStr+":"+sedStr;
            return HMStime;
        }
    

    将毫秒转换为标准日期格式

    public static String ms2Date(long _ms){
            Date date = new Date(_ms);
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
            return format.format(date);
        }
    
        public static String ms2DateOnlyDay(long _ms){
            Date date = new Date(_ms);
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
            return format.format(date);
        }
    

    标准时间转换为时间戳

    public static long Date2ms(String _data){
            SimpleDateFormat format =   new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            try {
                Date date = format.parse(_data);
                return date.getTime();
            }catch(Exception e){
                return 0;
            }
        }
    

    计算时间差

    public static  String DateDistance(Date startDate,Date endDate){
            if(startDate == null ||endDate == null){
                return null;
            }
            long timeLong = endDate.getTime() - startDate.getTime();
            if(timeLong<0){
                timeLong=0;
            }
            if (timeLong<60*1000)
                return timeLong/1000 + "秒前";
            else if (timeLong<60*60*1000){
                timeLong = timeLong/1000 /60;
                return timeLong + "分钟前";
            }
            else if (timeLong<60*60*24*1000){
                timeLong = timeLong/60/60/1000;
                return timeLong+"小时前";
            }
            else if ((timeLong/1000/60/60/24)<7){
                timeLong = timeLong/1000/ 60 / 60 / 24;
                return timeLong + "天前";
            }else{
                SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
                return formatter.format(startDate);
            }
        }
    

    计算与当前的时间差

    public static  String DateDistance2now(long _ms){
            SimpleDateFormat DateF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            try {
                Long time=new Long(_ms);
                String d = DateF.format(time);
                Date startDate=DateF.parse(d);
                Date nowDate = Calendar.getInstance().getTime();
                return DateDistance(startDate, nowDate);
            }catch (Exception e){
            }
            return null;
        }
    
  • 相关阅读:
    Winform中在ZedGraph中最多可以添加多少条曲线(转)
    c#委托的含义和用法
    vs2010打开vs2017工程
    C# Socket编程资源
    C# 调用打印机 打印 Excel (转)
    NPOI 教程
    C# 调用C++ DLL 的类型转换(转载版)(转)
    进程间通信(网络阅读笔记)
    NPOI 第二篇 设置样式与合并单元格(转)
    分布式事务的 6 种解决方案,写得非常好!
  • 原文地址:https://www.cnblogs.com/xl-phoenix/p/9617223.html
Copyright © 2011-2022 走看看