说明:最近碰到一个需求,后端返回一个时间字符串,需要跟当前系统时间做比对,如果时间已经过了,那么就返回false,在前端隐藏控件,如果时间没到,就返回true。其实就是一个时
说明:最近碰到一个需求,后端返回一个时间字符串,需要跟当前系统时间做比对,如果时间已经过了,那么就返回false,在前端隐藏控件,如果时间没到,就返回true。其实就是一个时间比对器,判断任务时间和系统时间哪个大注意,大写的HH表示24小时制
package com.example.demo2; import com.example.demo2.entity.*; import org.apache.tomcat.util.buf.StringUtils; import org.springframework.boot.test.context.SpringBootTest; import java.text.*; import java.util.*; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.stream.Collectors; import static java.util.concurrent.TimeUnit.SECONDS; import java.time.*; import java.time.format.DateTimeFormatter; import java.time.temporal.ChronoUnit; import java.time.temporal.TemporalAdjusters; import java.util.Date; import java.text.SimpleDateFormat; import java.util.Date; @SpringBootTest(classes = {Demo2Application.class}, properties = {"application.properties"}) public class Test { @org.junit.Test public void test() throws ParseException { String a = "15:56"; String b = "15:54"; String c = "16:01"; boolean date = getTimeStatus("23:26"); //false 不展示 System.out.println(date); } private boolean getTimeStatus(String timeGson) { boolean isRun = false; Date now = new Date(System.currentTimeMillis()); GregorianCalendar gc = new GregorianCalendar(); gc.setTime(now); java.text.SimpleDateFormat format = new java.text.SimpleDateFormat("HH:mm"); String nowTime = format.format(gc.getTime()); String[] timeGsonArray = timeGson.split(":"); String[] nowArray = nowTime.split(":"); String time = Arrays.stream(timeGsonArray).collect(Collectors.joining()); String nowTimeString = Arrays.stream(nowArray).collect(Collectors.joining()); int timeInt = Integer.parseInt(time); int nowTimeInt = Integer.parseInt(nowTimeString); if (nowTimeInt > timeInt) { isRun = false; } else { isRun = true; } return isRun; } }