天天看點

Date日期的格式與處理 Joda-Time

最近對Date日期應用的比較多,然而之前對于Date的了解還不夠深刻,是以寫這個文檔來記錄一下Date。

首先Date在java中有兩個類中有,一個是 java.util.Date 另一個是 java.sql.Date。

我們一般用的是第一種。

import java.util.Date;
public class DateFormat {
        public static void main(String[] args)
        {
                Date currentDate=new Date();
                System.out.println("currentDate is" +currentDate);      
        }
}

      

顯示結果currentDate isWed Jun 04 19:57:18 CST 2014

然而我們用

而月則需要加1因為是從0開始計算的。Date來直接取得時間時,年會從2014-》114( 用這個方法擷取年份時是從1900年開始計算的,是以當年份為2014時,得到的結果為114,是以如果要得到最終的年份,要再加上1900。)

顯然直接用Date不是很友善,是以我們可以采用一個SimpleDateFormat的一個類。

public class SimpleDateFormat extends DateFormat

SimpleDateFormat 是一個以國别敏感的方式格式化和分析資料的具體類。 它允許格式化 (date -> text)、文法分析 (text -> date)和标準化。

SimpleDateFormat 允許以為日期-時間格式化選擇任何使用者指定的方式啟動。 但是,希望用 DateFormat 中的getTimeInstance、 getDateInstance 或 getDateTimeInstance 建立一個日期-時間格式化程式。 每個類方法傳回一個以預設格式化方式初始化的日期/時間格式化程式。 可以根據需要用 applyPattern 方法修改格式化方式。 

SimpleDateFormat函數的繼承關系:

java.lang.Object

   |

   +----java.text.Format

           |

           +----java.text.DateFormat

                   |

                   +----java.text.SimpleDateFormat

public class DateFormat {
        public static void main(String[] args)
        {
                Date currentDate=new Date();
                SimpleDateFormat sdf=new SimpleDateFormat();
                String time=sdf.format(currentDate);
                System.out.println("currentDate is " +time);
                
                Date currentTime=new Date();
                SimpleDateFormat sdf1=new SimpleDateFormat("yyyy-mm-dd");
                String time2=sdf1.format(currentTime);
                System.out.println("currentDate is " +time2);                   
        }
}


      

運作結果 

currentDate is 14-6-4 下午8:10

currentDate is 2014-10-04

我們可以再建立一個simpleDateFormat的時候指定其格式,然後擷取我們想要的日期格式。

但這樣僅僅是解決了一個DateFormat的問題。

對于Date格式我們用得少了,更多的使用Calender

public class DateFormat {
        public static void main(String[] args) throws ParseException
        {
                SimpleDateFormat sdf=new SimpleDateFormat("yyyy-mm-dd");
                Calendar calendar=Calendar.getInstance();
                String time=sdf.format(calendar.getTime());
                
                System.out.println("currentDate is "+calendar.getTime());
                System.out.println("currentDate is "+calendar.get(Calendar.YEAR));
                System.out.println("currentDate is "+calendar.get(Calendar.MONTH));
                System.out.println("currentDate is "+calendar.get(Calendar.DATE));
                System.out.println("time is "+time);                            
        }

}

      

輸出結果  

currentDate is Wed Jun 04 20:37:58 CST 2014

currentDate is 2014

currentDate is 5(輸入的月份也需要加1)

currentDate is 4

time is 2014-37-04

月份是因為一月是0

如果有星期幾的話需要減一,因為sunday是1,saturday是7

calender日期加減後指派給Date類型 

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");   
String time=sdf.format(new Date());    
Calendar cd = Calendar.getInstance();   
  
try {   
    cd.setTime(sdf.parse(time));   
} catch (ParseException e) {               
    e.printStackTrace();   
}   
      cd.add(Calendar.DATE, 1);//增加一天        
       //cal.add(Calendar.DATE, -1);      //減一天    
       //cd.add(Calendar.MONTH, 1);//增加一月    
      Date date=cd.getTime();    
      System.out.println(sdf.format(date));

      

這是通過calendar來處理日期的增加減少。

總感覺這樣對日期的處理也不是很好,我們選取了一個新的開源的Date處理庫

Joda-Time

簡介:

public class DateFormatshadow {
        public static void main(String[] args) throws ParseException
        {
                
                DateTime date=new DateTime();
                DateTimeFormatter dtf=DateTimeFormat.forPattern("yyyy-MM-dd");//這地方注意一定要大寫MM
                DateTime time=dtf.parseDateTime("2014-05-23");
                //DateTime time=getDateTime("2011-05-22");
                System.out.println("  "+date.toString());
                System.out.println(" 年: "+date.getYear());
                System.out.println(" 月: "+date.getMonthOfYear());
                System.out.println(" 日: "+date.getDayOfMonth());
                System.out.println("  "+time.toString());
                System.out.print(time.plusDays(3).toString());
                
                
        }


}

      

輸出結果 

  2014-06-04T21:31:36.112+08:00

 年: 2014

 月: 6

 日: 4

  2014-05-23T00:00:00.000+08:00

2014-05-26T00:00:00.000+08:00

這樣就可以友善直接的對日期進行加減并且直接取得目前日期,并且繼承之前可以用的方法。