Java Calendar 計算時間差,calendar時間差

來源:互聯網
上載者:User

Java Calendar 計算時間差,calendar時間差

  1. public static void main(String[] args) {  
  2.         Calendar c=Calendar.getInstance();  
  3.         int y=2016;//年  
  4.         int M=1;//月  
  5.         int d=1;//日  
  6.         int H=0;//時  
  7.         int m=0;//分  
  8.         int s=0;//秒  
  9.         c.set(Calendar.YEAR, y);  
  10.         c.set(Calendar.MONTH, M-1);  
  11.         c.set(Calendar.DATE, d);  
  12.         c.set(Calendar.HOUR_OF_DAY, H);  
  13.         c.set(Calendar.MINUTE, m);  
  14.         c.set(Calendar.SECOND, s);  
  15.         Calendar now=Calendar.getInstance();  
  16.         long aTime=now.getTimeInMillis();  
  17.         long bTime=c.getTimeInMillis();  
  18.         long cTime=aTime-bTime;  
  19.         long sTime=cTime/1000;//時間差,單位:秒  
  20.         long mTime=sTime/60;  
  21.         long hTime=mTime/60;  
  22.         long dTime=hTime/24;  
  23.         SimpleDateFormat f=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
  24.         System.out.println("目前時間:"+f.format(now.getTime()));  
  25.         System.out.println("設定時間:"+f.format(c.getTime()));  
  26.         System.out.println("時間差:"+dTime+"天"+hTime%24+"時"+mTime%60+"分"+sTime%60+"秒");  
  27.     }  

聯繫我們

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