compareto java date_java中compareTo比较两个日期大小

java中compareTo比较两个日期大小

我们对两个日期进行比较的时候,或者是日期的string进行比较的时候,以前我一直以为,如果大于的话compareTo的返回值应该是1,等于的话是0,小于的话是-1,网上很多也是这样说,但是现实中我程序出错,最后打出来,看了一下,如果大于的话返回的是正整数,等于是0,小于的话就是负整数,而不仅仅局限于1,0和-1,以后做比较要注意(这段话出处见此)

日期进行比较

源码

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16/**

* Compares two Dates for ordering.

*

* @param anotherDate the Date to be compared.

* @return the value 0 if the argument Date is equal to

* this Date; a value less than 0 if this Date

* is before the Date argument; and a value greater than

* 0 if this Date is after the Date argument.

* @since 1.2

* @exception NullPointerException if anotherDate is null.

*/

public int compareTo(Date anotherDate) {

long thisTime = getMillisOf(this);

long anotherTime = getMillisOf(anotherDate);

return (thisTime

}

例子

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36package bai.test;

import java.util.Calendar;

import java.util.Date;

import org.junit.Test;

public class CompareTo_Date {

@Test

public void test() {

Calendar c = Calendar.getInstance();

c.set(2016,5,4);

Date before =c.getTime();

c.set(2016,5,5);

Date now=c.getTime();

c.set(2016,5,6);

Date after=c.getTime();

//before早于now,返回负数,可用于判断活动开始时间是否到了

int compareToBefore=before.compareTo(now);

System.out.println("compareToBefore = "+compareToBefore);

int compareToIntNow=now.compareTo(now);

System.out.println("compareToIntNow = "+compareToIntNow);

//after晚于now,返回正数,可用于判断活动结束时间是否到了

int compareToIntAfter=after.compareTo(now);

System.out.println("compareToIntAfter = "+compareToIntAfter);

}

}

输出如下

1

2

3compareToBefore = -1

compareToIntNow = 0

compareToIntAfter = 1

日期的string进行比较

源码

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59/**

* Compares two strings lexicographically.

* The comparison is based on the Unicode value of each character in

* the strings. The character sequence represented by this

* String object is compared lexicographically to the

* character sequence represented by the argument string. The result is

* a negative integer if this String object

* lexicographically precedes the argument string. The result is a

* positive integer if this String object lexicographically

* follows the argument string. The result is zero if the strings

* are equal; compareTo returns 0 exactly when

* the {@link #equals(Object)} method would return true.

*

* This is the definition of lexicographic ordering. If two strings are

* different, then either they have different characters at some index

* that is a valid index for both strings, or their lengths are different,

* or both. If they have different characters at one or more index

* positions, let k be the smallest such index; then the string

* whose character at position k has the smaller value, as

* determined by using the < operator, lexicographically precedes the

* other string. In this case, compareTo returns the

* difference of the two character values at position k in

* the two string -- that is, the value:

*

 
 

* this.charAt(k)-anotherString.charAt(k)

*

* If there is no index position at which they differ, then the shorter

* string lexicographically precedes the longer string. In this case,

* compareTo returns the difference of the lengths of the

* strings -- that is, the value:

*

 
  

* this.length()-anotherString.length()

*

*

* @param anotherString the String to be compared.

* @return the value 0 if the argument string is equal to

* this string; a value less than 0 if this string

* is lexicographically less than the string argument; and a

* value greater than 0 if this string is

* lexicographically greater than the string argument.

*/

public int compareTo(String anotherString) {

int len1 = value.length;

int len2 = anotherString.value.length;

int lim = Math.min(len1, len2);

char v1[] = value;

char v2[] = anotherString.value;

int k = 0;

while (k < lim) {

char c1 = v1[k];

char c2 = v2[k];

if (c1 != c2) {

return c1 - c2;

}

k++;

}

return len1 - len2;

}

例子

1

2

3

4

5

6

7

8

9

10@Test

public void test() {

String date = "2017-07-17 11:03:52";

System.out.println("compareToBefore1 : "+date.compareTo("2017-06-16 11:03:52"));

System.out.println("compareToBefore2 : "+date.compareTo("2017-05-16 11:03:52"));

System.out.println("compareToNow1 : "+date.compareTo("2017-07-17 11:03:52"));

System.out.println("compareToNow2 : "+date.compareTo("2017-07-17"));

System.out.println("compareToAfter1 : "+date.compareTo("2017-07-18 11:03:52"));

System.out.println("compareToAfter2 : "+date.compareTo("2017-09-16 11:03:52"));

}

输出如下

1

2

3

4

5

6compareToBefore1 : 1

compareToBefore2 : 2

compareToNow1 : 0

compareToNow2 : 9

compareToAfter1 : -1

compareToAfter2 : -2

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值