oracle中日期與字符串的相互轉(zhuǎn)化的方法詳解
1.字符串轉(zhuǎn)為日期格式(to_date)
例1:把字符串類型2005-01-01 13:14:20 轉(zhuǎn)成 2005/1/1 13:14:20日期格式
select to_date('2005-01-01 13:14:20','yyyy-MM-dd HH24:mi:ss') from dual;
結(jié)果:

例2:把字符串類型30-11月-19 轉(zhuǎn)成 2018/12/31 日期格式
select to_date('30-11月-19 ', 'dd-mon-yy') from dual
結(jié)果:

注意:字符串轉(zhuǎn)日期時(shí),字符串和日期格式要匹配,如字符串格式為30-11月-19,如果后邊跟yyyy-MM-dd就會(huì)報(bào)格式不匹配的錯(cuò)誤,必須使用dd-mon-yy
2.日期格式轉(zhuǎn)字符串(to_char)
例1:把sysdate(2020/5/12 17:31:23)轉(zhuǎn)化為yyyy-MM-dd HH24:mi:ss字符串格式
select to_char(sysdate,'yyyy-MM-dd HH24:mi:ss') from dual;
結(jié)果:

例2:表car.lp_totallpdata中有一個(gè)字段enddate字段,代表了結(jié)束日期。enddate字段中的數(shù)據(jù)格式是varchar2類型的:(30-11月-19),現(xiàn)在要求查出表中結(jié)束日期等于字符串’2019-11’的數(shù)據(jù)

也就是說(shuō)找出enddate = ‘2019-11’的數(shù)據(jù)
分析:
首先30-11月-19 和 2019-12都屬于字符串類型的,但是他們的格式不一樣,我們可以先把enddate字段中的數(shù)據(jù)轉(zhuǎn)化為正常的日期格式,再把他轉(zhuǎn)化為字符串,看他與2019-12是否相等
1.先把enddate字段中的數(shù)據(jù)轉(zhuǎn)化為正常的日期格式
to_date(t.enddate, 'dd-mon-yy') //先轉(zhuǎn)化為日期30-11月-19==> 2019/11/30
2.再把他轉(zhuǎn)化為我們想要的字符串
to_char(to_date(t.enddate, 'dd-mon-yy'),'yyyy-mm') // 2019/11/30 ==> 2019-11
3.完整的過(guò)濾sql
select t.* from car.lp_totallpdata t where to_char(to_date(t.enddate, 'dd-mon-yy'),'yyyy-mm')='2019-11'
3.日期范圍查詢
日期的范圍查詢,假設(shè)要查詢 2011-05-02 到 2011-05-30 之間的數(shù)據(jù)
這條查詢語(yǔ)句有兩種實(shí)現(xiàn)方式:
假設(shè)數(shù)據(jù)庫(kù)的字段 time 為 日期類型,傳來(lái)的數(shù)據(jù)為字符串類型?。?!
1. to_date 方式
把傳來(lái)的數(shù)據(jù) 2011-05-02 、 2011-05-02 轉(zhuǎn)化為日期再與time比較
select * from tablename
where time >= to_date('2011-05-02','yyyy-mm-dd')
and time <= to_date('2011-05-30','yyyy-mm-dd')
運(yùn)行的結(jié)果是:可以顯示05-02的數(shù)據(jù),但是不能顯示05-30的數(shù)據(jù)。
解決方案:
①如果想顯示05-30的數(shù)據(jù)可以<to_date(‘2011-05-31’,‘yyyy-mm-dd’),這樣就能顯示30號(hào)的了。
②如果想要顯示05-30的數(shù)據(jù)可以<=to_date(‘2011-05-30 23:59:59 999’,‘yyyy-mm-dd hh24:mi:ss’)也是可以查出來(lái)的。
2.to_char方式:
把time轉(zhuǎn)化為字符串再與傳來(lái)的數(shù)據(jù) 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'
查詢結(jié)果:可以同時(shí)顯示05-02和05-30的數(shù)據(jù)。
3. between … and
經(jīng)??吹接腥嗽谀扯螘r(shí)間區(qū)間上喜歡用between … and … ,其實(shí),可以直接地說(shuō):這種用法是錯(cuò)誤的!
查看某一天的數(shù)據(jù),或某一段時(shí)間內(nèi)的數(shù)據(jù),其實(shí)是一個(gè)左閉、右開的區(qū)間內(nèi)的數(shù)據(jù);
例如:我要查一張表 2011年3月11日到2011年3月24日內(nèi)所生成的數(shù)據(jù),其區(qū)間應(yīng)該為[2011-03-11 00:00:00, 2011-03-25 00:00:00)
– 即:不包括右邊2011-03-25 00:00:00時(shí)間點(diǎn)的值!
4. 等于某日期的查詢
格式化傳入的日期字符串與數(shù)據(jù)庫(kù)比較
select *
from goods
where g_time=to_date('2018/12/26 10:05:17','yyyy-MM-dd hh:mi:ss');
格式化數(shù)據(jù)庫(kù)的日期與傳入的日期字符串比較 select * from goods where carnum = '粵BEK735' and to_char(damageStartdate, 'yyyy-MM-dd HH24:mi:ss') = '2017-04-05 12:00:00';
傳入值與數(shù)據(jù)值模糊匹配 select * from goods where carnum = '粵BEK735' and to_char(damageStartdate, 'yyyy-MM-dd HH24:mi:ss') like '2017-04-05%';
5. LocalDateTime的使用
//轉(zhuǎn)換器
DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
//當(dāng)前時(shí)間
LocalDateTime time = LocalDateTime.now();
//日期轉(zhuǎn)字符串
String localTime = df.format(time);
System.out.println("LocalDateTime轉(zhuǎn)成String類型的時(shí)間:" + localTime);
//字符串轉(zhuǎn)日期
LocalDateTime ldt = LocalDateTime.parse("2017-09-28 17:07:05", df);
System.out.println("String類型的時(shí)間轉(zhuǎn)成LocalDateTime:" + ldt);
//日期轉(zhuǎn)時(shí)間戳
System.out.println("LocalDateTime:2017-09-28T17:07:05 轉(zhuǎn)時(shí)間戳:"+ldt.toEpochSecond(ZoneOffset.of("+8")));
System.out.println("===================================");
//LocalDateTime把字符串轉(zhuǎn)日期互轉(zhuǎn) (不帶時(shí)、分、秒的)
DateTimeFormatter dateTimeFormatter1 = DateTimeFormatter.ofPattern("yyyy/M/d");
//字符串轉(zhuǎn)日期
LocalDate parse = java.time.LocalDate.parse("2016/03/28", dateTimeFormatter1);
System.out.println("2016/03/28轉(zhuǎn)成日期后:" + parse);
//日期轉(zhuǎn)字符串
String format = dateTimeFormatter1.format(parse);
System.out.println("2016/03/28轉(zhuǎn)成合適的字符串后:" + format);
運(yùn)行結(jié)果如下:

