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 WebView无法加载H5页面的常见问题和解决方法

《AndroidWebView无法加载H5页面的常见问题和解决方法》AndroidWebView是一种视图组件,使得Android应用能够显示网页内容,它基于Chromium,具备现代浏览器的许多功... 目录1. WebView 简介2. 常见问题3. 网络权限设置4. 启用 JavaScript5. D

Win32下C++实现快速获取硬盘分区信息

《Win32下C++实现快速获取硬盘分区信息》这篇文章主要为大家详细介绍了Win32下C++如何实现快速获取硬盘分区信息,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 实现代码CDiskDriveUtils.h#pragma once #include <wtypesbase

Python如何在Word中生成多种不同类型的图表

《Python如何在Word中生成多种不同类型的图表》Word文档中插入图表不仅能直观呈现数据,还能提升文档的可读性和专业性,本文将介绍如何使用Python在Word文档中创建和自定义各种图表,需要的... 目录在Word中创建柱形图在Word中创建条形图在Word中创建折线图在Word中创建饼图在Word

SpringBoot接收JSON类型的参数方式

《SpringBoot接收JSON类型的参数方式》:本文主要介绍SpringBoot接收JSON类型的参数方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、jsON二、代码准备三、Apifox操作总结一、JSON在学习前端技术时,我们有讲到过JSON,而在

Android如何获取当前CPU频率和占用率

《Android如何获取当前CPU频率和占用率》最近在优化App的性能,需要获取当前CPU视频频率和占用率,所以本文小编就来和大家总结一下如何在Android中获取当前CPU频率和占用率吧... 最近在优化 App 的性能,需要获取当前 CPU视频频率和占用率,通过查询资料,大致思路如下:目前没有标准的

MySQL 日期时间格式化函数 DATE_FORMAT() 的使用示例详解

《MySQL日期时间格式化函数DATE_FORMAT()的使用示例详解》`DATE_FORMAT()`是MySQL中用于格式化日期时间的函数,本文详细介绍了其语法、格式化字符串的含义以及常见日期... 目录一、DATE_FORMAT()语法二、格式化字符串详解三、常见日期时间格式组合四、业务场景五、总结一、

golang获取prometheus数据(prometheus/client_golang包)

《golang获取prometheus数据(prometheus/client_golang包)》本文主要介绍了使用Go语言的prometheus/client_golang包来获取Prometheu... 目录1. 创建链接1.1 语法1.2 完整示例2. 简单查询2.1 语法2.2 完整示例3. 范围值

javaScript在表单提交时获取表单数据的示例代码

《javaScript在表单提交时获取表单数据的示例代码》本文介绍了五种在JavaScript中获取表单数据的方法:使用FormData对象、手动提取表单数据、使用querySelector获取单个字... 方法 1:使用 FormData 对象FormData 是一个方便的内置对象,用于获取表单中的键值

Android开发中gradle下载缓慢的问题级解决方法

《Android开发中gradle下载缓慢的问题级解决方法》本文介绍了解决Android开发中Gradle下载缓慢问题的几种方法,本文给大家介绍的非常详细,感兴趣的朋友跟随小编一起看看吧... 目录一、网络环境优化二、Gradle版本与配置优化三、其他优化措施针对android开发中Gradle下载缓慢的问

Rust中的BoxT之堆上的数据与递归类型详解

《Rust中的BoxT之堆上的数据与递归类型详解》本文介绍了Rust中的BoxT类型,包括其在堆与栈之间的内存分配,性能优势,以及如何利用BoxT来实现递归类型和处理大小未知类型,通过BoxT,Rus... 目录1. Box<T> 的基础知识1.1 堆与栈的分工1.2 性能优势2.1 递归类型的问题2.2