Android获取各式时间类型

2023-12-18 00:38

本文主要是介绍Android获取各式时间类型,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

在项目中需要获取各式各样的时间,而且也会有很多地方会调用到这些方法,所以我将它们归集于一个文件中,方便之后的使用。

现在我们需要了解一些相对基础获取时间的方法。

1.获取当前日历对象:

Calendar calendar = Calendar.getInstance();

2.获取当前时区下日期时间对应的时间戳:

calendar.getTimeInMillis();

3.获取标准格林尼治时间下日期时间对应的时间戳:

long unixTime = calendar.getTimeInMillis();
unixTime - TimeZone.getDefault().getRawOffset();

4.获取当前日期对象:

Date date = new Date();

5.获取当前时区下日期时间对应的时间戳:

date.getTimeInMillis();

6.设置日期时间格式:

SimpleDateFormat format = new  SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

7.获取当前时区下日期时间对应的时间戳:

format.format(date);

现在来实现具体的方法。

1.获取时间戳:

 public static long getTime() {Calendar calendar = Calendar.getInstance();// 获取当前日历对象long unixTime = calendar.getTimeInMillis();// 获取当前时区下日期时间对应的时间戳return unixTime;}

2.获取标准的时间:

 public static String getStandardTime() {SimpleDateFormat formatter = new SimpleDateFormat(BaseApplication.getInstance().getString(R.string.date_show_type_one));Date curDate = new Date(System.currentTimeMillis());// 获取当前时间return formatter.format(curDate);}

3.获取与现在时间的时间差(秒):

public static int getDurationSecond(String time) {int durationSecond = 0;SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");Date date;try {date = df.parse(time);MyLog.i("TimeUtils getDurationSecond Date=" + new Date().toString());durationSecond = (int) ((new Date().getTime() - date.getTime()) / 1000);} catch (Exception e) {MyLog.e("TimeUtils getDurationSecond error=" + e);}return durationSecond;}

4.获取时间差:

public static String getDuration(String one, String two) {String duration = "";SimpleDateFormat df = new SimpleDateFormat(<span style="font-family: SimHei;">"yyyy-MM-dd HH:mm:ss"</span>);Date date1;Date date2;try {date1 = df.parse(one);date2 = df.parse(two);int l = (int) ((date2.getTime() - date1.getTime()) / 1000 / 60);if (l > 60) {int hr = l / 60;int min = l % 60;duration = hr + "小时" + min + "分钟";} else {duration = l + "分钟";}} catch (Exception e) {e.printStackTrace();}return duration;}

完整代码展示:

public class MyTimeUtils {//获取时间戳public static long getTime() {Calendar calendar = Calendar.getInstance();// 获取当前日历对象long unixTime = calendar.getTimeInMillis();// 获取当前时区下日期时间对应的时间戳return unixTime;}public static String getTimeString() {return Long.toString(new Date().getTime());}//获取标准时间public static String getStandardTime() {SimpleDateFormat formatter = new SimpleDateFormat(BaseApplication.getInstance().getString(R.string.date_show_type_one));Date curDate = new Date(System.currentTimeMillis());// 获取当前时间return formatter.format(curDate);}// 获取与现在时间的时间差(秒)public static int getDurationSecond(String time) {int durationSecond = 0;SimpleDateFormat df = new SimpleDateFormat(<span style="font-family: SimHei;">"yyyy-MM-dd HH:mm:ss"</span>);Date date;try {date = df.parse(time);MyLog.i("TimeUtils getDurationSecond Date=" + new Date().toString());durationSecond = (int) ((new Date().getTime() - date.getTime()) / 1000);} catch (Exception e) {MyLog.e("TimeUtils getDurationSecond error=" + e);}return durationSecond;}// 获取时间差public static String getDuration(String one, String two) {String duration = "";SimpleDateFormat df = new SimpleDateFormat(<span style="font-family: SimHei;">"yyyy-MM-dd HH:mm:ss"</span><span style="font-family: SimHei;">);</span>Date date1;Date date2;try {date1 = df.parse(one);date2 = df.parse(two);int l = (int) ((date2.getTime() - date1.getTime()) / 1000 / 60);if (l > 60) {int hr = l / 60;int min = l % 60;duration = <span style="font-family: SimHei;">hr + "小时" + min + "分钟"</span>;} else {duration = <span style="font-family: SimHei;">l + "分钟";</span>}} catch (Exception e) {e.printStackTrace();}return duration;}// 获取与当前时间差public static String getcurDuration(String one) {String duration = "";SimpleDateFormat df = new SimpleDateFormat(<span style="font-family: SimHei;">"yyyy-MM-dd HH:mm:ss"</span>);Date date1;Date date2;try {date1 = df.parse(one);date2 = new Date();int l = (int) ((date2.getTime() - date1.getTime()) / 1000 / 60);if (l > 60) {int hr = l / 60;int min = l % 60;duration = <span style="font-family: SimHei;">hr + "小时" + min + "分钟"</span><span style="font-family: SimHei;">;</span>} else {duration =<span style="font-family: SimHei;"> l + "分钟";</span>}} catch (Exception e) {e.printStackTrace();}return duration;}/*** @return格式化当前日期和时间为字符串*/public static String mCurrentTime() {SimpleDateFormat df = new SimpleDateFormat(<span style="font-family: SimHei;">"yyyy-MM-dd HH:mm:ss"</span>);String currenttime = df.format(new Date());return currenttime;}public static String parseBangTime(long time) {MyLog.out("time==>" + time);String timeTemp = "";if (time < 60) {timeTemp = time + BaseApplication.getInstance().getString(R.string.seconds_before);} else if (time < (60 * 60)) {timeTemp = time / 60 + BaseApplication.getInstance().getString(R.string.minutes_before);} else if (time < (3600 * 24)) {timeTemp = time / 3600 + BaseApplication.getInstance().getString(R.string.hour_before);} else if (time < (60 * 60 * 24 * 30)) {timeTemp = time / (3600 * 24) + BaseApplication.getInstance().getString(R.string.today_before);} else {timeTemp = time / (3600 * 24 * 30) + BaseApplication.getInstance().getString(R.string.month_before);}return timeTemp;}public static String getTimeStamp() {SimpleDateFormat dateFormat = new SimpleDateFormat(BaseApplication.getInstance().getString(R.string.date_show_type_two));String timeStamp = dateFormat.format(new Date());MyLog.e("getTimeStamp=" + timeStamp);return timeStamp;}public static String getCurrentDate(){SimpleDateFormat df = new SimpleDateFormat(BaseApplication.getInstance().getString(R.string.date_show));String currentDate = df.format(new Date());return currentDate;}
}

 

 

 

这篇关于Android获取各式时间类型的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/506430

相关文章

Python使用OpenCV实现获取视频时长的小工具

《Python使用OpenCV实现获取视频时长的小工具》在处理视频数据时,获取视频的时长是一项常见且基础的需求,本文将详细介绍如何使用Python和OpenCV获取视频时长,并对每一行代码进行深入解析... 目录一、代码实现二、代码解析1. 导入 OpenCV 库2. 定义获取视频时长的函数3. 打开视频文

go中的时间处理过程

《go中的时间处理过程》:本文主要介绍go中的时间处理过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1 获取当前时间2 获取当前时间戳3 获取当前时间的字符串格式4 相互转化4.1 时间戳转时间字符串 (int64 > string)4.2 时间字符串转时间

Golang如何对cron进行二次封装实现指定时间执行定时任务

《Golang如何对cron进行二次封装实现指定时间执行定时任务》:本文主要介绍Golang如何对cron进行二次封装实现指定时间执行定时任务问题,具有很好的参考价值,希望对大家有所帮助,如有错误... 目录背景cron库下载代码示例【1】结构体定义【2】定时任务开启【3】使用示例【4】控制台输出总结背景

MySQL 获取字符串长度及注意事项

《MySQL获取字符串长度及注意事项》本文通过实例代码给大家介绍MySQL获取字符串长度及注意事项,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录mysql 获取字符串长度详解 核心长度函数对比⚠️ 六大关键注意事项1. 字符编码决定字节长度2

python3如何找到字典的下标index、获取list中指定元素的位置索引

《python3如何找到字典的下标index、获取list中指定元素的位置索引》:本文主要介绍python3如何找到字典的下标index、获取list中指定元素的位置索引问题,具有很好的参考价值,... 目录enumerate()找到字典的下标 index获取list中指定元素的位置索引总结enumerat

Android DataBinding 与 MVVM使用详解

《AndroidDataBinding与MVVM使用详解》本文介绍AndroidDataBinding库,其通过绑定UI组件与数据源实现自动更新,支持双向绑定和逻辑运算,减少模板代码,结合MV... 目录一、DataBinding 核心概念二、配置与基础使用1. 启用 DataBinding 2. 基础布局

Android ViewBinding使用流程

《AndroidViewBinding使用流程》AndroidViewBinding是Jetpack组件,替代findViewById,提供类型安全、空安全和编译时检查,代码简洁且性能优化,相比Da... 目录一、核心概念二、ViewBinding优点三、使用流程1. 启用 ViewBinding (模块级

SpringMVC高效获取JavaBean对象指南

《SpringMVC高效获取JavaBean对象指南》SpringMVC通过数据绑定自动将请求参数映射到JavaBean,支持表单、URL及JSON数据,需用@ModelAttribute、@Requ... 目录Spring MVC 获取 JavaBean 对象指南核心机制:数据绑定实现步骤1. 定义 Ja

C++中RAII资源获取即初始化

《C++中RAII资源获取即初始化》RAII通过构造/析构自动管理资源生命周期,确保安全释放,本文就来介绍一下C++中的RAII技术及其应用,具有一定的参考价值,感兴趣的可以了解一下... 目录一、核心原理与机制二、标准库中的RAII实现三、自定义RAII类设计原则四、常见应用场景1. 内存管理2. 文件操

C++ 函数 strftime 和时间格式示例详解

《C++函数strftime和时间格式示例详解》strftime是C/C++标准库中用于格式化日期和时间的函数,定义在ctime头文件中,它将tm结构体中的时间信息转换为指定格式的字符串,是处理... 目录C++ 函数 strftipythonme 详解一、函数原型二、功能描述三、格式字符串说明四、返回值五