Android 开发笔记 “java.util.Calendar.compareTo()”

时间:2015-01-13 22:56:54   收藏:0   阅读:704

 java.util.Calendar.compareTo() 方法比较Calendar对象和anotherCalendar对象之间的时间值(毫秒偏移量)。

声明

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

public int compareTo(Calendar anotherCalendar)

参数

返回值

如果参数所代表的时间等于通过此Calendar对象表示的时间方法返回0;或如果此Calendar的时间是由参数表示的时间之前返回小于0值,或如果该日历的时间所表示的时间之后返回大于0值。

异常

例子

下面的示例演示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 :1
The result is :-1

 

原文:http://www.cnblogs.com/Dylanblogs/p/4222495.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!