Java中时间相关函数Date、Calendar、SimpleDateFormat

来源:互联网 发布:全炫茂 知乎 编辑:程序博客网 时间:2024/06/05 04:18

1、Java 时间相关的类Date和Calendar,其中Date是已经过时的类,不推荐使用,推荐使用Calendar类。

2、Date常用的方法有

修饰符与类型方法与描述booleanafter(Date when)

Tests if this date is after the specified date.
booleanbefore(Date when)
Tests if this date is before the specified date.
Objectclone()
Return a copy of this object.
intcompareTo(Date anotherDate)
Compares two Dates for ordering.
booleanequals(Object obj)
Compares two dates for equality.
intgetDate()
Deprecated. 
As of JDK version 1.1, replaced by Calendar.get(Calendar.DAY_OF_MONTH).
intgetDay()
Deprecated. 
As of JDK version 1.1, replaced by Calendar.get(Calendar.DAY_OF_WEEK).
intgetHours()
Deprecated. 
As of JDK version 1.1, replaced by Calendar.get(Calendar.HOUR_OF_DAY).
intgetMinutes()
Deprecated. 
As of JDK version 1.1, replaced by Calendar.get(Calendar.MINUTE).
intgetMonth()
Deprecated. 
As of JDK version 1.1, replaced by Calendar.get(Calendar.MONTH).
intgetSeconds()
Deprecated. 
As of JDK version 1.1, replaced by Calendar.get(Calendar.SECOND).
longgetTime()
Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by thisDate object.
intgetTimezoneOffset()
Deprecated. 
As of JDK version 1.1, replaced by -(Calendar.get(Calendar.ZONE_OFFSET) + Calendar.get(Calendar.DST_OFFSET)) / (60 * 1000).
intgetYear()
Deprecated. 
As of JDK version 1.1, replaced by Calendar.get(Calendar.YEAR) - 1900.
inthashCode()
Returns a hash code value for this object.
static longparse(String s)
Deprecated. 
As of JDK version 1.1, replaced by DateFormat.parse(String s).
voidsetDate(int date)
Deprecated. 
As of JDK version 1.1, replaced by Calendar.set(Calendar.DAY_OF_MONTH, int date).
voidsetHours(int hours)
Deprecated. 
As of JDK version 1.1, replaced by Calendar.set(Calendar.HOUR_OF_DAY, int hours).
voidsetMinutes(int minutes)
Deprecated. 
As of JDK version 1.1, replaced by Calendar.set(Calendar.MINUTE, int minutes).
voidsetMonth(int month)
Deprecated. 
As of JDK version 1.1, replaced by Calendar.set(Calendar.MONTH, int month).
voidsetSeconds(int seconds)
Deprecated. 
As of JDK version 1.1, replaced by Calendar.set(Calendar.SECOND, int seconds).
voidsetTime(long time)
Sets this Date object to represent a point in time that istime milliseconds after January 1, 1970 00:00:00 GMT.
voidsetYear(int year)
Deprecated. 
As of JDK version 1.1, replaced by Calendar.set(Calendar.YEAR, year + 1900).
StringtoGMTString()
Deprecated. 
As of JDK version 1.1, replaced by DateFormat.format(Date date), using a GMTTimeZone.
StringtoLocaleString()
Deprecated. 
As of JDK version 1.1, replaced by DateFormat.format(Date date).
StringtoString()
Converts this Date object to a String of the form:
static longUTC(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:10
3、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 2015
3.2 Calendar具体的方法如下:
修饰符与类型方法与描述abstract voidadd(int field, int amount)
Adds or subtracts the specified amount of time to the given calendar field, based on the calendar's rules.
booleanafter(Object when)
Returns whether this Calendar represents a time after the time represented by the specifiedObject.
booleanbefore(Object when)
Returns whether this Calendar represents a time before the time represented by the specifiedObject.
voidclear()
Sets all the calendar field values and the time value (millisecond offset from theEpoch) of this Calendar undefined.
voidclear(int field)
Sets the given calendar field value and the time value (millisecond offset from theEpoch) of this Calendar undefined.
Objectclone()
Creates and returns a copy of this object.
intcompareTo(Calendar anotherCalendar)
Compares the time values (millisecond offsets from the Epoch) represented by two Calendar objects.
protected voidcomplete()
Fills in any unset fields in the calendar fields.
protected abstract voidcomputeFields()
Converts the current millisecond time value time to calendar field values in fields[].
protected abstract voidcomputeTime()
Converts the current calendar field values in fields[] to the millisecond time value time.
booleanequals(Object obj)
Compares this Calendar to the specified Object.
intget(int field)
Returns the value of the given calendar field.
intgetActualMaximum(int field)
Returns the maximum value that the specified calendar field could have, given the time value of thisCalendar.
intgetActualMinimum(int field)
Returns the minimum value that the specified calendar field could have, given the time value of thisCalendar.
static Locale[]getAvailableLocales()
Returns an array of all locales for which the getInstance methods of this class can return localized instances.
StringgetDisplayName(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.
intgetFirstDayOfWeek()
Gets what the first day of the week is; e.g., SUNDAY in the U.S.,MONDAY in France.
abstract intgetGreatestMinimum(int field)
Returns the highest minimum value for the given calendar field of thisCalendar instance.
static CalendargetInstance()
Gets a calendar using the default time zone and locale.
static CalendargetInstance(Locale aLocale)
Gets a calendar using the default time zone and specified locale.
static CalendargetInstance(TimeZone zone)
Gets a calendar using the specified time zone and default locale.
static CalendargetInstance(TimeZone zone,Locale aLocale)
Gets a calendar with the specified time zone and locale.
abstract intgetLeastMaximum(int field)
Returns the lowest maximum value for the given calendar field of thisCalendar instance.
abstract intgetMaximum(int field)
Returns the maximum value for the given calendar field of thisCalendar instance.
intgetMinimalDaysInFirstWeek()
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 intgetMinimum(int field)
Returns the minimum value for the given calendar field of thisCalendar instance.
DategetTime()
Returns a Date object representing this Calendar's time value (millisecond offset from theEpoch").
longgetTimeInMillis()
Returns this Calendar's time value in milliseconds.
TimeZonegetTimeZone()
Gets the time zone.
intgetWeeksInWeekYear()
Returns the number of weeks in the week year represented by thisCalendar.
intgetWeekYear()
Returns the week year represented by this Calendar.
inthashCode()
Returns a hash code for this calendar.
protected intinternalGet(int field)
Returns the value of the given calendar field.
booleanisLenient()
Tells whether date/time interpretation is to be lenient.
booleanisSet(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 aget method call.
booleanisWeekDateSupported()
Returns whether this Calendar supports week dates.
abstract voidroll(int field, boolean up)
Adds or subtracts (up/down) a single unit of time on the given time field without changing larger fields.
voidroll(int field, int amount)
Adds the specified (signed) amount to the specified calendar field without changing larger fields.
voidset(int field, int value)
Sets the given calendar field to the given value.
voidset(int year, int month, int date)
Sets the values for the calendar fields YEAR, MONTH, and DAY_OF_MONTH.
voidset(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.
voidset(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.
voidsetFirstDayOfWeek(int value)
Sets what the first day of the week is; e.g., SUNDAY in the U.S.,MONDAY in France.
voidsetLenient(boolean lenient)
Specifies whether or not date/time interpretation is to be lenient.
voidsetMinimalDaysInFirstWeek(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.
voidsetTime(Date date)
Sets this Calendar's time with the given Date.
voidsetTimeInMillis(long millis)
Sets this Calendar's current time from the given long value.
voidsetTimeZone(TimeZone value)
Sets the time zone with the given time zone value.
voidsetWeekDate(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.
StringtoString()
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
原创粉丝点击