Android 開發筆記 “java.util.Calendar.compareTo()”

來源:互聯網
上載者:User

標籤:

 java.util.Calendar.compareTo() 方法比較Calendar對象和anotherCalendar對象之間的時間值(毫秒位移量)。

聲明

以下是java.util.Calendar.compareTo()方法的聲明

public int compareTo(Calendar anotherCalendar)
參數
  • anotherCalendar -- 要比較的Calendar對象。

傳回值

如果參數所代表的時間等於通過此Calendar對象表示的時間方法返回0;或如果此Calendar的時間是由參數表示的時間之前返回小於0值,或如果該日曆的時間所表示的時間之後返回大於0值。

異常
  • NullPointerException - 如果指定的Calendar為null。

  • IllegalArgumentException - 如果不能得到指定的日曆Calendar 對象的時間值

例子

下面的樣本示範java.util.calendar.compareTo()方法的用法。

package com.yiibai;import java.util.*;public class CalendarDemo {   public static void main(String[] args) {      // create two calendar at the different dates      Calendar cal1 = new GregorianCalendar(2015, 8, 15);      Calendar cal2 = new GregorianCalendar(2008, 1, 02);      // compare the time values represented by two calendar objects.      int i = cal1.compareTo(cal2);      // return positive value if equals else return negative value      System.out.println("The result is :"+i);      // compare again but with the two calendars swapped      int j = cal2.compareTo(cal);      // return positive value if equals else return negative value      System.out.println("The result is :" + j);   }}

讓我們來編譯和運行上面的程式,這將產生以下結果:

The result is :1The result is :-1

 

Android 開發筆記 “java.util.Calendar.compareTo()”

聯繫我們

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