查看日期范圍間隔
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("目標(biāo)日期距離今天的時(shí)間差:%d 年 %d 個(gè)月 %d 天\n", p.getYears(), p.getMonths(), p.getDays());
運(yùn)行結(jié)果

案例:
“startTime”:“2021-10-08 13:21:08”,
“endTime”:“2021-10-08 13:21:12”,
求兩者的時(shí)間差:
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("查詢?nèi)掌诜秶鷥H支持三個(gè)月!");
}
需求案例:找出所有任務(wù)中,離當(dāng)前時(shí)間最近的任務(wù)信息,并返回!
@RequestMapping(value = "/memory",method = RequestMethod.GET)
@ApiOperation("修理廠錄入定損任務(wù)-記憶功能")
@ApiImplicitParam(paramType = "query", name = "openid", value = "修理廠openid", required = true, dataType = "String")
public R memory(@RequestParam("openid") String openid) {
// 1.獲取該openid下的所有定損任務(wù)
List<KfAppointmentDsEntity> memoryInfo = kfAppointmentDsService.list(new QueryWrapper<KfAppointmentDsEntity>().eq("factory_openid", openid));
// 2.收集所有已提交的定損任務(wù)的-提交時(shí)間戳
List<Long> collect = memoryInfo.stream()
.map(KfAppointmentDsEntity::getApplyTime)
.map(x -> {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
// 把Date格式轉(zhuǎn)化為時(shí)間戳
return LocalDateTime.parse(x, formatter).toEpochSecond(ZoneOffset.of("+8"));
}).collect(Collectors.toList());
// 3.通過(guò)比較時(shí)間戳大小,收集最近一次的提交時(shí)間戳
Long maxTime = collect.get(0);
for (int i = 0; i < collect.size(); i++) {
if (maxTime < collect.get(i)) {
maxTime = collect.get(i);
}
}
// 4.收集最近一次提交時(shí)間的任務(wù)信息
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查詢?nèi)掌跁r(shí)存在的問(wèn)題
場(chǎng)景:用 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’ 查詢當(dāng)天數(shù)據(jù),結(jié)果查不到數(shù)據(jù)。
原因:短日期類型默認(rèn)Time為00:00:00,所以當(dāng)使用between作限制條件時(shí),就相當(dāng)于 between ‘2009-1-22 00:00:00’ and ‘2009-1-22 00:00:00’,因此就查不出數(shù)據(jù)。使用 >=、<= 時(shí)同理
解決方法:
方法一:
如果 2009-01-23 00:00:00 沒有業(yè)務(wù)發(fā)生的話,也可以讓前端直接加一天,寫成
select * from table where date between '2009-01-22' and '2009-01-23'
但這樣寫會(huì)包含 2009-01-23 00:00:00 !
也可后端直接增加一天,后端解決代碼如下:
public class MianTest {
public static void main(String[] args) {
//轉(zhuǎn)換器
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//當(dāng)前日期
Date date = new Date(2021-1900,1,28);
System.out.println(format.format(date)+"+++++++++");
Calendar instance = Calendar.getInstance();
//把當(dāng)前日期放進(jìn)去
instance.setTime(date);
//日期向后推一天
instance.add(instance.DATE,1);
//這個(gè)日期就是日期向后推 1天 的結(jié)果
Date realyTime = instance.getTime();
System.out.println(format.format(realyTime)+"+++++++++");
}
}
結(jié)果如下:

