java中Date的一些用法

來源:互聯網
上載者:User

-----------------------------------------------返回時間差

 

import java.text.*;

import java.util.Date;

public class Demo {
 public static void main(String[] args) throws Exception {

  SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  Date begin = df.parse("2004-01-02 11:30:24");
  Date end = df.parse("2004-03-26 13:31:40");
  long between = (end.getTime() - begin.getTime()) / 1000;// 除以1000是為了轉換成秒
  long day = between / (24 * 3600);//天
  long hour = between % (24 * 3600) / 3600;//小時
  long minute = between % 3600 / 60;//分鐘
  long second = between % 60 / 60;//秒
  
  System.out.println(day+"天"+hour+"小時"+minute+"分鐘"+second+"秒");
  
  
 }
}

 

------------------------------------------------------------獲得Date對象中的資訊

 

                            Date d2 = new Date();

                   //年份

                   int year = d2.getYear() + 1900;

                   //月份

                   int month = d2.getMonth() + 1;

                   //日期

                   int date = d2.getDate();

                   //小時

                   int hour = d2.getHours();

                   //分鐘

                   int minute = d2.getMinutes();

                   //秒

                   int second = d2.getSeconds();

                   //星期幾

                   int day = d2.getDay();

                   System.out.println("年份:" + year);

                   System.out.println("月份:" + month);

                   System.out.println("日期:" + date);

                   System.out.println("小時:" + hour);

                   System.out.println("分鐘:" + minute);

                   System.out.println("秒:" + second);

                   System.out.println("星期:" + day);

----------------------------------------------------------------

       import java.util.*;

 

/**

 * 計算兩個日期之間相差的天數

 */

public class DateExample1 {

         public static void main(String[] args) {

                   //設定兩個日期

                   //日期:2009年3月11號

                   Calendar c1 = Calendar.getInstance();

                   c1.set(2009, 3 - 1, 11);

                   //日期:2010年4月1號

                   Calendar c2 = Calendar.getInstance();

                   c2.set(2010, 4 - 1, 1);

                   //轉換為相對時間

                   long t1 = c1.getTimeInMillis();

                   long t2 = c2.getTimeInMillis();

                   //計算天數

                   long days = (t2 - t1)/(24 * 60 * 60 * 1000);

                   System.out.println(days);

         }

}

 部分代碼來自:http://blog.csdn.net/Mailbomb/archive/2009/03/10/3976465.aspx

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.