date time 分开存储如何合并_如何将java.sql.Date和java.sql.Time合并到java.util.Date?

I have two objects: a java.sql.Date and a java.sql.Time.

What is the best way to merge them into single java.util.Date?

In the database those columns are stored separately. I get them by JDBC rs.getDate("Date") and rs.getTime("Time");.

解决方案

You can create two Calendar instances. In the first you initialize the date and in the latter the time. You can the extract the time values from the "time" instance and set them to the "date".

// Construct date and time objects

Calendar dateCal = Calendar.getInstance();

dateCal.setTime(date);

Calendar timeCal = Calendar.getInstance();

timeCal.setTime(time);

// Extract the time of the "time" object to the "date"

dateCal.set(Calendar.HOUR_OF_DAY, timeCal.get(Calendar.HOUR_OF_DAY));

dateCal.set(Calendar.MINUTE, timeCal.get(Calendar.MINUTE));

dateCal.set(Calendar.SECOND, timeCal.get(Calendar.SECOND));

// Get the time value!

date = dateCal.getTime();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值