java判断当前时间是否在对应时间段内

这里写自定义目录标题

java判断当前时间是否在对应时间段内

public static boolean isBetween(LocalDateTime beginTime, LocalDateTime endTime) {
    //获取当前时间
    LocalDateTime now = LocalDateTime.now();
    boolean flag = false;
    if (now.isAfter(beginTime) && now.isBefore(endTime)) {
        flag = true;
    }
    return flag;
}

JAVA8 LocalDate和LocalDateTime使用
//获取当前日期
LocalDate localDate = LocalDate.now(); //输出 2022-08-24

//主动构造
LocalDate localDate1 = LocalDate.of(2022, 08,24);    //输出 2022-08-24
//Date转LocalDate
Date date = new Date();
LocalDate dateToLocalDate = LocalDate.ofInstant(date.toInstant(), 	ZoneId.systemDefault());    //输出 2022-08-24	
LocalDate localDate1 = LocalDate.of(2022, 08,01);
 LocalDate localDate2 = LocalDate.of(2022, 08,02);
Period period = Period.between(localDate1, localDate2);
//这里请注意,如果日期小的在前面,下面的方法可能为负,  即用  第二个参数 - 第一个参数

System.out.println(period.getDays());        //1
System.out.println(period.getMonths());      //0
System.out.println(period.getYears());       //0

//为什么获取的日期差是0? LocalDate.toEpochDay(), 转换成纪元日期那?

LocalDate localDate1 = LocalDate.of(2022, 07,01); LocalDate localDate2 = LocalDate.of(2022, 08,01);
long minus = localDate2.toEpochDay() - localDate1.toEpochDay()System.out.println(minus);          //31

LocalDateTime获取到时分秒的方法

  //当前日期时间
 LocalDateTime localDateTime = LocalDateTime.now();    2022-08-24
System.out.println("localDateTime:"+localDateTime); 2022-08-24 T15:55:44.307730
LocalDateTime dt = LocalDateTime.of(2022, 8, 24, 10, 50, 0);
LocalDateTime localDateTime1 = LocalDateTime.ofInstant(date.toInstant(),ZoneId.systemDefault());

计算时间差值:Duration

Duration duration = Duration.between(dt, localDateTime1);
System.out.println("间隔天数:"+duration.toDays());     
System.out.println("间隔小时数:"+duration.toHours());   
System.out.println("间隔分钟数:"+duration.toMinutes());  
 System.out.println("间隔秒数:"+duration.toSeconds());    
LocalDateTime localDateTime1 = LocalDateTime.of(2022,8,17,23,0,0);
LocalDateTime localDateTime2 = LocalDateTime.of(2022, 8, 18, 10, 50, 0);
//17号和18号差一天的
Duration duration = Duration.between(localDateTime1, localDateTime2);
System.out.println("间隔天数:"+duration.toDays());      //0
System.out.println("间隔小时数:"+duration.toHours());   //11
System.out.println("间隔分钟数:"+duration.toMinutes()); //710
System.out.println("间隔秒数:"+duration.toSeconds());

这里Duration是绝对时间差值,是两个时间点相差的时间,2020-03-17 23:00:00 到 2020-03-18 10:50:00 相差11个小时,所以不够一天。 这个一定要注意区分。用LocalDate.toEpochDay相减,同一天的时间差值 用Duration

  • 0
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
假设你有以下的嵌套Map,表示不同时间段的起始和结束时间,以及对应的数值: ```java Map<String, Map<String, Integer>> timeRanges = new HashMap<>(); timeRanges.put("range1", new HashMap<String, Integer>() {{ put("start", 0); put("end", 3); put("value", 10); }}); timeRanges.put("range2", new HashMap<String, Integer>() {{ put("start", 3); put("end", 6); put("value", 20); }}); timeRanges.put("range3", new HashMap<String, Integer>() {{ put("start", 6); put("end", 9); put("value", 30); }}); timeRanges.put("range4", new HashMap<String, Integer>() {{ put("start", 9); put("end", 12); put("value", 40); }}); ``` 然后你可以使用SimpleDateFormat类来获取当前时间对应的小时数,再根据这个小时数来判断当前时间属于哪个时间段,并获取对应的数值: ```java SimpleDateFormat sdf = new SimpleDateFormat("HH"); String currentHour = sdf.format(new Date()); int hour = Integer.parseInt(currentHour); String timeRange = ""; int value = 0; for (Map.Entry<String, Map<String, Integer>> entry : timeRanges.entrySet()) { Map<String, Integer> range = entry.getValue(); int start = range.get("start"); int end = range.get("end"); if (hour >= start && hour < end) { timeRange = entry.getKey(); value = range.get("value"); break; } } System.out.println("当前时间段:" + timeRange + ",对应数值:" + value); ``` 这段代码首先创建了一个SimpleDateFormat对象,用来格式化当前时间,获取当前小时数。然后遍历嵌套Map,找到当前时间属于哪个时间段,并获取对应的数值。最后输出结果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

princeAladdin

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值