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 ...
随机推荐
- MySQL:视图
视图 一.视图的概述 1. 含义:是从数据库中一个或多个表中导出的虚拟表2. 作用:①简单化 ②安全性 ③逻辑数据独立性3. 注意:一个表可以由多个视图: 二.视图的创建 1. 总的语法形式 CREA ...
- mongodb初步使用体验
前言 Mongodb是一个非常有名的缓存数据库,和它名气相当的还有redis和hbase.笔者之前使用过redis,memcache和elasticsearch,借着工作机会,正好可以好好学习一下mo ...
- Sql Server索引重建
公司线上数据有几千万数据,有时候索引碎片会导致索引达不到我们的预期查询效率,这个时候将索引重建将会提升一定效率,不过重建的时候一定得晚上用户少的时候,索引重建需要一定时间. 直接贴自动重建索引脚本吧 ...
- python学习2:turtle的使用蟒蛇绘制的学习以及自己摸索的等边三角形绘制(跟随mooc学习)
首先先放上蟒蛇的绘制程序 import turtle#引入外部库#def保留字用于 定义函数 def drawSnake(rad,angle,len,neckrad): for i in range( ...
- Debian下安装docker
1.安装docker.io包之前,需要先设置使用backports源 编辑/etc/apt/sources.list文件,加入下面这一句: deb http://http.debian.net/deb ...
- 王者荣耀交流协会第5次Scrum立会
开会时间:2017年10月31日下午18:00-18:31 共计31分钟 开会地点:一食堂二楼靠近窗户倒数第四排 今日完成工作进度: 王超同学完成了将生成饼状图原型整合到程序中 立会内容: 添加了新 ...
- es6学习日记3
函数的扩展 ES6 之前,不能直接为函数的参数指定默认值,只能采用变通的方法. function log(x, y) { y = y || 'World'; console.log(x, y); } ...
- Java基于opencv实现图像数字识别(一)
Java基于opencv实现图像数字识别(一) 最近分到了一个任务,要做数字识别,我分配到的任务是把数字一个个的分开:当时一脸懵逼,直接百度java如何分割图片中的数字,然后就百度到了用Buffere ...
- Mat的详解
[转]OpenCV中Mat的详解 每次碰到Mat都得反复查具体的用法,网上的基础讲解不多,难得看到一篇,赶快转来收藏~ 原文地址:http://www.opencvchina.com/thread-1 ...
- hnsdfz -- 6.19 -- day4
感觉还好…… 暴力分挂了很多不知道为什么…… 听说今天出题人hsh很劲…… c题正解是个奇怪的知识点…… 恩总的来说今天的节奏依旧很散(大课间去围观sdfz跑操了233 暴力分都写了但是似乎没有尝试脑 ...