Android日期工具类

1,062 阅读3分钟

安卓日期工具类,用以获取时间信息

快速开始

点击DateUtils查看源码,欢迎Star,fork,并在issue提出改进意见

implementation 'io.github.sakurajimamaii:VastTools:0.0.3'

概括

Class Name
DateFormat
DateUtils

字段

Constant TypeConstant Field
Time FormatDATE_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 FormatGMT_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 NameUse for
DateFormatString用于检查日期字符串格式可引用的常量
GmtFormatString用于检查GMT时区字符串可引用的常量
YearFormatString用于检查年份字符串格式可引用的常量

公有属性

Param NameParam Class
localeLocale
currentTimeString
currentTimeZoneString

公有方法

ReturnsMethod
Datefun minDate()
SimpleDateFormatfun datetimeFormat(dateFormat: String)
Date?fun datetimeFromString(timeString: String,timeStringFormat: String)
Stringfun datetimeToString(date: Date,dateFormat: String)
Stringfun dateTimeToGMT(gmtFormat: String,dateFormat: String)
Stringfun dateTimeFromGMT(utcTime: String,dateFormat: String)
Stringfun weekStartTime(yearFormat: String)
Stringfun weekEndTime(yearFormat: String)
Stringfun getWeekStartTime(yearFormat: String)
Stringfun getWeekEndTime(yearFormat: String)
Unitfun 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

公有方法

print

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 解析为日期时间字符串。

如果不设置 datedateFormat,默认会以dateFormat格式解析当前时间

dateTimeToGMT

@JvmOverloads
fun dateTimeToGMT(
    @DateFormat.GmtFormatString gmtFormat: String = currentTimeZone,
    @DateFormat.DateFormatString dateFormat: String = TIME_FORMAT
): String

根据 dateFormat 格式,返回 gmtFormat 给出的时区中的时间字符串。

如果不设置 gmtFormatdateFormat,默认会以 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