oracle日期与字符串的相互转化

1.字符串转为日期格式(to_date)

例1:把字符串类型2005-01-01 13:14:20 转成 2005/1/1 13:14:20日期格式

select to_date('2005-01-01 13:14:20','yyyy-MM-dd HH24:mi:ss') from dual;

结果:
在这里插入图片描述
例2:把字符串类型30-11月-19 转成 2018/12/31 日期格式

select to_date('30-11月-19 ', 'dd-mon-yy') from dual

结果:
在这里插入图片描述
注意:字符串转日期时,字符串和日期格式要匹配,如字符串格式为30-11月-19,如果后边跟yyyy-MM-dd就会报格式不匹配的错误,必须使用dd-mon-yy


2.日期格式转字符串(to_char)

例1:把sysdate(2020/5/12 17:31:23)转化为yyyy-MM-dd HH24:mi:ss字符串格式

select to_char(sysdate,'yyyy-MM-dd HH24:mi:ss') from dual;

结果:
在这里插入图片描述
例2:表car.lp_totallpdata中有一个字段enddate字段,代表了结束日期。enddate字段中的数据格式是varchar2类型的:(30-11月-19),现在要求查出表中结束日期等于字符串’2019-11’的数据

也就是说图片描也就是说述
也就是说找出enddate = ‘2019-11’的数据

分析:
首先30-11月-19 和 2019-12都属于字符串类型的,但是他们的格式不一样,我们可以先把enddate字段中的数据转化为正常的日期格式,再把他转化为字符串,看他与2019-12是否相等
1.先把enddate字段中的数据转化为正常的日期格式

to_date(t.enddate, 'dd-mon-yy')  //先转化为日期	30-11月-19==> 2019/11/30

2.再把他转化为我们想要的字符串

to_char(to_date(t.enddate, 'dd-mon-yy'),'yyyy-mm')  // 2019/11/30 ==> 2019-11

2.完整的过滤sql

select t.*
from car.lp_totallpdata  t
where to_char(to_date(t.enddate, 'dd-mon-yy'),'yyyy-mm')='2019-11'


3.日期范围查询

日期的范围查询,假设要查询 2011-05-02 到 2011-05-30 之间的数据
这条查询语句有两种实现方式:

假设数据库的字段 time 为 日期类型,传来的数据为字符串类型!!!
1. to_date 方式

把传来的数据 2011-05-02 、 2011-05-02 转化为日期再与time比较

select * from tablename 
where time >= to_date('2011-05-02','yyyy-mm-dd')    
and  time <= to_date('2011-05-30','yyyy-mm-dd') 

运行的结果是:可以显示05-02的数据,但是不能显示05-30的数据。

解决方案:
①如果想显示05-30的数据可以<to_date(‘2011-05-31’,‘yyyy-mm-dd’),这样就能显示30号的了。

②如果想要显示05-30的数据可以<=to_date(‘2011-05-30 23:59:59 999’,‘yyyy-mm-dd hh24:mi:ss’)也是可以查出来的。

2.to_char方式:

把time转化为字符串再与传来的数据 2011-05-02 、 2011-05-02 做比较

select * from tablename 
where to_char(time,'yyyy-mm-dd') >= '2011-05-02'    
and to_char(time,'yyyy-mm-dd') <= '2011-05-30' 

查询结果:可以同时显示05-02和05-30的数据。

3. between … and
经常看到有人在某段时间区间上喜欢用between … and … ,其实,可以直接地说:这种用法是错误的!

查看某一天的数据,或某一段时间内的数据,其实是一个左闭、右开的区间内的数据;
例如:我要查一张表 2011年3月11日到2011年3月24日内所生成的数据,其区间应该为[2011-03-11 00:00:00, 2011-03-25 00:00:00)
– 即:不包括右边2011-03-25 00:00:00时间点的值!


4. 等于某日期的查询

格式化传入的日期字符串与数据库比较
select * 
from goods
where g_time=to_date('2018/12/26 10:05:17','yyyy-MM-dd hh:mi:ss');
格式化数据库的日期与传入的日期字符串比较
select *  
from goods
where carnum = '粤BEK735' and 
to_char(damageStartdate, 'yyyy-MM-dd HH24:mi:ss') = '2017-04-05 12:00:00'; 
传入值与数据值模糊匹配
select *  
from goods
where carnum = '粤BEK735' and 
to_char(damageStartdate, 'yyyy-MM-dd HH24:mi:ss')  like '2017-04-05%'; 

 

5. LocalDateTime的使用

        //转换器
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        //当前时间
        LocalDateTime time = LocalDateTime.now();

        //日期转字符串
        String localTime = df.format(time);
        System.out.println("LocalDateTime转成String类型的时间:" + localTime);

        //字符串转日期
        LocalDateTime ldt = LocalDateTime.parse("2017-09-28 17:07:05", df);
        System.out.println("String类型的时间转成LocalDateTime:" + ldt);
        //日期转时间戳
        System.out.println("LocalDateTime:2017-09-28T17:07:05 转时间戳:"+ldt.toEpochSecond(ZoneOffset.of("+8")));


        System.out.println("===================================");

        //LocalDateTime把字符串转日期互转 (不带时、分、秒的)
        DateTimeFormatter dateTimeFormatter1 = DateTimeFormatter.ofPattern("yyyy/M/d");
        
        //字符串转日期
        LocalDate parse = java.time.LocalDate.parse("2016/03/28", dateTimeFormatter1);
        System.out.println("2016/03/28转成日期后:" + parse);

        //日期转字符串
        String format = dateTimeFormatter1.format(parse);
        System.out.println("2016/03/28转成合适的字符串后:" + format);

运行结果如下:
在这里插入图片描述
 
