Java中如何检测某个时间点是否已超过当前时间?

2026-06-10 02:432阅读0评论SEO教程
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计401个文字,预计阅读时间需要2分钟。

Java中如何检测某个时间点是否已超过当前时间?

说明:最近接收到的一个需求,后端返回一个时间字符串,需要与当前系统时间对比。如果时间已过,则返回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; } }

Java中如何检测某个时间点是否已超过当前时间?

本文共计401个文字,预计阅读时间需要2分钟。

Java中如何检测某个时间点是否已超过当前时间?

说明:最近接收到的一个需求,后端返回一个时间字符串,需要与当前系统时间对比。如果时间已过,则返回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; } }

Java中如何检测某个时间点是否已超过当前时间?