Date Date类封装了当前日期和时间。
方法
描述
Date()
使用当前日期和时间初始化对象
Date(long millisec)
接收一个自1970年1月1日午夜以来经历的毫秒数初始化时间
方法
描述
boolean after(Date date)
如果调用Date对象中包含的日期比date指定的日期晚,就返回true;否则返回false
boolean before(Date date)
如果调用Date对象中包含的日期比date指定日期早,就返回true;否则返回false
Object clone()
复制调用Date对象
int compareTo(Date date)
比较调用对象中包含的日期与date指定的日期。如果两者相同,就返回0;如果调用对象早于date,就返回一个负值;如果调用对象晚于date,就返回一个正直
boolean equals(Object date)
如果调用Date对象中包含的日期和时间与date指定的日期和时间相同,就返回true;否则返回false
static Date from(Instant t)
返回与t中传递的Instant对象对应的Date对象(JDK8新增)
long getTime()
返回自1920年1月1日午夜开始已经经历的毫秒数
int hashCode()
返回调用对象的散列码
void setTime(long time)
将日期和时间设置为time指定的值,time是自从1970年1月1日午夜开始已经经历的毫秒数
Instant toInstant()
返回与调用Date对象对应的Instant对象(JDK8新增)
String toString()
将调用Date对象转换成字符串并返回结果
1 2 3 4 5 6 7 8 9 10 11 12 13 14 import java.util.Date;public class LearnDate1 { public static void main (String args[]) { Date date = new Date(); System.out.println(date); long msec = date.getTime(); System.out.println("Milliseconds since Jan.1, 1970 GMT = " + msec); } }
Calendar Calendar抽象类提供了一套方法,允许将毫秒数形式的时间转换成大量有用的时间组成部分。
方法
描述
abstract void add(int which, int val)
将val添加到由which指定的时间或日期组成部分中。如果要进行减法操作,可以添加一个负值。which必须是Calendar类定义域变量之一,如Calendar.HOUR
boolean after(Object calendarObj)
如果调用Calendar对象中包含的日期比calendarObj指定的日期晚,就返回true;否则返回false
boolean before(Object canlendarObj)
如果调用Calendar对象中包含的日期比calendarObj指定的日期早,就返回true了否则返回false
final void clear()
将调用对象中包含的所有时间组成部分清零
final void clear(int which)
将调用对象中包含的由which指定的时间组成部分清零
Object clone()
返回调用对象的一个副本
boolean equals(Object calendarObj)
如果调用Calendar对象包含的日期与calendarObj指定的日期相同,就返回true;否则返回false
int get(int calendarField)
返回调用对象中某个时间组成部分的值,这个组成部分由calendarField指定。可以返回的组成部分包括Calendar.YEAR、Calendar.MONTH等
static Locate[] getAvailableLocales()
返回一个由Locale对象组成的数组,其中包括可以使用日历地区信息
static Calendar getInstance()
为默认地区和时区返回Calendar对象
static Calendar getInstance(TimeZone tz)
为tz指定的时区返回Calendar对象。使用默认地区
static Calendar getInstance(Locale locale)
为locale指定的地区返回Canlendar对象。使用默认地区
static Calendar getInstance(TimeZone tz, Locale locale)
为tz指定的时区和locale指定的地区返回Calendar对象
final Date getTime()
返回与调用对象的时间相同的Date对象
TimeZone getTimeZone()
返回调用对象的时区
final boolean isSet(int which)
如果设置了指定的时间组成部分,就返回true;否则返回false
void set(int which, int val)
在调用对象中,将which指定的日期或时间组成部分设置为由val指定的值。which必须是Calendar定义的域变量之一,如Calendar.HOUR
final void set(int year, int month, int dayOfMonth, int hours, int minutes)
设置调用对象的各种日期和时间组成部分
final void set(int year, int month, int dayOfMonth, int hours, int minutes, int seconds)
设置调用对象的各种日期和时间组成部分
final void setTime(Date d)
设置调用对象的各种日期和时间组成部分。信息从Date对象d中获取
void setTimeZone(TimeZone tz)
将调用对象的时区设置为tz指定的时区
final Instant toInstant()
返回与调用Calendar实例对应的Instant对象(JDK8新增)
ALL_STYLES
HOUR_OF_DAY
PM
AM
JANUARY
SATURDAY
AM_PM
JULY
SECOND
APRIL
JUNE
SEPTEMBER
AUGUST
LONG
SHORT
DATE
LONG_FORMAT
SHORT_FORMAT
DAY_OF_MONTH
LONG_STANDALONE
SHORT_STANDALONE
DAY_OF_WEEK
MARCH
SUNDAY
DAY_OF_WEEK_IN_MONTH
MAY
THURSDAY
DAY_OF_YEAR
MILLISECOND
TUESDAY
DECEMBER
MINUTE
UNDECIMBER
DST_OFFSET
MONDAY
WEDNESDAY
ERA
MONTH
WEEK_OF_MONTH
FEBRUARY
NARROW_FORMAT
WEEK_OF_YEAR
FIELD_COUNT
NARROW_STANDALONE
YEAR
FRIDAY
NOVEMBER
ZONE_OFFSET
HOUR
OCTOBER
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 import java.util.Calendar;public class LearnCalendar1 { public static void main (String args[]) { String months[] = { "JAN" , "FEB" , "MAR" , "APR" , "MAY" , "JUN" , "JUL" , "AUG" , "SEP" , "OCT" , "NOV" , "DEC" }; Calendar calendar = Calendar.getInstance(); System.out.print("Date:" ); System.out.print(months[calendar.get(Calendar.MONTH)]); System.out.print(" " + calendar.get(Calendar.DATE) + " " ); System.out.println(calendar.get(Calendar.YEAR)); System.out.print("Time: " ); System.out.print(calendar.get(Calendar.HOUR) + ":" ); System.out.print(calendar.get(Calendar.MINUTE) + ":" ); System.out.println(calendar.get(Calendar.SECOND)); calendar.set(Calendar.HOUR, 10 ); calendar.set(Calendar.MINUTE, 29 ); calendar.set(Calendar.SECOND, 22 ); System.out.print("Updated time:" ); System.out.print(calendar.get(Calendar.HOUR) + ":" ); System.out.print(calendar.get(Calendar.MINUTE) + ":" ); System.out.println(calendar.get(Calendar.SECOND)); } }
GregorianCalendar GregorianCalendary类时Calendar类的具体实现、Calendar类的getInstance()通常返回一个GregorianCalendar对象。
方法
描述
GregorianCalendar()
使用默认地区和时区下的当前日期和时间进行初始化
GregorianCalendar(int year, int month, int dayOfMonth)
自定义年月日
GregorianCalendar(int year, int month, int dayOfMonth, int hours, int minutes)
自定义年月日时分
GregorianCalendar(int year, int month, int dayOfMonth, int hours, int minutes, int seconds)
自定义年月日时分秒
GregorianCalendar(Locale locale)
使用地区进行初始化
GregorianCalendar(TimeZone timeZone)
使用时区进行初始化
GregorianCalendar(TimeZone timeZone, Locale locale)
使用地区和时区进行初始化
方法
描述
boolean isLeapYear(int year)
如果year是闰年,返回true;否则false
fro()
toZeonDateTime()
getCalendarType()
TimeZone TimeZone抽象类可以处理与格林尼治标准时间(GMT)————也就是世界时间(UTC)之间的时差。
方法
描述
Object clone()
返回特定于TimeZone的clone()版本
static String[] getAvailableIDs()
返回一个表示所有时区名称的String对象数组
static String[] getAvailableIDs(int timeDelta)
返回一个String对象数组,表示与GMT时差为timeDelta的所有时区名称
static TimeZone getDefault()
返回一个表示宿主计算机上默认时区的TimeZone对象
String getID()
返回调用TimeZone对象的名称
abstract int getOffset(int era, int year, int month, int dayOfMonth, int dayOfWeek, int millisec)
返回计算当时时间时需要添加到GMT的时差,这个值会针对夏令时进行调整。该方法的参数表示日期和时间的组成部分
abstract int getRawOffset()
返回计算当地时间时需要添加到GMT的原始时差(使用毫秒表示),这个值不会针对夏令时进行调整
static TimeZone getTimeZone(String tzName)
为名为tzName的时区返回TimeZone对象
abastract boolean inDaylightTime(Date d)
如果日期d在调用对象的夏令时范围之内,就返回true;否则返回false
static void setDefault(TimeZone tz)
设置当前主机使用的默认时区,tz是对将要使用的TimeZone对象的引用
void setID(String tzName)
将时区的名称(即时区的ID)设置为tzName指定的名称
abstract void setRawOffset(int millis)
以毫秒为单位设置与GMT之间的时差
ZoneId toZoneId()
将调用对象转换为ZoneId,并返回结果。ZoneId定义在java.time包中(JDK8新增)
abastract boolean useDaylightTime()
如果调用对象使用夏令时,就返回true;否则返回false
SimpeTimeZone SimpleTimeZone类时TimeZone的一个便利子类。它实现了TimeZone的抽象方法,并且可以操作Gregorian日历的时区,此外还能够计算夏令时。
方法
描述
SimpleTimeZone(int timeDelta, String tzName)
创建一个相对于格林尼治标准时间的时差是timeDelta,时区名称为tzName的对象
SimpleTimeZone(int timeDelta, String tzId, int dstMonth0, int dstDayInMonth0, int dstDay0, int time0, int dstMonth1, int dstDayInMonth1, int dstDay1, int time1)
创建一个相对GMT时差为timeDelta,时区名称为tzID,夏令时开始和结束的对象
SimpleTimeZone(int timeDelta, String tzId, int dstMonth0, int dstDayInMonth0, int dstDay0, int time0, int dstMonth1, int dstDayInMonth1, int dstDay1, int time1, int dstDelta)
特殊指定夏令时期间节约的毫秒数为dstDelta
SimpleTimeZone(int timeDelta, String tzId, int dstMonth0, int dstDayInMonth0, int dstDay0, int time0, int time0mode, int dstMonth1, int dstDayInMonth1, int dstDay1, int time1, int time1mode, int dstDelta)
time0mode指定开始时间的模式,time1mode指定结束时间的模式(有效模式为STANDARD_TIME,WALL_TIME,UTC_TIME)
Locale Locale用于描述地理或文化上的区域。
方法
描述
Locale(String language)
构建特定语言区域对象
Locale(String language, String country)
构建特定语言和国家的区域对象
Locale(String language, String country, String variant)
构建特定语言、国家和辅助信息的区域对象
方法
描述
static void setDefault(Locale localeObj)
将JVM使用的默认地区设置为localeObj
final String getDisplayCountry()
返回可阅读的国家名称
final String getDisplayLanguage()
返回可阅读的语言
final String getDisplayName()
返回可阅读的地区名称
static Locale getDefault()
获取默认地区信息
CANADA
GERMAN
KOREAN
CANADA_FRENCH
GERMANY
PRC
CHINA
ITALIAN
SIMPLIFIED_CHINESE
CHINESE
ITALY
TAIWAN
ENGLISH
JAPAN
TRADITIONAL_CHINESE
FRANCE
JAPANESE
UK
FRENCH
KOREA
US
DateFormat是抽象类,提供了格式化和解析日期与时间的能力。
方法
描述
static final DateFormat getDateInstance()
static final DateFormat getDateInstance(int style)
static final DateFormat getDateInstance(int style, Locale locale)
final String format(Date a)
参数d是将要显示的Date对象,返回字符串包含格式化之后的信息
值
含义
DEFAULT
SHORT
MEDIUM
LONG
FULL
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 import java.text.DateFormat;import java.util.Date;import java.util.Locale;public class LearnDateFormat1 { public static void main (String args[]) { Date date = new Date(); DateFormat df; df = DateFormat.getTimeInstance(DateFormat.SHORT, Locale.CHINESE); System.out.println("Chinese :" + df.format(date)); df = DateFormat.getDateInstance(DateFormat.LONG, Locale.UK); System.out.println("UK :" + df.format(date)); df = DateFormat.getDateInstance(DateFormat.FULL, Locale.US); System.out.println("US :" + df.format(date)); } }
SimpleDateFormat是DateFormat的一个具体子类,定义了自己的格式化模式,可以用于显示日期和时间信息。
方法
描述
SimpleDateFormat(String formatString)参数formatString描述了符合显示日期和时间信息
符号
描述
a
AM或PM(上午或下午)
d
某月中的某天(1~31)
h
AM/PM中某个小时(1~12)
k
一天中某个小时(1~24)
m
小时中的某分钟(0~59)
s
分钟中的某秒(0~59)
u
一星期中的某天,星期一是1
w
一年中的某个星期(1~52)
y
年份
z
时区
D
一年中的某天(1~366)
E
一星期中的某天(星期一)
F
一月中的第几个星期几
G
纪元(AD或BC,分布表示公元后或公元前)
H
一天中的某个小时(0~23)
K
AM/PM中的某个小时(0~11)
L
月份
M
月份
S
毫秒
W
某月中的某个星期数(1~5)
X
IOS 8061格式的时区
Y
一年中的某个星期
Z
RFC 822 格式的时区
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 import java.text.SimpleDateFormat;import java.util.Date;public class LearnSimpleDateFormat1 { public static void main (String args[]) { Date date = new Date(); SimpleDateFormat sdf; sdf = new SimpleDateFormat("hh:mm:ss" ); System.out.println(sdf.format(date)); sdf = new SimpleDateFormat("dd MMM yyyy hh:mm:ss zzz" ); System.out.println(sdf.format(date)); sdf = new SimpleDateFormat("E MMM dd yyyy" ); System.out.println(sdf.format(date)); } }
LocalDate LocalDate封装的日期使用默认的由ISO 8601指定的Gregorian日历。
方法
描述
static LocalDate now()
放回当前系统日期
LocalTime LocalTime封装了由ISO 8601指定的时间
方法
描述
static LocalTime now()
放回当前系统时间
LocalDateTime LocalDateTime封装了日期和时间
方法
描述
static LocalDateTime now()
放回当前系统日期和时间
static LocalDateTime parse(CharSequence dateTimeStr)
将字符串解析
static LocalDateTime parse(CharSequence dateTimeStr, DateTimeFormatter dateTimeFmtr)
使用指定格式化器解析字符串
1 2 3 4 5 6 7 8 9 10 11 12 import java.time.LocalDateTime;import java.time.format.DateTimeFormatter;public class LearnLocalDateTime { public static void main (String args[]) { LocalDateTime curDateTime = LocalDateTime.parse("June 21, 2014 12:01 AM" , DateTimeFormatter.ofPattern("MMMM d, yyyy h:mm a" )); System.out.println(curDateTime.format(DateTimeFormatter.ofPattern("MMMM d',' yyyy hh':'mm a" ))); } }
DateTimeFormatter用于格式化日期和时间
方法
描述
static DateTimeFormatter ofLocalizedDate(FormatStyle fmtDate)
创建日期的格式化器
static DateTimeFormatter ofLocalizedTime(FormatStyle fmtTime)
创建时间的格式化器
static DateTimeFormatter ofLocalizedDateTime(FormatStyle fmtDate, FormatStyle fmtTime)
创建日期和时间的格式化器
值
含义
FULL
LONG
MEDIUM
SHORT
|模式字符|描述| |a|AM/PM指示器| |d|某个月份的某天| |E|一星期中的某天| |h|采用12小时形式表示的小时| |H|采用24小时形式表示的小时| |M|月份| |m|分钟| |s|秒钟| |y|年份|
1 2 3 4 5 6 7 8 9 10 11 import java.time.LocalDateTime;import java.time.format.DateTimeFormatter;public class LearnDateTimeFormatter1 { public static void main (String args[]) { LocalDateTime curDateTime = LocalDateTime.now(); System.out.println(curDateTime.format(DateTimeFormatter.ofPattern("MMMM d, yyyy h:mm a" ))); } }