Calendar_Date_String三者的转换

本文介绍了如何在Java中进行日期与字符串之间的相互转换,包括使用Calendar、Date及SimpleDateFormat类实现不同格式的日期转换,并提供了一个将Java日期插入Oracle数据库的具体实例。

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

1.Calendar 转化 String
//获取当前时间的具体情况,如年,月,日,week,date,分,秒等
Calendar calendat = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat(“yyyy-MM-dd”);
String dateStr = sdf.format(calendar.getTime());

2.String 转化Calendar
String str=”2010-5-27”;
SimpleDateFormat sdf= new SimpleDateFormat(“yyyy-MM-dd”);
Date date =sdf.parse(str);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);

3.Date 转化String
SimpleDateFormat sdf= new SimpleDateFormat(“yyyy-MM-dd”);
String dateStr=sdf.format(new Date());

4.String 转化Date
String str=”2010-5-27”;
SimpleDateFormat sdf= new SimpleDateFormat(“yyyy-MM-dd”);
Date birthday = sdf.parse(str);

5.Date 转化Calendar
Calendar calendar = Calendar.getInstance();
calendar.setTime(new java.util.Date());

6.Calendar转化Date
Calendar calendar = Calendar.getInstance();
java.util.Date date =calendar.getTime();

下面有个例子,插入oralce表类型为DATE的数据

SimpleDateFormat sdf = new SimpleDateFormat(“yyyyMMdd HHmmss”);
String date = sdf.format(new Date());
—————————————–
String sql = “Insert into SM_SEND (*******) ” +
“values (*********************
“’,1,” + “to_date(‘” +date+ “’,’YYYY-MM-DD HH24:MI:SS’)” +
“,” + “to_date(‘” +date+ “’,’YYYY-MM-DD HH24:MI:SS’)” +
“,0,null,null,null,null,null,null,null)”;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值