查看日期范围间隔

  LocalDate today = LocalDate.now();
  System.out.println("Today:" + today);
  LocalDate oldDate = LocalDate.of(2018, 9, 23);
  System.out.println("OldDate:" + oldDate);

  Period p = Period.between(oldDate, today);
  System.out.printf("目标日期距离今天的时间差:%d 年 %d 个月 %d 天\n", p.getYears(), p.getMonths(), p.getDays());

运行结果
在这里插入图片描述
案例:
“startTime”:“2021-10-08 13:21:08”,
“endTime”:“2021-10-08 13:21:12”,
求两者的时间差:

   DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
   LocalDate ldate = LocalDateTime.parse(settlementDetailRequest.getStartTime(), df).toLocalDate();
   LocalDate rdate = LocalDateTime.parse(settlementDetailRequest.getEndTime(), df).toLocalDate();
   Period p = Period.between(ldate, rdate);

   if (p.getYears() > 1 || p.getMonths() > 3 ||(p.getMonths()==3&&p.getDays()>0)) {
       return BaseResp.fail("查询日期范围仅支持三个月!");
   }

 
需求案例:找出所有任务中,离当前时间最近的任务信息,并返回!

    @RequestMapping(value = "/memory",method = RequestMethod.GET)
    @ApiOperation("修理厂录入定损任务-记忆功能")
    @ApiImplicitParam(paramType = "query", name = "openid", value = "修理厂openid", required = true, dataType = "String")
    public R memory(@RequestParam("openid") String openid) {

        // 1.获取该openid下的所有定损任务
        List<KfAppointmentDsEntity> memoryInfo = kfAppointmentDsService.list(new QueryWrapper<KfAppointmentDsEntity>().eq("factory_openid", openid));

        // 2.收集所有已提交的定损任务的-提交时间戳
        List<Long> collect = memoryInfo.stream()
                .map(KfAppointmentDsEntity::getApplyTime)
                .map(x -> {
                    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
                    // 把Date格式转化为时间戳
                    return LocalDateTime.parse(x, formatter).toEpochSecond(ZoneOffset.of("+8"));
                }).collect(Collectors.toList());

        // 3.通过比较时间戳大小,收集最近一次的提交时间戳
        Long maxTime = collect.get(0);
        for (int i = 0; i < collect.size(); i++) {
            if (maxTime < collect.get(i)) {
                maxTime = collect.get(i);
            }
        }

        // 4.收集最近一次提交时间的任务信息
        Long finalMaxTime = maxTime;
        List<KfAppointmentDsEntity> entitity = memoryInfo.stream().filter(x -> {
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            Long everyTime = LocalDateTime.parse(x.getApplyTime(), formatter).toEpochSecond(ZoneOffset.of("+8"));
            return everyTime.equals(finalMaxTime);
        }).collect(Collectors.toList());

		// 5.属性copy,并返回
        MemoryVo memoryVo = new MemoryVo();
        BeanUtils.copyProperties(entitity.get(0),memoryVo);
        return R.ok().put("data",memoryVo);
    }

 

6. between…and查询日期时存在的问题

场景:用 select * from TABLE where date between ‘2009-1-22’ And ‘2009-1-22’ ,或者 select * from TABLE where date >= ‘2009-1-22’ And date <= ‘2009-1-22’ 查询当天数据,结果查不到数据。

原因:短日期类型默认Time为00:00:00,所以当使用between作限制条件时,就相当于 between ‘2009-1-22 00:00:00’ and ‘2009-1-22 00:00:00’,因此就查不出数据。使用 >=、<= 时同理

解决方法:

方法一
如果 2009-01-23 00:00:00 没有业务发生的话,也可以让前端直接加一天,写成

select * from table where date between '2009-01-22' and '2009-01-23' 

但这样写会包含 2009-01-23 00:00:00 !
也可后端直接增加一天,后端解决代码如下:

public class MianTest {
    public static void main(String[] args) {

        //转换器
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //当前日期
        Date date = new Date(2021-1900,1,28);
        System.out.println(format.format(date)+"+++++++++");
        
        Calendar instance = Calendar.getInstance();
        //把当前日期放进去
        instance.setTime(date);
        //日期向后推一天
        instance.add(instance.DATE,1);

        //这个日期就是日期向后推 1天 的结果
        Date realyTime = instance.getTime();
        System.out.println(format.format(realyTime)+"+++++++++");
    }
}

结果如下:
在这里插入图片描述

方法二
如果用String接收的日期,可以让前端按如下方式传数据
在这里插入图片描述
我们在后台对endTime进行修改,增加59:59:59

  //endTime增加一天
  String endTime = vo.getEndTime();
  String replaceTime = endTime.replace("00:00:00", "59:59:59");
  vo.setEndTime(replaceTime);

也可完成正确的查询结果

方法三:
如果用Data接收的日期,可以让前端还按这种方式传数据
在这里插入图片描述
后台为Data类型的endTime增加59:59:59

public class MianTest {
    public static void main(String[] args) {

        //转换器
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //当前日期
        Date date = new Date(2021-1900,1,8);
        System.out.println(format.format(date)+"+++++++++");

        Calendar instance = Calendar.getInstance();
        //把当前日期放进去
        instance.setTime(date);
        //日期向后推,整数往后推,负数向前推
        instance.add(Calendar.HOUR,23);
        instance.add(Calendar.MINUTE,59);
        instance.add(Calendar.SECOND,59);

        //这个日期就是日期向后推 23:59:59的结果
        Date realyTime = instance.getTime();
        System.out.println(format.format(realyTime)+"+++++++++");
    }
}

结果如下,也可达成正确的效果
在这里插入图片描述

  • 6
    点赞
  • 53
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值