Android date tool class to get time information

Quick start

Click on DateUtils to see the source code, welcome Star, fork, and suggest improvements in issue

implementation 'the IO. Making. Sakurajimamaii: VastTools: 0.0.3'
Copy the code

summary

class

Class Name
DateFormat
DateUtils

field

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

annotations

Annotation Name Use for
DateFormatString A constant used to check the date-string format for referencability
GmtFormatString Used to checkGMTTime zone string referable constant
YearFormatString A constant used to check the year string format for referencability

Public attribute

Param Name Param Class
locale Locale
currentTime String
currentTimeZone String

Public methods

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)

class

DateFormat

Common date string formats and GMT time zone string constants are provided, as well as annotation classes to check for constants that fields can reference

DateUtils

Provides a way to get time

field

Date format field

Date format currently supported

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 month DD day HH: MM";
public static final String FORMAT_MMCDD = "MM month DD day";
public static final String FORMAT_YYYYCMMCDD = "Yyyy year MM month DD day";
Copy the code

GMT time zone field

Current supported time zone

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";
Copy the code

Public attribute

locale

Represents a particular geographic, political, or cultural area.

currentTime

The current time is in the format yyyY-MM-DD HH: MM :ss

currentTimeZone

Obtain the current time zone, in the format of GMT+08:00

Public methods

print

fun minDate(a): Date
Copy the code

Gets the Date object of the smallest Date

datetimeFormat

@JvmOverloads
fun datetimeFormat(@DateFormat.DateFormatString dateFormat:String = TIME_FORMAT): SimpleDateFormat
Copy the code

parameter

DateFormat :String specifies the dateFormat you provide. The default value is TIME_FORMAT

The return value

SimpleDateFormat returns a date-time formatting object

datetimeFromString

@JvmOverloads
fun datetimeFromString(timeString: String.@DateFormat.DateFormatString timeStringFormat: String = TIME_FORMAT): Date?
Copy the code

Parse the date and time from timeString in the format timeStringFormat.

datetimeToString

@JvmOverloads
fun datetimeToString(
    date: Date = Date().@DateFormat.DateFormatString dateFormat: String = TIME_FORMAT
): String
Copy the code

Parse date to a date-time string in dateFormat.

If you do not set date or dateFormat, the current time is parsed in dateFormat by default.

dateTimeToGMT

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

Returns the time string in the time zone given by gmtFormat, based on the dateFormat format.

If gmtFormat or dateFormat is not set, the current local time is parsed in TIME_FORMAT by default.

dateTimeFromGMT

fun dateTimeFromGMT(utcTime: String.@DateFormat.DateFormatString dateFormat: String = TIME_FORMAT): String
Copy the code

Gets the current local time string by parsing utcTime in dateFormat.

weekStartTime

fun weekStartTime(@DateFormat.YearFormatString yearFormat: String = DATE_FORMAT): String
Copy the code

Gets the start time stamp of the week in yearFormat. Monday is the first day of the week.

weekEndTime

fun weekEndTime(@DateFormat.YearFormatString yearFormat: String = DATE_FORMAT): String
Copy the code

Gets the end of the week timestamp in yearFormat. Monday is the first day of the week.

getWeekStartTime

@JvmOverloads
fun getWeekStartTime(
    calendar: Calendar = Calendar.getInstance().@DateFormat.YearFormatString yearFormat: String = DATE_FORMAT
): String
Copy the code

Gets the start time stamp of the week in yearFormat. SUNDAY is the first day of the week.

getWeekEndTime

@JvmOverloads
fun getWeekEndTime(
    calendar: Calendar = Calendar.getInstance().@DateFormat.YearFormatString yearFormat: String = DATE_FORMAT
): String
Copy the code

Gets the end of the week timestamp in yearFormat. SUNDAY is the first day of the week.

setLocale

fun setLocale(locale: Locale)
Copy the code

parameter

Locale: locale the locale that you want to set