安卓时间戳和时间字符串转换工具类
来源:互联网 发布:怎样找淘宝一件代发 编辑:程序博客网 时间:2024/05/18 23:54
我们前端和后台打交道都是用到时间戳如:1509330651,但我显示给用户看的都是各种格式的时间字符串:yyyy年MM月dd日 ,HH:mm。
不多说直接上代码:
/** * 时间相关工具类 */public class TimeUtil {/** * 以指定字符串形式返回当前系统时间 */public static String getSystem() {SimpleDateFormat format = new SimpleDateFormat(Constant.cformatsecond,Locale.getDefault());Date date = new Date(System.currentTimeMillis());String s = format.format(date);return s;}/** * 时间戳转成指定字符串 */public static String long2time(long time, String formatStyle) {SimpleDateFormat format = new SimpleDateFormat(formatStyle,Locale.getDefault());Date date = new Date(time);String s = format.format(date);return s;}/** * 指定字符串转为时间戳 * * @param time * 指定字符串 * @param formatStyle * 字符串格式 */public static long time2long(String time, String formatStyle) {SimpleDateFormat format = new SimpleDateFormat(formatStyle,Locale.getDefault());Date date;try {date = format.parse(time);} catch (ParseException e) {date = new Date();//如果格式错误就返回当前时间搓e.printStackTrace();Logs.d(e.getMessage());}return date.getTime();}/** * 根据一段时间戳返回耗时时间字符 */public static String long2time(long time) {String str = "";long h = 60 * 60 * 1000;long m = 60 * 1000;long s = 1000;if (time > h) {long hh = time / h;long mm = time / m;long ss = time / s;str = hh + "小时" + mm + "分钟" + ss + "秒";} else if (time > m) {long mm = time / m;long ss = time / s;str = mm + "分钟" + ss + "秒";} else {long ss = time / s;str = ss + "秒";}return str;}}
long time:就是时间搓
String formatString:自定义的时间字符串格式 如:
/* 时间格式 */ public final static String cformatDay = "yyyy年MM月dd日"; public final static String cformatD = "M月d日"; public final static String cformatsecond = "yyyy年MM月dd日HH时mm分ss秒"; public final static String formatminute = "HH:mm"; public final static String formatsecond = "yyyy-MM-dd HH:mm:ss";
阅读全文
0 0
- 安卓时间戳和时间字符串转换工具类
- java和安卓时间转换工具
- 安卓工具类集合—— 1 时间、时间戳转换工具
- mysql时间字符串和时间戳转换
- Android工具类——TimeUtils时间戳与时间字符串相互转换
- 时间转换工具类
- 时间转换工具类
- 时间转换工具类
- 安卓时间工具类,计算时间差,比较当前时间
- 字符串和时间戳相互转换
- JavaScript日期字符串和时间戳转换
- js字符串和时间戳互相转换
- shell时间(字符串和时间戳转换)
- XZ_iOS之时间戳和时间字符串的一个转换
- shell时间(字符串和时间戳转换)
- python 字符串和时间转换
- 字符串和时间转换time_t
- 字符串和时间转换time_t
- 调整搜索二叉树中两个错误结点
- JAVA和PHP版CAS客户端集成
- apache和nginx web服务器优缺点对比
- Struts2中<s:doubleselect>标签从MySQL数据库中查询出数据实现二级联动
- linux vi编辑
- 安卓时间戳和时间字符串转换工具类
- React Mobx使用基础
- Fast single shot detection and pose estimation 论文笔记
- Hadoop学习笔记—22.Hadoop2.x环境搭建与配置
- 关于面试的一些知识点
- 灰度变换与空间滤波
- java语言程序设计 第十二章 (12.18、12.19、12.20、12.21、12.22)
- 初识React
- spring boot 计划任务,定时任务的注解的使用@Scheduled@EnableScheduling