【Java】JAVA与JS中 日期相关处理

本文介绍了JavaScript中获取当前日期和时间的方法,以及在前后台之间进行String与Date转换的注解使用。同时讲解了Java中Date、String和Calendar的相互转换,并提供了Oracle数据库中日期比较的各种SQL操作示例。
摘要由CSDN通过智能技术生成

一、js获取当前年月日

    var date = new Date();
        date .getYear();                  //获取当前年份(2位)
        date .getFullYear();              //获取完整的年份(4位)
        date .getMonth();                 //获取当前月份(0-11,0代表1月)
        date .getDate();                  //获取当前日(1-31)
        date .getDay();                   //获取当前星期 X(0-6,0代表星期天)
        date .getTime();                  //获取当前时间(从1970.1.1开始的毫秒数)
        date .getHours();                 //获取当前小时数(0-23)
        date .getMinutes();               //获取当前分钟数(0-59)
        date .getSeconds();               //获取当前秒数(0-59)
        date .getMilliseconds();          //获取当前毫秒数(0-999)
        date .toLocaleDateString();       //获取当前日期
    var mytime=date.toLocaleTimeString(); //获取当前时间
        date.toLocaleString( );           //获取日期与时间

二、前后台传值过程中String与Date的转换

    1、前端传入String类型的时间,转化为Date
        @DateTimeFormat(pattern ="yyyy-MM-dd")
        
    【注】1、pattern 设置前端传入的数据格式,告诉框架,方便解析。
         2、前端输入String类型 2021-2-5 被解析为Date类型 Fri Feb 05 00:00:00 CST 2021
        
    2、后端返回Date类型的时间到前端,转换为String
        @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone="GMT+8") 
        
    【注】(1)pattern 设置返回到前端的数据格式
         (2)使用@JsonFormat引起的时间比正常时间慢8小时,默认情况下timeZone为GMT(即标准时区)
        所以改为背景时间需要加上timezone="GMT+8"
    
    public class Employee{
           //自增ID
           private Integer id;
           
           @DateTimeFormat(pattern ="yyyy-MM-dd")
           @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone="GMT+8")
           //创建时间
           private Date createTime; 
    
    }

三、JAVA中Date与String和Calendar的相互转换

在这里插入图片描述JAVA中Date类的使用 - LiuShuku - 博客园

    【注】
    String:2021-2-5  
    new Date(): Fri Feb 05 00:00:00 CST 2021
    一般又要把date转换为String,而数据库又是date
    
    1、Date与String之间的转换:
    //1.Date转化String
    SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
    String dateStr=sdf.format(new Date());
    //2.String转化Date
    String str="2021-2-5";
    SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
    Date birthday = sdf.parse(str);
    
    2、Date个Calendar之间的转换
    //1.Date 转化为Calendar
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(new Date());
    //2.Calenda转换为Date
     Calendar calendar = Calendar.getInstance();
     Date date =calendar.getTime();
    
    3、Calendar和String对象之间的转换
    //1.Calendar转化String
    Calendar calendat = Calendar.getInstance();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    String dateStr = sdf.format(calendar.getTime());
    //2.String转化Calendar
    String str="2021-2-5";
    SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
    Date date =sdf.parse(str);
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);

四、Oracle 数据库比较日期大小

1、小于、大于、等于
select * from JN_BUS_KJLWSBJBXX where dqsj <= to_date('2021-09-07 00:00:00','yyyy-mm-dd hh24:mi:ss')
select * from JN_BUS_KJLWSBJBXX where dqsj >= to_date('2021-09-07 00:00:00','yyyy-mm-dd hh24:mi:ss')
select * from JN_BUS_KJLWSBJBXX where dqsj = to_date('2021-09-07 00:00:00','yyyy-mm-dd hh24:mi:ss')
SELECT * from ZYZB_JZ_FDMHL_ZPH where to_char(dqsj,'yyyy-mm-dd hh24:mi:ss') = '2021-09-23 00:00:00'
​
2、to_char to_date 等于某个时刻
select IDS , HAOMRQ from Employee where 1=1 and '2021-10-24' = to_char(HAOMRQ,'yyyy-mm-dd') order by dcno ASC
select IDS , HAOMRQ from Employee where 1=1 and HAOMRQ = to_date('2021-10-24 09:12:23','yyyy-mm-dd hh24:mi:ss') order by dcno ASC
​
3、在某段时间内的比较:
select * from JN_BUS_KJLWSBJBXX where dqsj between to_date('2021-07-07 00:00:00','yyyy-mm-dd hh24:mi:ss') and to_date('2021-09-07 00:00:00','yyyy-mm-dd hh24:mi:ss')
select * from JN_BUS_KJLWSBJBXX where dqsj <= to_date('2021-09-07 00:00:00','yyyy-mm-dd hh24:mi:ss') and update >= to_date('2021-07-07 00:00:00','yyyy-mm-dd hh24:mi:ss')
​
4、向Date类型插入自动的系统时间 ,HAOMRQ, dqsj都是Date类型的数据
insert into Employee(IDS , HAOMRQ, dqsj)values(666,sysdate,sysdate);
​
5、oracle语句查询昨天的数据
select IDS, HAOMRQ , dqsj from Employee where to_char(HAOMRQ,'yyyy-MM-dd')=to_char(sysdate-1,'yyyy-MM-dd') order by dcno ASC
​
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Mateo-520

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

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

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

打赏作者

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

抵扣说明:

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

余额充值