目錄java
@(Java 中的日期與時間)ide
最經常使用的幾個類,Date、DateFormat、Calendar、Locale函數
1.無參構造方法this
//根據當前系統默認的毫秒值建立時間對象 public Date() { this(System.currentTimeMillis()); }
2.根據毫秒值建立時間對象code
long time = 1000*60*60; Date d = new Date(time);
3.傳入年月日時分秒建立時間對象orm
Date d2 = new Date(20,10,10,11,11,11) //這獲得的時間並非20-10-10這種 //下面是源碼 public Date(int year, int month, int date, int hrs, int min, int sec) { int y = year + 1900; // month is 0-based. So we have to normalize month to support Long.MAX_VALUE. if (month >= 12) { y += month / 12; month %= 12; } else if (month < 0) { y += CalendarUtils.floorDivide(month, 12); month = CalendarUtils.mod(month, 12); } BaseCalendar cal = getCalendarSystem(y); cdate = (BaseCalendar.Date) cal.newCalendarDate(TimeZone.getDefaultRef()); cdate.setNormalizedDate(y, month + 1, date).setTimeOfDay(hrs, min, sec, 0); getTimeImpl(); cdate = null; }
DateFormat是日期時間格式化子類的抽象類,它以與語言無關的方式格式化並解析日期和時間。
他是抽象類,使用其子類SimpleDateFormat對象
DateFormat 類自己的內部提供了能夠直接爲其實例化的操做blog
//獲得日期的DateFormat對象: public static final DateFormat getDateInstance(); //獲得日期時間的DateFormat對象: public static final DateFormat getDateTimeInstance(); //使用DateFormat類格式化Date類日期 public final String format(Date date)
SimpleDateFormat類
SimpleDateFormat函數的繼承關係:
java.lang.Object
|
+—-java.text.Format
|
+—-java.text.DateFormat
|
+—-java.text.SimpleDateFormat繼承
//構造方法: public SimpleDateFormat(String pattern) //轉換: public Date parse(String source)throws ParseException //-->此時取得的是所有時間數。 //格式化: public final String Format(Date date) //-->將時間從新格式化成字符串顯示。
把Date轉化成指定的日期格式圖片
public class FormatDateTime { public static void main(String[] args) { SimpleDateFormat myFmt=new SimpleDateFormat("yyyy年MM月dd日 HH時mm分ss秒"); SimpleDateFormat myFmt1=new SimpleDateFormat("yy/MM/dd HH:mm"); SimpleDateFormat myFmt2=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//等價於now.toLocaleString() SimpleDateFormat myFmt3=new SimpleDateFormat("yyyy年MM月dd日 HH時mm分ss秒 E "); SimpleDateFormat myFmt4=new SimpleDateFormat("一年中的第 D 天 一年中第w個星期 一月中第W個星期 在一天中k時 z時區"); Date now=new Date(); System.out.println(myFmt.format(now)); System.out.println(myFmt1.format(now)); System.out.println(myFmt2.format(now)); System.out.println(myFmt3.format(now)); System.out.println(myFmt4.format(now)); System.out.println(now.toGMTString()); System.out.println(now.toLocaleString()); System.out.println(now.toString()); } }
把給定的字符串中的日期提取爲Date
這樣作,一般是一個日期字符串,但不是想要的格式,能夠先轉化爲Date,再轉化爲其它格式。
import java.text.* ; import java.util.* ; public class DateDemo05{ public static void main(String args[]){ String strDate = "2008-10-19 10:11:30.345" ; // 準備第一個模板,從字符串中提取出日期數字 String pat1 = "yyyy-MM-dd HH:mm:ss.SSS" ; // 準備第二個模板,將提取後的日期數字變爲指定的格式 String pat2 = "yyyy年MM月dd日 HH時mm分ss秒SSS毫秒" ; SimpleDateFormat sdf1 = new SimpleDateFormat(pat1) ; // 實例化模板對象 SimpleDateFormat sdf2 = new SimpleDateFormat(pat2) ; // 實例化模板對象 Date d = null ; try{ d = sdf1.parse(strDate) ; // 將給定的字符串中的日期提取出來 }catch(Exception e){ // 若是提供的字符串格式有錯誤,則進行異常處理 e.printStackTrace() ; // 打印異常信息 } System.out.println(sdf2.format(d)) ; // 將日期變爲新的格式 } };
DateFormat 和SimpleDateFormat 的區別
1.DateFormat 能夠直接使用,但其自己是一個抽象類,能夠根據Locate指定的區域獲得對應的日期時間格式
2.SimpleDateFormat 類是DateFormat 類的子類,通常狀況下來說 DateFormat 類不多會直接使用。而都使用SimpleDateFormat 類完成。
一、DateFormat:是抽象類,因此使用子類SimpleDateFormat 二、SimpleDateFormat類構造方法: public SimpleDateFormat():使用默認模式。 public SimpleDateFormat(String pattern):使用給定的模式。 API規定的模式:y M d H m s 三、SimpleDateFormat類成員方法: public final String format(Date date):日期格式化爲日期字符串。 pattern Date parse(String source):日期字符串解析爲日期。
Calendar:它爲特定瞬間與一組諸如 YEAR、MONTH、DAY_OF_MONTH、HOUR 等日曆字段之間的轉換提供了一些方法,併爲操做日曆字段(例如得到下星期的日期)提供了一些方法。
1、構造方法
protected Calendar() :因爲修飾符是protected,因此沒法直接建立該對象。須要經過別的途徑生成該對象。
2、成員方法
Calendar類的成員方法
static Calendar getInstance()
使用默認時區和區域設置獲取日曆。經過該方法生成Calendar對象。以下所示:
Calendar cr=Calendar.getInstance(); public void set(int year,int month,int date,int hourofday,int minute,int second) 設置日曆的年、月、日、時、分、秒。 public int get(int field) 返回給定日曆字段的值。所謂字段就是年、月、日等等。 public void setTime(Date date) 使用給定的Date設置此日曆的時間。Date------Calendar public Date getTime() 返回一個Date表示此日曆的時間。Calendar-----Date abstract void add(int field,int amount) 按照日曆的規則,給指定字段添加或減小時間量。 public long getTimeInMillies() 以毫秒爲單位返回該日曆的時間值。
3、日曆字段
日曆字段包含如下兩種:一種是表示時間的單位,例如年、月、日等等。另外一種是具體的日期,例如一月、二月、三月、一日、二日、三日、一點鐘、兩點鐘等等具體的時間。前一種通常時獲取的時候使用,後一種通常判斷的時候使用。
時間單位字段:
YEAR 年 MINUTE 分
DAY_OF_WEEK_IN_MONTH
某月中第幾周
MONTH 月 SECOND/MILLISECOND 秒/毫秒 WEEK_OF_MONTH 日曆式的第幾周
DATE 日 DAY_OF_MONTH
和DATE同樣
DAY_OF_YEAR 一年的第多少天
HOUR_OF_DAY 時 DAY_OF_WEEK 周幾 WEEK_OF_YEAR 一年的第多少周
AM_PM 返回1則表示是下午,返回0表示上午。
public class CalendarDemo { public static void main(String[] args) { // 其日曆字段已由當前日期和時間初始化: Calendar rightNow = Calendar.getInstance(); // 子類對象 // 獲取年 int year = rightNow.get(Calendar.YEAR); // 獲取月 int month = rightNow.get(Calendar.MONTH); // 獲取日 int date = rightNow.get(Calendar.DATE); //獲取幾點 int hour=rightNow.get(Calendar.HOUR_OF_DAY); //獲取上午下午 int moa=rightNow.get(Calendar.AM_PM); if(moa==1) System.out.println("下午"); else System.out.println("上午"); System.out.println(year + "年" + (month + 1) + "月" + date + "日"+hour+"時"); rightNow.add(Calendar.YEAR,5); rightNow.add(Calendar.DATE, -10); int year1 = rightNow.get(Calendar.YEAR); int date1 = rightNow.get(Calendar.DATE); System.out.println(year1 + "年" + (month + 1) + "月" + date1 + "日"+hour+"時"); } }
注意:month是從0開始的,而月份是從1開始的,因此month須要加一
Locale通常用於國際化,Locale表示一個特定的地區,Locale類支持很是多的國家和地區。咱們能夠經過如下方法,查看Locale支持的所有區域:
Locale[] ls = Locale.getAvailableLocales(); for (Locale locale:ls) { System.out.println("locale :"+locale); }
主要構造函數
// Locale的構造函數 Locale(String language) Locale(String language, String country) Locale(String language, String country, String variant)
使用例子:
有關java國際化,另一篇文章有描述。