更多更全的工具类,请参考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. noj最长公共子序列

    1041.最长公共子序列 时限:1000ms 内存限制:200000K  总时限:3000ms 描述 一个给定序列的子序列是在该序列中删去若干元素后得到的序列.确切地说,若给定序列X=<x1, ...

  2. ES6 用Promise对象实现的 Ajax 操作

    下面是一个用Promise对象实现的 Ajax 操作的例子. const getJSON = function(url) { const promise = new Promise(function( ...

  3. 写了一个Java的简单缓存模型

    缓存操作接口 /** * 缓存操作接口 * * @author xiudong * * @param <T> */ public interface Cache<T> { /* ...

  4. CentOS 7 命令行安装TeamViewer

    由于要通过要远程登录到内网的电脑(一台笔记本),用于在紧急情况下处理服务器故障.刚开始准备使用ssh端口转发,无奈vps转发速度太慢. 后面考虑使用TeamViewer远程控制Windows桌面,但是 ...

  5. python学习之路07

    Python中你可能不关心的这几个关键字:break.continue.pass 1.break 作用:跳出循环[直接跳出整个循环,继续执行后面的代码] 1.特殊情况一 #当break使用在嵌套循环中 ...

  6. 20155208徐子涵 Exp5 MSF基础应用

    20155208徐子涵 Exp5 MSF基础应用 基础问题回答 用自己的话解释什么是exploit,payload,encode. Exploit:Exploit 的英文意思就是利用,它在黑客眼里就是 ...

  7. mongo 索引,速度

    (如有打扰,请忽略)阿里云ECS大羊群,2U4G低至1.4折,限实名新用户,需要的点吧https://promotion.aliyun.com/ntms/act/vm/aliyun-group/tea ...

  8. 我的代码-models

    # coding: utf-8 # In[1]: import pandas as pdimport numpy as npfrom sklearn import treefrom sklearn.s ...

  9. java web(七): mybatis的动态sql和mybatis generator自动生成pojo类和映射文件

    前言: MyBatis 的强大特性之一便是它的动态 SQL.如果你有使用 JDBC 或其它类似框架的经验,你就能体会到根据 不同条件拼接 SQL 语句的痛苦.例如拼接时要确保不能忘记添加必要的空格,还 ...

  10. zabbix使用微信报警(四)

    https://qy.weixin.qq.com/  企业号注册 http://qydev.weixin.qq.com/wiki/index.php?title=%E9%A6%96%E9%A1%B5  ...