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;
}
Android下的几种时间格式转换的更多相关文章
- Sql日期时间格式转换;取年 月 日,函数:DateName()、DATEPART()
一.sql server2000中使用convert来取得datetime数据类型样式(全) 日期数据格式的处理,两个示例: CONVERT(varchar(16), 时间一, 20) 结果:2007 ...
- sql 日期时间格式转换
Sql日期时间格式转换 sql server2000中使用convert来取得datetime数据类型样式(全) 日期数据格式的处理,两个示例: CONVERT(varchar(16), 时间一, ...
- 问题:Oracle to_date;结果:oracle常用的时间格式转换
oracle常用的时间格式转换 1:取得当前日期是本月的第几周 SQL> select to_char(sysdate,'YYYYMMDD W HH24:MI:SS') from dual; T ...
- SQL Server日期时间格式转换字符串详解 (详询请加qq:2085920154)
在SQL Server数据库中,SQL Server日期时间格式转换字符串可以改变SQL Server日期和时间的格式,是每个SQL数据库用户都应该掌握的.本文我们主要就介绍一下SQL Server日 ...
- SQL Server日期时间格式转换字符串
在SQL Server数据库中,SQL Server日期时间格式转换字符串可以改变SQL Server日期和时间的格式,是每个SQL数据库用户都应该掌握的.本文我们主要就介绍一下SQL Server日 ...
- MySQL时间戳和时间格式转换函数
MySQL时间戳和时间格式转换函数:unix_timestamp and from_unixtime unix_timestamp将时间转化成时间戳格式.from_unixtime将时间戳转化成时间格 ...
- SQL Server日期时间格式转换字符串详解
本文我们主要介绍了SQL Server日期时间格式转换字符串的相关知识,并给出了大量实例对其各个参数进行对比说明,希望能够对您有所帮助. 在SQL Server数据库中,SQL Server日期时间格 ...
- [php基础]Mysql日期函数:日期时间格式转换函数详解
在PHP网站开发中,Mysql数据库设计中日期时间字段必不可少,由于Mysql日期函数输出的日期格式与PHP日期函数之间的日期格式兼容性不够,这就需要根据网站实际情况使用Mysql或PHP日期转换函数 ...
- lua 特殊时间格式转换
[1]时间格式转换需求 工作中,因业务需要将时间格式进行转换.需求内容如下: 原格式:17:04:49.475 UTC Mon Mar 04 2019 转换格式:2019-03-04 17:04:4 ...
随机推荐
- 18-09-21 numpy 的基础学习01
# 1关于numpy 的学习import numpy as np # 一 如何创建数组****# 1 有规律的一维数据的创建======# 1 range() 和arange() 区别 貌似没有区别l ...
- Java实现数据库与eclipse的连接
JavaBean:用于传递数据,拥有与数据相关的逻辑处理 JSP:从Model接收数据并生成HTML Servlet:接收HTTP请求并控制Model和View jdbc:用于驱动连接 一.[建立数据 ...
- jQuery-1.样式篇
jQuery对象与DOM对象 对于才开始接触jQuery库的初学者,我们需要清楚认识一点: jQuery对象与DOM对象是不一样的 可能一时半会分不清楚哪些是jQuery对象,哪些是DOM对象,下面重 ...
- node 创建server 及加载静态页面
1.Demo.js 创建Server let http = require('http'); let url = require("url"); let util = re ...
- Js高级 部分内容 面向对象
1.单列模式 2.工厂模式 3.构造函数 (1)类 js天生自带的类 Object基类 Function Array String Number Math Date Boolean Regex ...
- mysql 分组排序前n + 长表转宽表
MySQL数据库优化的八种方式(经典必看) 建表 CREATE TABLE if not EXISTS `bb` ( `id` int not null primary key auto_increm ...
- oracle-rman-3
http://blog.csdn.net/leshami/article/details/6032525 rman概述及体系结构 http://blog.itpub.net/23513800/view ...
- zabbix_agent添加到系统服务启动(八)
Centos6.5上安装了zabbix_agent后,需要把zabbix_agent添加到系统服务启动,要不然每次要一长串路径再启动,挺麻烦的. 步骤: 1)拷贝zabbix解压包里的zabbix_a ...
- python制作模块
自己写的函数,为了下一次方便用,做成模块 主要有这几个步骤: 1:准备发布 2:构建发布 3:导入模块并使用 1:准备发布 首先,我自己写的一个打印出列表(含嵌套列表),打印出列表中的每个数据项,文件 ...
- python的set处理二维数组转一维数组
for splitValue in set(dataset[:, featureIndex].tolist()): 首先set是一个无序,无重复的数据结构,所以很多时候使用它来进行去重:但是set接收 ...