更多更全的工具类,请参考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;
}

Android下的几种时间格式转换的更多相关文章

  1. Sql日期时间格式转换;取年 月 日,函数:DateName()、DATEPART()

    一.sql server2000中使用convert来取得datetime数据类型样式(全) 日期数据格式的处理,两个示例: CONVERT(varchar(16), 时间一, 20) 结果:2007 ...

  2. sql 日期时间格式转换

    Sql日期时间格式转换   sql server2000中使用convert来取得datetime数据类型样式(全) 日期数据格式的处理,两个示例: CONVERT(varchar(16), 时间一, ...

  3. 问题:Oracle to_date;结果:oracle常用的时间格式转换

    oracle常用的时间格式转换 1:取得当前日期是本月的第几周 SQL> select to_char(sysdate,'YYYYMMDD W HH24:MI:SS') from dual; T ...

  4. SQL Server日期时间格式转换字符串详解 (详询请加qq:2085920154)

    在SQL Server数据库中,SQL Server日期时间格式转换字符串可以改变SQL Server日期和时间的格式,是每个SQL数据库用户都应该掌握的.本文我们主要就介绍一下SQL Server日 ...

  5. SQL Server日期时间格式转换字符串

    在SQL Server数据库中,SQL Server日期时间格式转换字符串可以改变SQL Server日期和时间的格式,是每个SQL数据库用户都应该掌握的.本文我们主要就介绍一下SQL Server日 ...

  6. MySQL时间戳和时间格式转换函数

    MySQL时间戳和时间格式转换函数:unix_timestamp and from_unixtime unix_timestamp将时间转化成时间戳格式.from_unixtime将时间戳转化成时间格 ...

  7. SQL Server日期时间格式转换字符串详解

    本文我们主要介绍了SQL Server日期时间格式转换字符串的相关知识,并给出了大量实例对其各个参数进行对比说明,希望能够对您有所帮助. 在SQL Server数据库中,SQL Server日期时间格 ...

  8. [php基础]Mysql日期函数:日期时间格式转换函数详解

    在PHP网站开发中,Mysql数据库设计中日期时间字段必不可少,由于Mysql日期函数输出的日期格式与PHP日期函数之间的日期格式兼容性不够,这就需要根据网站实际情况使用Mysql或PHP日期转换函数 ...

  9. lua 特殊时间格式转换

    [1]时间格式转换需求 工作中,因业务需要将时间格式进行转换.需求内容如下: 原格式:17:04:49.475  UTC Mon Mar 04 2019 转换格式:2019-03-04 17:04:4 ...

随机推荐

  1. Redis在Linux系统下的安装和启动

    详情可参见:https://blog.csdn.net/q1035331653/article/details/79077260 开机自启脚本参见:https://www.cnblogs.com/si ...

  2. 2017年3月28日15:59:16 终于明白spring这套鬼东西是怎么玩的了

    先说重点,新东家公司的项目框架没有一样是我之前用过的,首先pm和我说的是一套微服务的概念,微服务不同于传统的功能模块实现,他将服务松散化分不到各个系统之间,这样也是实现分散压力的一种. 微服务是由sp ...

  3. android active间数据传递

    Bundle是key-value存储. Bundle bundle=new Bundle(); bundle.putString("key", "value") ...

  4. 关于css样式错乱

    在浏览器中的console中执行以下代码会有惊喜哦: [].forEach.call($$("*"), function(a) { a.style.outline = " ...

  5. 一台服务器绑定多个ip地址

    一台服务器绑定多个ip地址. 方法一: 使用标准的网络配置工具(比如ifconfig和route命令)添加lP别名: 在eth0网卡再绑定一个ip:192.168.101.103 /sbin/ifco ...

  6. ArrayUtil的创建和使用

    求数组最小值求数组最大值遍历数组元素求数组总和求数组平均数求数组反转实现数组的复制对数组进行排序 写入的方法和包: public class ArrayUtil { //求数组的最大值 public ...

  7. Java图片验证码学习

  8. java android 捕获未处理异常

    1. 定义一个异常处理类 public class ExceptionHandler implements Thread.UncaughtExceptionHandler { public Excep ...

  9. Linux学习--“杀死”程序

    (cp https://billie66.github.io/TLCL/book/chap11.html) While this is all very straightforward, there ...

  10. 1.1.20 Word不能保存问题

    1.进入如下目录:C:\用户(user)\Administrator\AppData\Roaming\Microsoft\Templates 2.找到Normal和NormalOld的两个文件,删除. ...