更多更全的工具类,请参考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. bootstrap之编译CSS和Javascript-0基础安装grunt教程

    昨天晚上看到 bootstrap 全局CSS样式中 使用Less 章节中提到的通过grunt重新编译CSS和Javascript文件,对于我这样从未接触过windows cmd node控制台 npm ...

  2. Linux修改日期、时间,系统与硬件时间

    Linux的时间分为两种,硬件时间和系统时间两种: 一.查看与修改系统时间 查看系统时间:date # date Fri Nov 26 15:20:18 CST 1999 用指定的格式显示系统时间:  ...

  3. Python 事件

    from multiprocessing import Process,Event e = Event() #创建事件对象,这个对象的初识状态为False print('e的状态是:',e.is_se ...

  4. springBoot的数据库操作

    一:操作数据库起步 1.Spring-Data-Jpa JPA定义了对象持久化的标准. 目前实现了有Hibernate,TopLink 2.pom添加依赖 <dependency> < ...

  5. 数据持久化系列之Mysql

    一.命令行操作 1.显示所有库: show databases; 2.要操作某个库,比如库名: db_book:use db_book; 3.查看表的基本结构,比如表名: t_book:desc t_ ...

  6. 小妖精的完美游戏教室——人工智能,A*算法,引言

    今天也要直播魔法,求科学的! 欢迎来到小妖精Balous的完美游戏教室! 经过前两周的学习,相信米娜桑已经对状态机有所了解了呢~虽然状态机能够实现几乎所有的人工智能,但是,在实践中,你们有没有发现,自 ...

  7. 在jsp中应如何避免,request.getContextPath();等get报错问题

    导致request中的大部分get方法无法获取,是因为没有依赖的jar包.就像这样.这里不仅仅有getContextPath()方法报错,如果存在这个问题,大部分的request方法都不可用. 解决这 ...

  8. windows openssh 设置root 目录

    默认windows openssh 服务的root 目录是用户账户所在的目录(一般是administrator),但是我们可以通过修改sshd_config 重新修改路径 可选的修改方式 直接修改ss ...

  9. 第一章 C#入门 (Windows窗体应用程序)(一)

    我的第一个窗体应用程序(一) [案例说明]  在文本框中显示一行文字“Hello C#!”,单击[显示]按钮后在文本框中显示文字:单击[清除]按钮后清除文本框中的内容. [案例实现步骤] 1.新建项目 ...

  10. MySql Scaffolding an Existing Database in EF Core

    官方文档详见:https://dev.mysql.com/doc/connector-net/en/connector-net-entityframework-core-scaffold-exampl ...