方法二:
如果用String接收的日期,可以讓前端按如下方式傳數(shù)據(jù)

我們?cè)诤笈_(tái)對(duì)endTime進(jìn)行修改,增加59:59:59
//endTime增加一天
String endTime = vo.getEndTime();
String replaceTime = endTime.replace("00:00:00", "59:59:59");
vo.setEndTime(replaceTime);
也可完成正確的查詢結(jié)果
方法三:
如果用Data接收的日期,可以讓前端還按這種方式傳數(shù)據(jù)

后臺(tái)為Data類型的endTime增加59:59:59
public class MianTest {
public static void main(String[] args) {
//轉(zhuǎn)換器
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//當(dāng)前日期
Date date = new Date(2021-1900,1,8);
System.out.println(format.format(date)+"+++++++++");
Calendar instance = Calendar.getInstance();
//把當(dāng)前日期放進(jìn)去
instance.setTime(date);
//日期向后推,整數(shù)往后推,負(fù)數(shù)向前推
instance.add(Calendar.HOUR,23);
instance.add(Calendar.MINUTE,59);
instance.add(Calendar.SECOND,59);
//這個(gè)日期就是日期向后推 23:59:59的結(jié)果
Date realyTime = instance.getTime();
System.out.println(format.format(realyTime)+"+++++++++");
}
}
結(jié)果如下,也可達(dá)成正確的效果

以上就是oracle中日期與字符串的相互轉(zhuǎn)化的方法詳解的詳細(xì)內(nèi)容,更多關(guān)于oracle日期與字符串相互轉(zhuǎn)化的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Oracle數(shù)據(jù)庫(kù)數(shù)據(jù)丟失恢復(fù)的幾種方法總結(jié)
相信大家無(wú)論是開發(fā)、測(cè)試還是運(yùn)維過(guò)程中,都可能會(huì)因?yàn)檎`操作、連錯(cuò)數(shù)據(jù)庫(kù)、用錯(cuò)用戶、語(yǔ)句條件有誤等原因,導(dǎo)致錯(cuò)誤刪除、錯(cuò)誤更新等問(wèn)題。當(dāng)你捶胸頓足或嚇得腿軟時(shí),肯定希望有辦法來(lái)恢復(fù)這些數(shù)據(jù)。oracle就提供了一些強(qiáng)大的方法或機(jī)制,可以幫到有需要的你。2016-12-12
關(guān)于Oracle數(shù)據(jù)庫(kù)dbLink的創(chuàng)建和使用詳解
這篇文章主要介紹了關(guān)于Oracle數(shù)據(jù)庫(kù)dbLink的創(chuàng)建和使用詳解,Oracle的數(shù)據(jù)庫(kù)鏈路dbLink是一種允許在兩個(gè)不同的數(shù)據(jù)庫(kù)實(shí)例之間進(jìn)行通信和數(shù)據(jù)交換的功能,它可以讓你在一個(gè)數(shù)據(jù)庫(kù)中訪問(wèn)另一個(gè)數(shù)據(jù)庫(kù)的對(duì)象和數(shù)據(jù),需要的朋友可以參考下2023-08-08
解決The?Network?Adapter?could?not?establish?the?conn問(wèn)題
這篇文章主要介紹了解決The?Network?Adapter?could?not?establish?the?conn問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02
Oracle手動(dòng)建庫(kù)安裝部署超詳細(xì)教程
這篇文章主要介紹了Oracle手動(dòng)建庫(kù)安裝部署超詳細(xì)教程,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03
Oracle數(shù)據(jù)庫(kù)導(dǎo)入導(dǎo)出超詳細(xì)教程
最近做項(xiàng)目的時(shí)候遇到過(guò)oracle數(shù)據(jù)庫(kù)導(dǎo)入導(dǎo)出,在這里我做下記錄,防止自己忘記了,下面這篇文章主要給大家介紹了關(guān)于Oracle數(shù)據(jù)庫(kù)導(dǎo)入導(dǎo)出的相關(guān)資料,需要的朋友可以參考下2023-12-12
Oracle?19c?RAC?手工建庫(kù)的搭建過(guò)程
這篇文章主要介紹了Oracle?19c?RAC?手工建庫(kù)搭建過(guò)程,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-01-01
oracle數(shù)據(jù)庫(kù)索引失效的問(wèn)題及解決
本文總結(jié)了在Oracle數(shù)據(jù)庫(kù)中索引失效的一些常見場(chǎng)景,包括使用isnull、isnotnull、!=、<、>、函數(shù)處理、like前置%查詢以及范圍索引和等值索引同時(shí)存在等情況,通過(guò)實(shí)際的SQL查詢驗(yàn)證,展示了索引失效的原因,并給出了相應(yīng)的優(yōu)化建議2025-01-01

