当前位置:网站首页>String type time comparison method with error string.compareto
String type time comparison method with error string.compareto
2022-07-25 14:55:00 【zulj131】
Recently because of a bug Look at the old projects of the company , Found the following code :
/** * Returns the number of seconds between , Get the settings within the specified time , If not within the specified time , Return to null * @param conf Configuration information class * @param time At present, it is HH:mm:ss, And start and end Not unified ,start and end yes HH:mm * @return */
private String getDistanceSecond(RepostUser conf,String time)
{
if (conf.getStart1()!=null && !conf.getStart1().equals("") && time.compareTo(conf.getStart1()) >= 0 && time.compareTo(conf.getEnd1()) <= 0)
{
return (conf.getDistanceSecond()+ conf.getDistanceTime()*60)+","+conf.getNum();
}
else if (conf.getStart2() != null && conf.getStart2() != null && !conf.getStart2().equals("") && time.compareTo(conf.getStart2()) >= 0 && time.compareTo(conf.getEnd2()) <= 0)
{
return conf.getDistanceSecond2()+ conf.getDistanceTime2()*60+","+conf.getNum2();
}
else if (conf.getStart3()!=null && conf.getStart3() != null && !conf.getStart3().equals("") && time.compareTo(conf.getStart3()) >= 0 && time.compareTo(conf.getEnd3()) <= 0)
{
return conf.getDistanceSecond3()+ conf.getDistanceTime3()*60+","+conf.getNum3();
}
else if (conf.getStart4() != null && conf.getStart4() != null && !conf.getStart4().equals("") && time.compareTo(conf.getStart4()) >= 0 && time.compareTo(conf.getEnd4()) <= 0)
{
return conf.getDistanceSecond4()+ conf.getDistanceTime4()*60+","+conf.getNum4();
}
else if (conf.getStart5()!=null && conf.getStart5() != null && !conf.getStart5().equals("") && time.compareTo(conf.getStart5()) >= 0 && time.compareTo(conf.getEnd5()) <= 0)
{
return conf.getDistanceSecond5()+conf.getDistanceTime5()*60+","+conf.getNum5();
}
return null;// Indicates that it is out of range
}
there start and end, The format is HH:mm
time It is HH:mm:ss
here start and end , Why and time The format is different , I don't care , Just as start and end The number of seconds is 00 To deal with .
According to the above , I wrote a test program .
public class MainApp {
public static void main(String[] args) {
String time="16:40:00";
String start="16:35";
String end="16:40";
if(time.compareTo(start) >= 0 && time.compareTo(end) <= 0){
System.out.println(" In the interval ");
}else{
System.out.println(" Not in the interval ");
}
}
}
Results output :
OK, Then say so , The original code does not contain boundaries ? Then I'll try again time=16:35:00
public class MainApp {
public static void main(String[] args) {
String time="16:35:00";
String start="16:35";
String end="16:40";
if(time.compareTo(start) >= 0 && time.compareTo(end) <= 0){
System.out.println(" In the interval ");
}else{
System.out.println(" Not in the interval ");
}
}
}
Results output :
In the interval
!!!, Then this is wrong , The boundary either contains , Or it doesn't include , What do you mean by giving me a include and a exclude ?
Actually, this one uses String Of compareTo Method to compare , This method is like this :
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;
}
First compareTo Will compare each character of the string ASCII The size of a yard , If the range , Then return the difference between them . Otherwise, continue to find unequal characters .
If in Math.min(len1,len2) Characters within , It's all the same .
Then directly return the difference of their string length .
So I think this method can compare the size of time ? My answer is not necessarily , If they are all the same length , In theory, it's really no problem , But you make me grow differently , I can do nothing about it, either , We have to use a more accurate time to compare .
So I use JDK8 Time provided api( stay java.time Inside the bag ) refactoring .
public class MainApp {
public static void main(String[] args) {
String time="16:35:00";
String start="16:35";
String end="16:40";
LocalTime time2=LocalTime.parse(time,DateTimeFormatter.ofPattern("HH:mm:ss"));
LocalTime time3=LocalTime.parse(start,DateTimeFormatter.ofPattern("HH:mm"));
LocalTime time4=LocalTime.parse(end,DateTimeFormatter.ofPattern("HH:mm"));
System.out.println(time3);
if (time2.compareTo(time3) >= 0 && time2.compareTo(time4) <= 0) {
System.out.println(" In the interval ");
}else{
System.out.println(" Not in the interval ");
}
}
}
In this way, it will be more in line with normal thinking .
But if it's too Many objects are generated , I think we can take the evil route . Direct will start and end Add... To the back of :00 Just fine .
public class MainApp {
public static void main(String[] args) {
String time="16:40:00";
String start="16:35";
String end="16:40";
if(start.length()==5){
start+=":00";
}
if(end.length()==5){
end+=":00";
}
if (time.compareTo(start) >= 0 && time.compareTo(end) <= 0) {
System.out.println(" In the interval ");
}else{
System.out.println(" Not in the interval ");
}
It's also possible to , Because the larger the number ,ASCII The bigger the size , In the case of equal length , It can be more correct .
边栏推荐
- Application practice: Great integrator of the paddy classification model [paddlehub, finetune, prompt]
- Filters get the data in data; Filters use data in data
- 关于ROS2安装connext RMW的进度条卡在13%问题的解决办法
- Go语言创始人从Google离职
- Writing standard of physical quantities and unit symbols
- SSH服务器拒绝了密码
- IP地址分类,判断一个网段是子网超网
- Gson and fastjson
- [C题目]牛客 链表中倒数第k个结点
- The concept and operation rules of calculus of variations
猜你喜欢
![[MySQL must know and know] trigger | permission management](/img/59/cb805d972097a6a8ed7f3ae454a91d.png)
[MySQL must know and know] trigger | permission management

【MySQL必知必会】触发器 | 权限管理

基于浏览器的分屏阅读

I2C device driver hierarchy

Add the jar package under lib directory to the project in idea

006操作符简介

【口才】谈判说服技巧及策略

51 single chip microcomputer learning notes (2)

43 box model
![应用实践:Paddle分类模型大集成者[PaddleHub、Finetune、prompt]](/img/b6/62a346174bfa63fe352f9ef7596bfc.png)
应用实践:Paddle分类模型大集成者[PaddleHub、Finetune、prompt]
随机推荐
Development of uni app offline ID card identification plug-in based on paddleocr
gson与fastjson
06、类神经网络
Software testing -- 1. Outline of software testing knowledge
Gameframework making games (II) making UI interface
27 classification of selectors
Syntax summary of easygui
Awk from getting started to digging in (23) awk built-in variables argc, argc -- command line parameter transfer
Resource not found: rgbd_launch 解决方案
C language and SQL Server database technology
I hope some suggestions on SQL optimization can help you who are tortured by SQL like me
微信公众号正式环境上线部署,第三方公众平台接入
MySQL sort
45padding won't open the box
[C题目]力扣876. 链表的中间结点
LeetCode_ String_ Medium_ 151. Reverse the words in the string
[C topic] force buckle 876. Intermediate node of linked list
Gonzalez Digital Image Processing Chapter 1 Introduction
LeetCode_ Factorization_ Simple_ 263. Ugly number
Quickly set up dobbo demo