Android日期工具类
Posted 码上夏雨
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android日期工具类相关的知识,希望对你有一定的参考价值。
安卓日期工具类,用以获取时间信息
快速开始
点击DateUtils查看源码,欢迎Star,fork,并在issue提出改进意见
implementation 'io.github.sakurajimamaii:VastTools:0.0.3'
概括
类
Class Name |
---|
DateFormat |
DateUtils |
字段
Constant Type | Constant Field |
---|---|
Time Format | DATE_FORMAT TIME_FORMAT FORMAT_YYYY_MM FORMAT_YYYY FORMAT_HH_MM FORMAT_HH_MM_SS FORMAT_MM_SS FORMAT_MM_DD_HH_MM FORMAT_MM_DD_HH_MM_SS FORMAT_YYYY_MM_DD_HH_MM FORMAT_YYYY2MM2DD FORMAT_YYYY2MM2DD_HH_MM FORMAT_MMCDD_HH_MM FORMAT_MMCDD FORMAT_YYYYCMMCDD |
GMT Format | GMT_PLUS_ZONE GMT_PLUS_ONE GMT_PLUS_TWO GMT_PLUS_THREE GMT_PLUS_FOUR GMT_PLUS_FIVE GMT_PLUS_SIX GMT_PLUS_SEVEN GMT_PLUS_EIGHT GMT_PLUS_NINE GMT_PLUS_TEN GMT_PLUS_ELEVEN GMT_PLUS_TWELVE GMT_MINUS_ONE GMT_MINUS_TWO GMT_MINUS_THREE GMT_MINUS_FOUR GMT_MINUS_FIVE GMT_MINUS_SIX GMT_MINUS_SEVEN GMT_MINUS_EIGHT GMT_MINUS_NINE GMT_MINUS_TEN GMT_MINUS_ELEVEN GMT_MINUS_TWELVE |
注解
Annotation Name | Use for |
---|---|
DateFormatString | 用于检查日期字符串格式可引用的常量 |
GmtFormatString | 用于检查GMT时区字符串可引用的常量 |
YearFormatString | 用于检查年份字符串格式可引用的常量 |
公有属性
Param Name | Param Class |
---|---|
locale | Locale |
currentTime | String |
currentTimeZone | String |
公有方法
Returns | Method |
---|---|
Date | fun minDate() |
SimpleDateFormat | fun datetimeFormat(dateFormat: String) |
Date? | fun datetimeFromString(timeString: String,timeStringFormat: String) |
String | fun datetimeToString(date: Date,dateFormat: String) |
String | fun dateTimeToGMT(gmtFormat: String,dateFormat: String) |
String | fun dateTimeFromGMT(utcTime: String,dateFormat: String) |
String | fun weekStartTime(yearFormat: String) |
String | fun weekEndTime(yearFormat: String) |
String | fun getWeekStartTime(yearFormat: String) |
String | fun getWeekEndTime(yearFormat: String) |
Unit | fun setLocale(locale: Locale) |
类
DateFormat
提供了常用的日期字符串格式和GMT时区字符串常量,以及注解类用以检查字段可以引用的常量
DateUtils
提供了获取时间的方法
字段
日期格式字段
目前支持的日期格式
public static final String DATE_FORMAT = "yyyy-MM-dd";
public static final String TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
public static final String FORMAT_YYYY_MM = "yyyy-MM";
public static final String FORMAT_YYYY = "yyyy";
public static final String FORMAT_HH_MM = "HH:mm";
public static final String FORMAT_HH_MM_SS = "HH:mm:ss";
public static final String FORMAT_MM_SS = "mm:ss";
public static final String FORMAT_MM_DD_HH_MM = "MM-dd HH:mm";
public static final String FORMAT_MM_DD_HH_MM_SS = "MM-dd HH:mm:ss";
public static final String FORMAT_YYYY_MM_DD_HH_MM = "yyyy-MM-dd HH:mm";
public static final String FORMAT_YYYY2MM2DD = "yyyy.MM.dd";
public static final String FORMAT_YYYY2MM2DD_HH_MM = "yyyy.MM.dd HH:mm";
public static final String FORMAT_MMCDD_HH_MM = "MM月dd日 HH:mm";
public static final String FORMAT_MMCDD = "MM月dd日";
public static final String FORMAT_YYYYCMMCDD = "yyyy年MM月dd日";
GMT时区字段
目前支持的时区
public static final String GMT_PLUS_ZONE = "GMT+00:00";
public static final String GMT_PLUS_ONE = "GMT+01:00";
public static final String GMT_PLUS_TWO = "GMT+02:00";
public static final String GMT_PLUS_THREE = "GMT+03:00";
public static final String GMT_PLUS_FOUR = "GMT+04:00";
public static final String GMT_PLUS_FIVE = "GMT+05:00";
public static final String GMT_PLUS_SIX = "GMT+06:00";
public static final String GMT_PLUS_SEVEN = "GMT+07:00";
public static final String GMT_PLUS_EIGHT = "GMT+08:00";
public static final String GMT_PLUS_NINE = "GMT+09:00";
public static final String GMT_PLUS_TEN = "GMT+10:00";
public static final String GMT_PLUS_ELEVEN = "GMT+11:00";
public static final String GMT_PLUS_TWELVE = "GMT+12:00";
public static final String GMT_MINUS_ONE = "GMT-01:00";
public static final String GMT_MINUS_TWO = "GMT-02:00";
public static final String GMT_MINUS_THREE = "GMT-03:00";
public static final String GMT_MINUS_FOUR = "GMT-04:00";
public static final String GMT_MINUS_FIVE = "GMT-05:00";
public static final String GMT_MINUS_SIX = "GMT-06:00";
public static final String GMT_MINUS_SEVEN = "GMT-07:00";
public static final String GMT_MINUS_EIGHT = "GMT-08:00";
public static final String GMT_MINUS_NINE = "GMT-09:00";
public static final String GMT_MINUS_TEN = "GMT-10:00";
public static final String GMT_MINUS_ELEVEN = "GMT-11:00";
public static final String GMT_MINUS_TWELVE = "GMT-12:00";
公有属性
locale
代表特定的地理、政治或文化区域。
currentTime
获取到的当前时间,格式为 yyyy-MM-dd HH:mm:ss
currentTimeZone
获取到的当前所处的时区,形如 GMT+08:00
公有方法
fun minDate(): Date
获取最小日期的 Date
对象
datetimeFormat
@JvmOverloads
fun datetimeFormat(@DateFormat.DateFormatString dateFormat:String = TIME_FORMAT): SimpleDateFormat
参数
dateFormat:String
你提供的日期格式,默认值是 TIME_FORMAT
返回值
SimpleDateFormat
返回日期时间格式化对象
datetimeFromString
@JvmOverloads
fun datetimeFromString(timeString: String,@DateFormat.DateFormatString timeStringFormat: String = TIME_FORMAT): Date?
按照 timeStringFormat
的格式从 timeString
中解析出日期和时间。
datetimeToString
@JvmOverloads
fun datetimeToString(
date: Date = Date(),
@DateFormat.DateFormatString dateFormat: String = TIME_FORMAT
): String
按照 dateFormat
的格式将 date
解析为日期时间字符串。
如果不设置 date
或 dateFormat
,默认会以dateFormat
格式解析当前时间。
dateTimeToGMT
@JvmOverloads
fun dateTimeToGMT(
@DateFormat.GmtFormatString gmtFormat: String = currentTimeZone,
@DateFormat.DateFormatString dateFormat: String = TIME_FORMAT
): String
根据 dateFormat
格式,返回 gmtFormat
给出的时区中的时间字符串。
如果不设置 gmtFormat
或 dateFormat
,默认会以 TIME_FORMAT
格式解析当前本地时间。
dateTimeFromGMT
fun dateTimeFromGMT(utcTime: String,@DateFormat.DateFormatString dateFormat: String = TIME_FORMAT): String
通过解析 dateFormat
格式的 utcTime
获取当前本地时间字符串。
weekStartTime
fun weekStartTime(@DateFormat.YearFormatString yearFormat: String = DATE_FORMAT): String
获取一周的开始时间戳,格式是 yearFormat
。星期一是一周的第一天。
weekEndTime
fun weekEndTime(@DateFormat.YearFormatString yearFormat: String = DATE_FORMAT): String
获取一周的结束时间戳,格式是 yearFormat
。星期一是一周的第一天。
getWeekStartTime
@JvmOverloads
fun getWeekStartTime(
calendar: Calendar = Calendar.getInstance(),
@DateFormat.YearFormatString yearFormat: String = DATE_FORMAT
): String
获取一周的开始时间戳,格式是 yearFormat
。 SUNDAY 是一周的第一天。
getWeekEndTime
@JvmOverloads
fun getWeekEndTime(
calendar: Calendar = Calendar.getInstance(),
@DateFormat.YearFormatString yearFormat: String = DATE_FORMAT
): String
获取一周的结束时间戳,格式是 yearFormat
。 SUNDAY 是一周的第一天。
setLocale
fun setLocale(locale: Locale)
参数
locale: Locale
你要设置的locale
以上是关于Android日期工具类的主要内容,如果未能解决你的问题,请参考以下文章
Android工具类 日期工具类 (番外篇) 格式时间 - 分 秒 毫 时间间隔 判断日期前后 星期