java.util.date compareto_java.util.Calendar compareTo()方法

java.util.Calendar compareTo()方法

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

1 语法

public int compareTo(Calendar anotherCalendar)

2 参数

anotherCalendar:要比较的Calendar对象。

3 返回值

返回0,1或-1的整数值

4 示例1

package com.yiidian;

/**

* 一点教程网: http://www.yiidian.com

*/

/**

* java.util.Calendar compareTo()方法的例子

*/

import java.util.Calendar;

public class CalendarComparetoExample1 {

public static void main(String[] args) {

// create two calendar at the different dates

Calendar cal1 = (Calendar) Calendar.getInstance();

Calendar cal2 = (Calendar) Calendar.getInstance();;

// compare the time values represented by two calendar objects.

cal2.add(Calendar.HOUR, 10);

cal2.add(Calendar.MINUTE, 10);

cal2.add(Calendar.SECOND, 10);

int i = cal2.compareTo(cal1);

// It should return a positive integer(usually 1),

//if the current triggering object is greater than the passed one

System.out.println("The result is :"+i);

}

}

输出结果为:

The result is :1

5 示例2

package com.yiidian;

/**

* 一点教程网: http://www.yiidian.com

*/

/**

* java.util.Calendar compareTo()方法的例子

*/

import java.util.Calendar;

public class CalendarComparetoExample2 {

public static void main(String[] args) {

// create two calendar at the different dates

Calendar cal1 = (Calendar) Calendar.getInstance();

Calendar cal2 = (Calendar) Calendar.getInstance();;

// compare the time values represented by two calendar objects.

cal2.add(Calendar.HOUR, 10);

cal2.add(Calendar.MINUTE, 10);

cal2.add(Calendar.SECOND, 10);

int z = cal1.compareTo(cal2);

// It should return a negative integer( -1),

//if the current triggering object is less than the passed one

System.out.println("The result is :" + z);

}

}

输出结果为:

The result is :-1

6 示例3

package com.yiidian;

/**

* 一点教程网: http://www.yiidian.com

*/

/**

* java.util.Calendar compareTo()方法的例子

*/

import java.util.Calendar;

public class CalendarComparetoExample3 {

public static void main(String[] args) {

// create two calendar at the different dates

Calendar cal1 = (Calendar) Calendar.getInstance();

Calendar cal2 = (Calendar) Calendar.getInstance();;

// compare the time values represented by two calendar objects.

cal2.add(Calendar.HOUR, 10);

cal2.add(Calendar.MINUTE, 10);

cal2.add(Calendar.SECOND, 10);

// compare again but with the two calendars swapped

int j = cal1.compareTo(cal1);

// It should return 0 ,

//if the current triggering object is less equal to the passed one

System.out.println("The result is :" + j);

}

}

输出结果为:

The result is :0

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值