public class Dates extends Object
标准时间:GMT时间,也叫格林威治平时,也叫 UTC时间。 Java 6 :
Date:
表示一个瞬时值,单位是毫秒。它的结果是一个标准的GMT瞬时值。它和时区(timezone)地域(Locale)没有关系。
在同一时刻,地球上两个不同时区国家的人使用Date API获取到的毫秒数完全一样的。
Timezone:
表示时区,其实是各个时区与GMT的毫秒数之差, 也即Offset。
因为不同时区的时间看到的是不一样的,但是通过Date获取到的毫秒数是一样的,怎么做到的呢?
Date#toString 或者 SimpleDateFormat 都会根据Timezone 和 Locale 来进行处理,具体处理如下:
1) 使用GMT 毫秒数 + Timezone.offset 计算出当地的实际毫秒数
2) 格式化显示时会使用本土语言(Locale)进行处理
Calender:
日历,它综合了 GMT millis(Date), timezone, locale,也就是说它是用来表示某个时区的某个国家的时间。
并在此基础上提供了时间的加减运算。
setTimeInMillis,setTimeInMillis 这两个方法的参数或者返回值是 UTC millis。
提供的时间的加减运算都是针对 Locale Time的。
SimpleDateFormat 又是基于Calender(即基于 GMT millis(Date), timezone, locale)的一个日期格式化工具
Pattern 涉及的符号:https://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html
| 限定符和类型 | 字段和说明 |
|---|---|
static char[] |
DATE_FORMAT_FLAGS |
static long |
DAY_TO_MILLIS |
static int |
DAY_TO_SECONDS |
static String |
dd_MM_yyyy |
static String |
HH_mm_ss |
static String |
HH_mm_ss_CN |
static String |
HH_MM_ssZone |
static long |
HOURS_TO_MILLIS |
static int |
HOURS_TO_SECONDS |
static long |
MINUTES_TO_MILLIS |
static int |
MINUTES_TO_SECONDS |
static String |
MM_dd_yyyy |
static String |
MM_dd_yyyy_1 |
static long |
SECONDS_TO_MILLIS |
static String |
yyyy_MM_dd |
static String |
yyyy_MM_dd_1 |
static String |
yyyy_MM_dd_CN |
static String |
yyyy_MM_dd_CN_1 |
static String |
yyyy_MM_dd_HH_mm_ss |
static String |
yyyy_MM_dd_HH_mm_ss_SSS |
static String |
yyyy_MM_dd_T_HH_mm_ss |
static String |
yyyy_MM_dd_T_HH_mm_ssZone |
static String |
yyyyMMdd |
static String |
yyyyMMddHHmmss |
| 构造器和说明 |
|---|
Dates() |
| 限定符和类型 | 方法和说明 |
|---|---|
static Date |
add(Date date,
Calendars.DateField calendarField,
int amount) |
static Date |
add(Date date,
int calendarField,
int amount)
Adds to a date returning a new object.
|
static Date |
addDays(Date date,
int amount)
Adds a number of days to a date returning a new object.
|
static Date |
addHours(Date date,
int amount)
Adds a number of hours to a date returning a new object.
|
static Date |
addMilliseconds(Date date,
int amount)
Adds a number of milliseconds to a date returning a new object.
|
static Date |
addMinutes(Date date,
int amount)
Adds a number of minutes to a date returning a new object.
|
static Date |
addMonths(Date date,
int amount)
Adds a number of months to a date returning a new object.
|
static Date |
addSeconds(Date date,
int amount)
Adds a number of seconds to a date returning a new object.
|
static Date |
addWeeks(Date date,
int amount)
Adds a number of weeks to a date returning a new object.
|
static Date |
addYears(Date date,
int amount)
Adds a number of years to a date returning a new object.
|
static String |
format(Date date) |
static String |
format(Date date,
String pattern) |
static String |
format(long millis,
String pattern) |
static int |
get(Date date,
Calendars.DateField field) |
static int |
getDays(Date date) |
static int |
getHours(Date date) |
static int |
getMillis(Date date) |
static int |
getMinutes(Date date) |
static int |
getMonths(Date date) |
static int |
getMonths(Date date,
boolean getActualMonth) |
static int |
getSeconds(Date date) |
static SimpleDateFormat |
getSimpleDateFormat(String pattern) |
static SimpleDateFormat |
getSimpleDateFormat(String pattern,
Locale locale) |
static SimpleDateFormat |
getSimpleDateFormat(String pattern,
String timeZoneId) |
static SimpleDateFormat |
getSimpleDateFormat(String pattern,
String timeZoneId,
Locale locale) |
static SimpleDateFormat |
getSimpleDateFormat(String pattern,
TimeZone timeZone) |
static SimpleDateFormat |
getSimpleDateFormat(String pattern,
TimeZone timeZone,
Locale locale) |
static int |
getYears(Date date) |
static TimeZone |
localTimeZone() |
static long |
nextTime(long durationInMills) |
static long |
nextTime(long baselineInMills,
long durationInMills) |
static Date |
now() |
static long |
nowMills() |
static String |
nowReadableString() |
static Date |
parse(String dateString,
List<String> patterns) |
static Date |
parse(String dateString,
String... patterns) |
static Date |
parse(String dateString,
String pattern) |
static Date |
set(Date date,
Calendars.DateField field,
int amount)
Sets the specified field to a date returning a new object.
|
static Date |
setDays(Date date,
int amount)
Sets the day of month field to a date returning a new object.
|
static Date |
setHours(Date date,
int amount)
Sets the hours field to a date returning a new object.
|
static Date |
setMilliseconds(Date date,
int amount)
Sets the milliseconds field to a date returning a new object.
|
static Date |
setMinutes(Date date,
int amount)
Sets the minute field to a date returning a new object.
|
static Date |
setMonths(Date date,
int amount)
Sets the months field to a date returning a new object.
|
static Date |
setMonths(Date date,
int amount,
boolean valueIsActual) |
static Date |
setSeconds(Date date,
int amount)
Sets the seconds field to a date returning a new object.
|
static Date |
setYears(Date date,
int amount)
Sets the years field to a date returning a new object.
|
static Calendar |
toCalendar(Date date)
Converts a
Date into a Calendar. |
static Calendar |
toCalendar(Date date,
TimeZone tz)
Converts a
Date of a given TimeZone into a Calendar |
public static final int MINUTES_TO_SECONDS
public static final int HOURS_TO_SECONDS
public static final int DAY_TO_SECONDS
public static final long SECONDS_TO_MILLIS
public static final long MINUTES_TO_MILLIS
public static final long HOURS_TO_MILLIS
public static final long DAY_TO_MILLIS
public static final char[] DATE_FORMAT_FLAGS
SimpleDateFormatpublic static SimpleDateFormat getSimpleDateFormat(@NotEmpty String pattern)
public static SimpleDateFormat getSimpleDateFormat(@NotEmpty String pattern, @Nullable Locale locale)
public static SimpleDateFormat getSimpleDateFormat(@NotEmpty String pattern, @Nullable TimeZone timeZone)
public static SimpleDateFormat getSimpleDateFormat(@NotEmpty String pattern, @Nullable String timeZoneId)
public static SimpleDateFormat getSimpleDateFormat(@NotEmpty String pattern, @Nullable TimeZone timeZone, @Nullable Locale locale)
public static SimpleDateFormat getSimpleDateFormat(@NotEmpty String pattern, @Nullable String timeZoneId, @Nullable Locale locale)
public static Date addYears(Date date, int amount)
Date is unchanged.date - the date, not nullamount - the amount to add, may be negativeDate with the amount addedIllegalArgumentException - if the date is nullpublic static Date addMonths(Date date, int amount)
Date is unchanged.date - the date, not nullamount - the amount to add, may be negativeDate with the amount addedIllegalArgumentException - if the date is nullpublic static Date addWeeks(Date date, int amount)
Date is unchanged.date - the date, not nullamount - the amount to add, may be negativeDate with the amount addedIllegalArgumentException - if the date is nullpublic static Date addDays(Date date, int amount)
Date is unchanged.date - the date, not nullamount - the amount to add, may be negativeDate with the amount addedIllegalArgumentException - if the date is nullpublic static Date addHours(Date date, int amount)
Date is unchanged.date - the date, not nullamount - the amount to add, may be negativeDate with the amount addedIllegalArgumentException - if the date is nullpublic static Date addMinutes(Date date, int amount)
Date is unchanged.date - the date, not nullamount - the amount to add, may be negativeDate with the amount addedIllegalArgumentException - if the date is nullpublic static Date addSeconds(Date date, int amount)
Date is unchanged.date - the date, not nullamount - the amount to add, may be negativeDate with the amount addedIllegalArgumentException - if the date is nullpublic static Date addMilliseconds(Date date, int amount)
Date is unchanged.date - the date, not nullamount - the amount to add, may be negativeDate with the amount addedIllegalArgumentException - if the date is nullpublic static Date add(Date date, int calendarField, int amount)
Date is unchanged.date - the date, not nullcalendarField - the calendar field to add toamount - the amount to add, may be negativeDate with the amount addedIllegalArgumentException - if the date is nullpublic static Date add(Date date, Calendars.DateField calendarField, int amount)
public static Date setYears(Date date, int amount)
Date is unchanged.date - the date, not nullamount - the amount to setDate set with the specified valueIllegalArgumentException - if the date is nullpublic static Date setMonths(Date date, int amount)
Date is unchanged.date - the date, not nullamount - the amount to setDate set with the specified valueIllegalArgumentException - if the date is nullpublic static Date setDays(Date date, int amount)
Date is unchanged.date - the date, not nullamount - the amount to setDate set with the specified valueIllegalArgumentException - if the date is nullpublic static Date setHours(Date date, int amount)
Date is unchanged.date - the date, not nullamount - the amount to setDate set with the specified valueIllegalArgumentException - if the date is nullpublic static Date setMinutes(Date date, int amount)
Date is unchanged.date - the date, not nullamount - the amount to setDate set with the specified valueIllegalArgumentException - if the date is nullpublic static Date setSeconds(Date date, int amount)
Date is unchanged.date - the date, not nullamount - the amount to setDate set with the specified valueIllegalArgumentException - if the date is nullpublic static Date setMilliseconds(Date date, int amount)
Date is unchanged.date - the date, not nullamount - the amount to setDate set with the specified valueIllegalArgumentException - if the date is nullpublic static Date set(Date date, Calendars.DateField field, int amount)
Date is unchanged.date - the date, not nullfield - the Calendars.DateField field to set the amount toamount - the amount to setDate set with the specified valueIllegalArgumentException - if the date is nullpublic static int get(@NonNull Date date, Calendars.DateField field)
public static long nextTime(long baselineInMills,
long durationInMills)
public static long nextTime(long durationInMills)
public static long nowMills()
public static Date now()
public static String nowReadableString()
public static Calendar toCalendar(Date date)
Date into a Calendar.date - the date to convert to a CalendarNullPointerException - if null is passed inpublic static Calendar toCalendar(Date date, TimeZone tz)
Date of a given TimeZone into a Calendardate - the date to convert to a Calendartz - the time zone of the dateNullPointerException - if date or tz is nullpublic static TimeZone localTimeZone()
Copyright © 2021. All rights reserved.