Java中日期時間格式的小結__java

來源:互聯網
上載者:User

=================================================
我的總結:Java中有關日期時間格式的總結
=================================================
1、與日期時間相關的類:
      第一:java.util.Date;
      第二:java.util.Calender;
      第三:java.text.DateFormat
      第四:java.text.SimpleDateFormate
2、步驟方法:
      1)擷取系統日期時間的步驟(使用Date擷取):
           步驟一 :確定要輸出的事件格式  SimpleDateFormate sdf = new SimpleDateFormate("yyyy-MM-dd hh:mm:ss");
           步驟二:擷取目前時間  Date nowDate = new Date();
           步驟三:輸出目前時間  System.out.println(sdfdong.format(nowDateDong));
      2)擷取系統日期時間的步驟(使用Calendar擷取):
           步驟一 :確定要輸出的事件格式  SimpleDateFormate sdf = new SimpleDateFormate("yyyy-MM-dd hh:mm:ss");
           步驟二: 擷取目前時間  Calendar nowCal = Calendar.getInstance();
           步驟三:輸出目前時間  System.out.println(sdfdong.format(nowDateDong));
      3)日期時間及格式類的相關說明:
           (1)SimpleDateFormate類中格式說明:
                     y 代表年, M代表月,d代表日,h代表時,m代表分,s代表秒
                    SimpleDateFormate是DateFormate的一個子類,是一個以與語言環境有關的方式來格式化和解析日期的具體類。
                    它允許進行格式化(日期 -> 文本)、解析(文本 -> 日期)和正常化
                    @格式化方法為:formate,將日期格式化為字串以便輸出,傳回型別為StringBuilder,參數為Date對象,
                        使用方法為:String myNowDate = sdf.formate(nowDate)
                    @解析方法為: parse,將文本解析為確定日期,原型為 Date parse(String text, ParsePosition pos)
                                              傳回型別為Date,參數String text為輸入的字串,參數ParPosition pos為從字串的何處開始解析
                        使用方法為:Date nowDate = sdf.parse("2010-11-24  14:19:00");
                   【關注】DateFormate的使用: DateFormat  myDateFormat = DateFormate.getDateInstance();獲得是預設時區、預設語言下
                                  預設格式日期時間格式;常常使用此類的getDateTimeInstance來設定特定時區、特定語言下的日期時間格式
                     舉例為:
                     DateFormat df=new SimpleDateFormat("yyyy-MM-dd EE hh:mm:ss");
      System.out.println(df.format(new Date()));
      Date date = new Date();
      DateFormat shortDate=DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
      DateFormat mediumDate =DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM);
      DateFormat longDate =DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG);
      DateFormat fullDate =DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL);

      System.out.println(shortDate.format(date));
      System.out.println(mediumDate.format(date));
      System.out.println(longDate.format(date));
      System.out.println(fullDate.format(date));

        08-4-15 下午3:24
      2008-4-15 15:24:31
      2008年4月15日 下午03時24分31秒
      2008年4月15日 星期二 下午03時24分31秒CST
            (2)Calendar的使用:
                     常使用Calendar的方法為:getInstance(), add(), get(), set(), getTime(), setTime(), set(), getMaxium(),
                                                                getActualMaximum(),getActualMinimum()
                      @使用getInstance產生Calendar對象:Calendar rightNow = Calendar.getInstance();
                          【注意】getInstance方法有4個重載,用於擷取設定的時區、語言環境下的一個日曆,
                                        無參數是擷取預設時區和語言的日曆
                      @使用add()方法來改變特定日期和時間(如月的加減,日的加減):
                            abstract  void add(int field, int amount) ;
                            使用樣本:   求10小時後的時間
                            java.util.Calendar  Cal=java.util.Calendar.getInstance();  
                      Cal.setTime(dateOper);  
                      Cal.add(java.util.Calendar.HOUR_OF_DAY,10);    //如果是10小時前的話,-10即可
                    System.out.println("date:"+forma.format(Cal.getTime()));
                      @使用get()方法來擷取日曆中的一個特定欄位:int get(int field);   //返回給定日曆欄位的值
                            使用舉例:int daysofYear = nowCal.get(Calendar.DATE_OF_YEAR);
                       @使用set()方法來擷取
                      @使用getTime()方法來擷取目前時間: Date getTime();    //返回一個表示此 Calendar 時間值
                                                                                                        //(從曆元至現在的毫秒位移量)的 Date 對象
                      @使用setTime()方法:void setTime(Date date);    //使用給定的 Date 設定此 Calendar 的時間。
                      @使用set()方法:set()方法有4個重載,分別為
                             void set(int field, int value) ;     //將給定的日曆欄位設定為給定值
                             void set(int year, int month, int date) ;       //設定日曆欄位 YEAR、MONTH 和 DAY_OF_MONTH 的值
                             void set(int year, int month, int date, int hourOfDay, int minute) ;     //設定日曆欄位 YEAR、MONTH、
                                                                                                            // DAY_OF_MONTH、HOUR_OF_DAY 和 MINUTE 的值
                             void set(int year, int month, int date, int hourOfDay, int minute, int second) ;     //設定欄位 YEAR、
                                                                                            //MONTH、DAY_OF_MONTH、HOUR、MINUTE 和 SECOND 的值
                      @使用getMaxium()方法來擷取給定日曆欄位的最大值:abstract  int getMinimum(int field);
                             參數為int field,用來設定年,月,日,時,分,秒等欄位
                      @使用getActualMaximum()方法來擷取日曆欄位中可能擁有的最大值: int getActualMaximum(int field) ;
                          使用getActualMinimum()方法來擷取日曆欄位中可能擁有的最小值: int getActualMinimum(int field) ;
              (3)Date的使用方法:
                       主要的使用是用於獲得當前的舉例格林威治時間(1970年1月1日 00:00:00),常用的方法不多
                        使用執行個體:求兩個日期之間相隔的天數:
                         java.text.SimpleDateFormat format = new java.text.SimpleDateFormat("yyyy-MM-dd");
                         java.util.Date beginDate= format.parse("2007-12-24");    
                         java.util.Date endDate=   format.parse("2007-12-25");  
                 long day=(date.getTime()-mydate.getTime())/(24*60*60*1000);  
                System.out.println("相隔的天數="+day);
                                          一年前的日期:
                 java.text.Format formatter=new java.text.SimpleDateFormat("yyyy-MM-dd");
                 java.util.Date todayDate=new java.util.Date();   
                long beforeTime=(todayDate.getTime()/1000)-60*60*24*365;
                todayDate.setTime(beforeTime*1000);
                 String beforeDate=formatter.format(todayDate);
                 System.out.println(beforeDate);
