JAVA与JS中 日期相关处理

本文介绍了如何使用JavaScript获取当前日期和时间,字符串与Date的双向转换,以及在Oracle数据库中进行日期比较的方法,包括小于、大于、等于操作和时间范围查询。还涵盖了前后台传值中时间格式的转换和常用的日期格式化工具如SimpleDateFormat和Calendar的用法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、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的相互转换

在这里插入图片描述
https://www.cnblogs.com/gu-bin/p/10022703.html

	【注】
	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

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值