本文最后更新于:2023年11月9日 晚上
                  
                
              
            
            
              
                
                在开发中,如果我们想查询数据库中某一天所有某个或所有时间点的数据,那么就要先获得当天的所有想查询的时间点
本人开发中的真实案例: 需要把当天从00:00开始到第二天的00:00(半小时(30分钟)为间隔)的数据全部查询出来展示**
相当于就是00:00,00:30,01:00,01:30,02:00,02:30……….
获得我们想要的所有时间点之后,可使用sql模糊或精确查询数据库日期时间字段
 小伙伴们很好奇为什么要以30分钟为间隔,解释下哈:本人项目中的数据库日期时间字段都是间隔30分钟的哈,意思是要么是整点比如12:00,要么就是:12:30,所有不存在其他时间哈
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 
 | package com.date.test;
 import org.junit.jupiter.api.Test;
 import org.springframework.boot.test.context.SpringBootTest;
 import java.text.ParseException;
 import java.text.SimpleDateFormat;
 import java.util.Calendar;
 import java.util.Date;
 
 
 
 
 
 
 @SpringBootTest
 public class test {
 
 @Test
 public void test() throws ParseException {
 
 
 Date date = new Date();
 
 SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
 
 String today = df.format(date);
 
 
 SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
 
 Date parse = simpleDateFormat.parse(today + " 00:00");
 
 
 String format = simpleDateFormat.format(parse);
 System.out.println(format);
 
 
 for (int y = 0; y < 48; y++) {
 
 
 Calendar c = Calendar.getInstance();
 
 c.setTime(parse);
 
 c.add(Calendar.MINUTE, 30);
 
 Date dateTime = c.getTime();
 
 parse = dateTime;
 
 
 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
 String format2 = sdf.format(dateTime);
 
 System.out.println(format2);
 }
 }
 }
 
 |