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

相关文章

Android 12解决push framework.jar无法开机的方法小结

《Android12解决pushframework.jar无法开机的方法小结》:本文主要介绍在Android12中解决pushframework.jar无法开机的方法,包括编译指令、框架层和s... 目录1. android 编译指令1.1 framework层的编译指令1.2 替换framework.ja

Android开发环境配置避坑指南

《Android开发环境配置避坑指南》本文主要介绍了Android开发环境配置过程中遇到的问题及解决方案,包括VPN注意事项、工具版本统一、Gerrit邮箱配置、Git拉取和提交代码、MergevsR... 目录网络环境:VPN 注意事项工具版本统一:android Studio & JDKGerrit的邮

Android实现定时任务的几种方式汇总(附源码)

《Android实现定时任务的几种方式汇总(附源码)》在Android应用中,定时任务(ScheduledTask)的需求几乎无处不在:从定时刷新数据、定时备份、定时推送通知,到夜间静默下载、循环执行... 目录一、项目介绍1. 背景与意义二、相关基础知识与系统约束三、方案一:Handler.postDel

Java controller接口出入参时间序列化转换操作方法(两种)

《Javacontroller接口出入参时间序列化转换操作方法(两种)》:本文主要介绍Javacontroller接口出入参时间序列化转换操作方法,本文给大家列举两种简单方法,感兴趣的朋友一起看... 目录方式一、使用注解方式二、统一配置场景:在controller编写的接口,在前后端交互过程中一般都会涉及

python处理带有时区的日期和时间数据

《python处理带有时区的日期和时间数据》这篇文章主要为大家详细介绍了如何在Python中使用pytz库处理时区信息,包括获取当前UTC时间,转换为特定时区等,有需要的小伙伴可以参考一下... 目录时区基本信息python datetime使用timezonepandas处理时区数据知识延展时区基本信息

SpringBoot整合mybatisPlus实现批量插入并获取ID详解

《SpringBoot整合mybatisPlus实现批量插入并获取ID详解》这篇文章主要为大家详细介绍了SpringBoot如何整合mybatisPlus实现批量插入并获取ID,文中的示例代码讲解详细... 目录【1】saveBATch(一万条数据总耗时:2478ms)【2】集合方式foreach(一万条数

python获取网页表格的多种方法汇总

《python获取网页表格的多种方法汇总》我们在网页上看到很多的表格,如果要获取里面的数据或者转化成其他格式,就需要将表格获取下来并进行整理,在Python中,获取网页表格的方法有多种,下面就跟随小编... 目录1. 使用Pandas的read_html2. 使用BeautifulSoup和pandas3.

SpringBoot UserAgentUtils获取用户浏览器的用法

《SpringBootUserAgentUtils获取用户浏览器的用法》UserAgentUtils是于处理用户代理(User-Agent)字符串的工具类,一般用于解析和处理浏览器、操作系统以及设备... 目录介绍效果图依赖封装客户端工具封装IP工具实体类获取设备信息入库介绍UserAgentUtils

Android使用ImageView.ScaleType实现图片的缩放与裁剪功能

《Android使用ImageView.ScaleType实现图片的缩放与裁剪功能》ImageView是最常用的控件之一,它用于展示各种类型的图片,为了能够根据需求调整图片的显示效果,Android提... 目录什么是 ImageView.ScaleType?FIT_XYFIT_STARTFIT_CENTE

Python的time模块一些常用功能(各种与时间相关的函数)

《Python的time模块一些常用功能(各种与时间相关的函数)》Python的time模块提供了各种与时间相关的函数,包括获取当前时间、处理时间间隔、执行时间测量等,:本文主要介绍Python的... 目录1. 获取当前时间2. 时间格式化3. 延时执行4. 时间戳运算5. 计算代码执行时间6. 转换为指