我是靠谱客的博主 愉快小笼包,这篇文章主要介绍java字符串、时间大小比较,现在分享给大家,希望可以做个参考。

复制代码
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
package mytest; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class TimeTest { public static void main(String[] args) throws ParseException { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); String currentTime = sdf.format(new Date()); System.out.println(currentTime); //输出 2018-05-17 Service ss = new Service("2018-5-16"); System.out.println(ss.getTime()); // 输出 2018-5-16 System.out.println( sdf.parse(ss.getTime()).compareTo(sdf.parse(currentTime)) < 0 ); // 输出 true System.out.println(ss.getTime().compareTo(currentTime)); //输出 -5 } } class Service { private String time; public Service(String time) { super(); this.time = time; } public String getTime() { return time; } public void setTime(String time) { this.time = time; } }

时间是字符串 类型时, 比较大小 先要 转化成 时间格式 比较。

字符串比较大小的规则:

      首先取出两个字符串的长度,比较较小的长度内,两者是否相等。

          若不相等,则直接返回该位置字符的ASCII码相减后的值。

          若各位置都相等,则将两个字符串长度的差值返回。

转载于:https://www.cnblogs.com/z360519549/p/9052004.html

最后

以上就是愉快小笼包最近收集整理的关于java字符串、时间大小比较的全部内容,更多相关java字符串、时间大小比较内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(78)

评论列表共有 0 条评论

立即
投稿
返回
顶部