java判断时间是否已经过期

说明:最近碰到一个需求,后端返回一个时间字符串,需要跟当前系统时间做比对,如果时间已经过了,那么就返回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;
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值