Java中时间相关函数Date、Calendar、SimpleDateFormat
来源:互联网 发布:全炫茂 知乎 编辑:程序博客网 时间:2024/06/05 04:18
1、Java 时间相关的类Date和Calendar,其中Date是已经过时的类,不推荐使用,推荐使用Calendar类。
2、Date常用的方法有
boolean
after(Date when)
Tests if this date is after the specified date.
boolean
before(Date when)
Tests if this date is before the specified date.
Object
clone()
Return a copy of this object.
int
compareTo(Date anotherDate)
Compares two Dates for ordering.
boolean
equals(Object obj)
Compares two dates for equality.
int
getDate()
Deprecated.
As of JDK version 1.1, replaced by
Calendar.get(Calendar.DAY_OF_MONTH)
.int
getDay()
Deprecated.
As of JDK version 1.1, replaced by
Calendar.get(Calendar.DAY_OF_WEEK)
.int
getHours()
Deprecated.
As of JDK version 1.1, replaced by
Calendar.get(Calendar.HOUR_OF_DAY)
.int
getMinutes()
Deprecated.
As of JDK version 1.1, replaced by
Calendar.get(Calendar.MINUTE)
.int
getMonth()
Deprecated.
As of JDK version 1.1, replaced by
Calendar.get(Calendar.MONTH)
.int
getSeconds()
Deprecated.
As of JDK version 1.1, replaced by
Calendar.get(Calendar.SECOND)
.long
getTime()
Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by thisDate object.
int
getTimezoneOffset()
Deprecated.
As of JDK version 1.1, replaced by
-(Calendar.get(Calendar.ZONE_OFFSET) + Calendar.get(Calendar.DST_OFFSET)) / (60 * 1000)
.int
getYear()
Deprecated.
As of JDK version 1.1, replaced by
Calendar.get(Calendar.YEAR) - 1900
.int
hashCode()
Returns a hash code value for this object.
static long
parse(String s)
Deprecated.
As of JDK version 1.1, replaced by
DateFormat.parse(String s)
.void
setDate(int date)
Deprecated.
As of JDK version 1.1, replaced by
Calendar.set(Calendar.DAY_OF_MONTH, int date)
.void
setHours(int hours)
Deprecated.
As of JDK version 1.1, replaced by
Calendar.set(Calendar.HOUR_OF_DAY, int hours)
.void
setMinutes(int minutes)
Deprecated.
As of JDK version 1.1, replaced by
Calendar.set(Calendar.MINUTE, int minutes)
.void
setMonth(int month)
Deprecated.
As of JDK version 1.1, replaced by
Calendar.set(Calendar.MONTH, int month)
.void
setSeconds(int seconds)
Deprecated.
As of JDK version 1.1, replaced by
Calendar.set(Calendar.SECOND, int seconds)
.void
setTime(long time)
Sets this
Date
object to represent a point in time that istime
milliseconds after January 1, 1970 00:00:00 GMT.void
setYear(int year)
Deprecated.
As of JDK version 1.1, replaced by
Calendar.set(Calendar.YEAR, year + 1900)
.String
toGMTString()
Deprecated.
As of JDK version 1.1, replaced by
DateFormat.format(Date date)
, using a GMTTimeZone
.String
toLocaleString()
Deprecated.
As of JDK version 1.1, replaced by
DateFormat.format(Date date)
.String
toString()
Converts this
Date
object to a String
of the form:static long
UTC(int year, int month, int date, int hrs, int min, int sec)
Deprecated.
As of JDK version 1.1, replaced by
Calendar.set(year + 1900, month, date, hrs, min, sec)
orGregorianCalendar(year + 1900, month, date, hrs, min, sec)
, using a UTCTimeZone
, followed by Calendar.getTime().getTime()
.SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); System.out.println("今天是:"+sdf.format(c.getTime()));//2015-12-12 12:22:20 Date d = new Date(); System.out.println(d.getDate());//12 System.out.println(d.getTime());//1449893761937 System.out.println(sdf.format(d.getTime()));//2015-12-12 12:16:37 System.out.println(d.toLocaleString());//2015-12-12 12:21:103、Calendar
3.1 Calendar相对Date类能够更好的处理时间和日期,同时它可以与Date很好的转化。
Calendar cc = Calendar.getInstance(); Date dd = cc.getTime(); System.out.println(dd);//Sat Dec 12 12:26:35 CST 2015 cc.setTime(dd); System.out.println(cc.getTime());//Sat Dec 12 12:27:37 CST 20153.2 Calendar具体的方法如下:
abstract void
add(int field, int amount)
Adds or subtracts the specified amount of time to the given calendar field, based on the calendar's rules.
boolean
after(Object when)
Returns whether this
Calendar
represents a time after the time represented by the specifiedObject
.boolean
before(Object when)
Returns whether this
Calendar
represents a time before the time represented by the specifiedObject
.void
clear()
Sets all the calendar field values and the time value (millisecond offset from theEpoch) of this
Calendar
undefined.void
clear(int field)
Sets the given calendar field value and the time value (millisecond offset from theEpoch) of this
Calendar
undefined.Object
clone()
Creates and returns a copy of this object.
int
compareTo(Calendar anotherCalendar)
Compares the time values (millisecond offsets from the Epoch) represented by two
Calendar
objects.protected void
complete()
Fills in any unset fields in the calendar fields.
protected abstract void
computeFields()
Converts the current millisecond time value
time
to calendar field values in fields[]
.protected abstract void
computeTime()
Converts the current calendar field values in
fields[]
to the millisecond time value time
.boolean
equals(Object obj)
Compares this
Calendar
to the specified Object
.int
get(int field)
Returns the value of the given calendar field.
int
getActualMaximum(int field)
Returns the maximum value that the specified calendar field could have, given the time value of this
Calendar
.int
getActualMinimum(int field)
Returns the minimum value that the specified calendar field could have, given the time value of this
Calendar
.static Locale[]
getAvailableLocales()
Returns an array of all locales for which the
getInstance
methods of this class can return localized instances.String
getDisplayName(int field, int style,Locale locale)
Returns the string representation of the calendar
field
value in the givenstyle
and locale
.Map<String,Integer>
getDisplayNames(int field, int style,Locale locale)
Returns a
Map
containing all names of the calendarfield
in the given style
and locale
and their corresponding field values.int
getFirstDayOfWeek()
Gets what the first day of the week is; e.g.,
SUNDAY
in the U.S.,MONDAY
in France.abstract int
getGreatestMinimum(int field)
Returns the highest minimum value for the given calendar field of this
Calendar
instance.static Calendar
getInstance()
Gets a calendar using the default time zone and locale.
static Calendar
getInstance(Locale aLocale)
Gets a calendar using the default time zone and specified locale.
static Calendar
getInstance(TimeZone zone)
Gets a calendar using the specified time zone and default locale.
static Calendar
getInstance(TimeZone zone,Locale aLocale)
Gets a calendar with the specified time zone and locale.
abstract int
getLeastMaximum(int field)
Returns the lowest maximum value for the given calendar field of this
Calendar
instance.abstract int
getMaximum(int field)
Returns the maximum value for the given calendar field of this
Calendar
instance.int
getMinimalDaysInFirstWeek()
Gets what the minimal days required in the first week of the year are; e.g., if the first week is defined as one that contains the first day of the first month of a year, this method returns 1.
abstract int
getMinimum(int field)
Returns the minimum value for the given calendar field of this
Calendar
instance.Date
getTime()
Returns a
Date
object representing this Calendar
's time value (millisecond offset from theEpoch").long
getTimeInMillis()
Returns this Calendar's time value in milliseconds.
TimeZone
getTimeZone()
Gets the time zone.
int
getWeeksInWeekYear()
Returns the number of weeks in the week year represented by this
Calendar
.int
getWeekYear()
Returns the week year represented by this
Calendar
.int
hashCode()
Returns a hash code for this calendar.
protected int
internalGet(int field)
Returns the value of the given calendar field.
boolean
isLenient()
Tells whether date/time interpretation is to be lenient.
boolean
isSet(int field)
Determines if the given calendar field has a value set, including cases that the value has been set by internal fields calculations triggered by a
get
method call.boolean
isWeekDateSupported()
Returns whether this
Calendar
supports week dates.abstract void
roll(int field, boolean up)
Adds or subtracts (up/down) a single unit of time on the given time field without changing larger fields.
void
roll(int field, int amount)
Adds the specified (signed) amount to the specified calendar field without changing larger fields.
void
set(int field, int value)
Sets the given calendar field to the given value.
void
set(int year, int month, int date)
Sets the values for the calendar fields
YEAR
, MONTH
, and DAY_OF_MONTH
.void
set(int year, int month, int date, int hourOfDay, int minute)
Sets the values for the calendar fields
YEAR
, MONTH
, DAY_OF_MONTH
, HOUR_OF_DAY
, and MINUTE
.void
set(int year, int month, int date, int hourOfDay, int minute, int second)
Sets the values for the fields
YEAR
, MONTH
,DAY_OF_MONTH
, HOUR
, MINUTE
, and SECOND
.void
setFirstDayOfWeek(int value)
Sets what the first day of the week is; e.g.,
SUNDAY
in the U.S.,MONDAY
in France.void
setLenient(boolean lenient)
Specifies whether or not date/time interpretation is to be lenient.
void
setMinimalDaysInFirstWeek(int value)
Sets what the minimal days required in the first week of the year are; For example, if the first week is defined as one that contains the first day of the first month of a year, call this method with value 1.
void
setTime(Date date)
Sets this Calendar's time with the given
Date
.void
setTimeInMillis(long millis)
Sets this Calendar's current time from the given long value.
void
setTimeZone(TimeZone value)
Sets the time zone with the given time zone value.
void
setWeekDate(int weekYear, int weekOfYear, int dayOfWeek)
Sets the date of this
Calendar
with the the given date specifiers - week year, week of year, and day of week.String
toString()
Return a string representation of this calendar
3.3 重要方法实例
package SomeSystemClassLearning;import java.text.DateFormat;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date;import java.util.Locale;import java.util.TimeZone;public class DateLearning { public static void main(String args[]) throws Exception { Calendar can = Calendar.getInstance(); System.out.println("年--->"+can.get(can.YEAR)); System.out.println("月--->"+can.get(can.MONTH)); System.out.println("日--->"+can.get(can.DAY_OF_MONTH)); can.set(2015, 11, 06); System.out.println("设置的时间为--->"+can.getTime()); can.add(can.YEAR, -1); System.out.println("时间为---->"+can.getTime()); //Sat Dec 06 12:54:46 CST 2014 can.set(can.MONTH, 2); System.out.println("时间为:"+can.getTime()); //Thu Mar 06 12:54:46 CST 2014 TimeZone tz = TimeZone.getDefault(); System.out.println("默认的时区时:"+tz);//sun.util.calendar.ZoneInfo[id="Asia/Shanghai",offset=28800000,dstSavings=0,useDaylight=false,transitions=19,lastRule=null] System.out.println("时区的名称为:"+tz.getDisplayName());//中国标准时间 System.out.println("时区的ID为:"+tz.getID());//Asia/Shanghai Date dt= new Date(); String dateStr =DateFormat.getDateInstance(DateFormat.FULL,Locale.CHINA).format(dt); System.out.println("今天的日期是:"+dateStr);//2015年12月4日 星期五 String dateStr2 =DateFormat.getDateInstance(DateFormat.LONG,Locale.CHINA).format(dt); System.out.println("今天的日期是:"+dateStr2);//2015年12月4日 String dateStr3 =DateFormat.getDateInstance(DateFormat.MEDIUM,Locale.CHINA).format(dt); System.out.println("今天的日期是:"+dateStr3); //2015-12-4 String dateStr4 =DateFormat.getDateInstance(DateFormat.SHORT,Locale.CHINA).format(dt); System.out.println("今天的日期是:"+dateStr4); //15-12-4 String str1 = "2015-12-12"; System.out.println(DateFormat.getDateInstance().parse(str1));//Sat Dec 12 00:00:00 CST 2015 SimpleDateFormat sd = new SimpleDateFormat("yyyy年第D天"); String str = sd.format(dt); System.out.println(str);//2015年第338天 String str2= "15###十二月##04"; SimpleDateFormat sd1 = new SimpleDateFormat("y###MMM##d"); System.out.println(sd1.parse(str2 )); Calendar c = Calendar.getInstance(); System.out.println(c.getTime()); System.out.println("年:"+c.get(c.YEAR)); System.out.println("月:"+c.get(c.MONTH)); System.out.println("日"+c.get(c.DAY_OF_MONTH)); System.out.println("星期:"+c.get(c.DAY_OF_WEEK)); String dateSdf =DateFormat.getDateInstance(DateFormat.FULL,Locale.CHINA).format(c.getTime()); System.out.println("今天的日期是:"+dateStr);//2015年12月4日 星期五 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); System.out.println("今天是:"+sdf.format(c.getTime()));//2015-12-12 12:22:20 Date d = new Date(); System.out.println(d.getDate());//12 System.out.println(d.getTime());//1449893761937 System.out.println(sdf.format(d.getTime()));//2015-12-12 12:16:37 System.out.println(d.toLocaleString());//2015-12-12 12:21:10 Calendar cc = Calendar.getInstance(); Date dd = cc.getTime(); System.out.println(dd);//Sat Dec 12 12:26:35 CST 2015 cc.setTime(dd); System.out.println(cc.getTime());//Sat Dec 12 12:27:37 CST 2015 cc.set(2016, 2, 8); Calendar cc2 = Calendar.getInstance(); cc2.set(2015, 12, 12); System.out.println(cc2.after(cc));//false }}
0 0
- Java中时间相关函数Date、Calendar、SimpleDateFormat
- Java 时间--Date,SimpleDateFormat,Calendar
- Java中与时间相关的几个类(Date、Calendar、SimpleDateFormat)
- Java中关于时间的处理 Date SimpleDateFormat Calendar GegorianCalendar
- java:日期相关Date,Calendar,SimpleDateFormat
- Java| 时间日期处理(SimpleDateFormat,Calendar,Date)
- java中Random,Date,Calendar,DateFormat,SimpleDateFormat
- java中Calendar,simpleDateFormat,Date 操作
- java API 跟时间相关类:Date/Calendar/GregorianCalendar/DateFormat/SimpleDateFormat
- java 日期相关Date、SimpleDateFormat、Calendar区分和使用
- java calendar SimpleDateFormat Date 类
- Java - Calendar & Date & DateUtils & SimpleDateFormat
- java-Date、SimpleDateFormat、Calendar类
- Calendar日历+Date时间+SimpleDateFormat转换格式
- Java中的Date、Calendar、DateFormat、SimpleDateFormat
- Java之Date、Calendar、SimpleDateFormat总结
- java 时间类SimpleDateFormat,Date
- 获取时间Calendar、使用 Date 和 SimpleDateFormat 类表示时间
- mysql操作脚本总结
- 关于socket网络编程,各种报错若干总结
- 为方便管理Maven中的模块,Maven提供了聚合与继承来管理
- 同步与Java内存模型
- 共享DLL 和静态库DLL
- Java中时间相关函数Date、Calendar、SimpleDateFormat
- 两数交换
- 基于3D卷积神经网络的人体行为理解(论文笔记)
- UVa live 3905 扫描线
- UVA1586 Molar Mess
- POJ-3077
- iOS项目之Swift新闻App(二)—网络数据请求和添加TableHeader
- ffmpeg解码H264缺少帧的解决办法
- SOJ 4454 (矩阵快速幂)