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

相关文章

MyBatis Plus实现时间字段自动填充的完整方案

《MyBatisPlus实现时间字段自动填充的完整方案》在日常开发中,我们经常需要记录数据的创建时间和更新时间,传统的做法是在每次插入或更新操作时手动设置这些时间字段,这种方式不仅繁琐,还容易遗漏,... 目录前言解决目标技术栈实现步骤1. 实体类注解配置2. 创建元数据处理器3. 服务层代码优化填充机制详

python获取指定名字的程序的文件路径的两种方法

《python获取指定名字的程序的文件路径的两种方法》本文主要介绍了python获取指定名字的程序的文件路径的两种方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要... 最近在做项目,需要用到给定一个程序名字就可以自动获取到这个程序在Windows系统下的绝对路径,以下

C++统计函数执行时间的最佳实践

《C++统计函数执行时间的最佳实践》在软件开发过程中,性能分析是优化程序的重要环节,了解函数的执行时间分布对于识别性能瓶颈至关重要,本文将分享一个C++函数执行时间统计工具,希望对大家有所帮助... 目录前言工具特性核心设计1. 数据结构设计2. 单例模式管理器3. RAII自动计时使用方法基本用法高级用法

SpringBoot 获取请求参数的常用注解及用法

《SpringBoot获取请求参数的常用注解及用法》SpringBoot通过@RequestParam、@PathVariable等注解支持从HTTP请求中获取参数,涵盖查询、路径、请求体、头、C... 目录SpringBoot 提供了多种注解来方便地从 HTTP 请求中获取参数以下是主要的注解及其用法:1

Python中Json和其他类型相互转换的实现示例

《Python中Json和其他类型相互转换的实现示例》本文介绍了在Python中使用json模块实现json数据与dict、object之间的高效转换,包括loads(),load(),dumps()... 项目中经常会用到json格式转为object对象、dict字典格式等。在此做个记录,方便后续用到该方

C# LiteDB处理时间序列数据的高性能解决方案

《C#LiteDB处理时间序列数据的高性能解决方案》LiteDB作为.NET生态下的轻量级嵌入式NoSQL数据库,一直是时间序列处理的优选方案,本文将为大家大家简单介绍一下LiteDB处理时间序列数... 目录为什么选择LiteDB处理时间序列数据第一章:LiteDB时间序列数据模型设计1.1 核心设计原则

Android协程高级用法大全

《Android协程高级用法大全》这篇文章给大家介绍Android协程高级用法大全,本文结合实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友跟随小编一起学习吧... 目录1️⃣ 协程作用域(CoroutineScope)与生命周期绑定Activity/Fragment 中手

python中的显式声明类型参数使用方式

《python中的显式声明类型参数使用方式》文章探讨了Python3.10+版本中类型注解的使用,指出FastAPI官方示例强调显式声明参数类型,通过|操作符替代Union/Optional,可提升代... 目录背景python函数显式声明的类型汇总基本类型集合类型Optional and Union(py

MySQL中查询和展示LONGBLOB类型数据的技巧总结

《MySQL中查询和展示LONGBLOB类型数据的技巧总结》在MySQL中LONGBLOB是一种二进制大对象(BLOB)数据类型,用于存储大量的二进制数据,:本文主要介绍MySQL中查询和展示LO... 目录前言1. 查询 LONGBLOB 数据的大小2. 查询并展示 LONGBLOB 数据2.1 转换为十

MySQL按时间维度对亿级数据表进行平滑分表

《MySQL按时间维度对亿级数据表进行平滑分表》本文将以一个真实的4亿数据表分表案例为基础,详细介绍如何在不影响线上业务的情况下,完成按时间维度分表的完整过程,感兴趣的小伙伴可以了解一下... 目录引言一、为什么我们需要分表1.1 单表数据量过大的问题1.2 分表方案选型二、分表前的准备工作2.1 数据评估