3、附錄一個小例子:用於計算距離生日還有多少天

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 時就日期格式的相關聯絡
 * @author Administrator
 * 時間:2010-11-23
 * */
public class CalculateBirthday {
 /**
  * @param str 生日日期
  * @return 距離生日多少天
  * @throws ParseException
  * */
 public static long daysBeforeBirthday(String str) throws ParseException{
  SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  Date birthDay = sdf.parse(str);
  Date nowDay = new Date();
  Calendar birthCal = Calendar.getInstance();
  Calendar nowCal = Calendar.getInstance();
  birthCal.setTime(birthDay);
  nowCal.setTime(nowDay);
  birthCal.set(Calendar.YEAR, nowCal.get(Calendar.YEAR));
  int bDayofYear = birthCal.get(Calendar.DAY_OF_YEAR);
  int nDayofYear = nowCal.get(Calendar.DAY_OF_YEAR);
  if(bDayofYear > nDayofYear){
   return (bDayofYear-nDayofYear);
  }else if(bDayofYear < nDayofYear){
   birthCal.add(Calendar.YEAR, 1);
   long distTimeInMills = birthCal.getTimeInMillis() - nowCal.getTimeInMillis();
   return distTimeInMills/(24*60*60*1000);
  }else{
   return 0;
  }
 }
 
 public static void main(String[] args) throws ParseException{
  System.out.println(daysBeforeBirthday("1988-08-28")+"天");
  System.out.println(daysBeforeBirthday("1988-03-21")+"天");
  System.out.println(daysBeforeBirthday("1990-11-24")+"天");
 }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.