Explain differences among java.util.Date, java.sql.Date, java.sql.Time, and java.sql.Timestamp?

本文对比了java.util.Date、java.sql.Date、java.sql.Time及java.sql.Timestamp的区别,并通过实例介绍了如何使用SimpleDateFormat进行日期时间格式化,以及如何利用GregorianCalendar进行日期时间的有效验证。

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

Explain differences among java.util.Date, java.sql.Date, java.sql.Time, and java.sql.Timestamp?

 

java.util.Date

             |

java.sql.Date    java.sql.Time java.sql.Timestamp

java.util.Date - class supports both the Date (i.e. year/month/date etc) and the Time (hour, minute, second, and millisecond) components.

java.sql.Date - class supports only the Date (i.e. year/month/date etc) component. The hours, minutes, seconds and milliseconds of the Time component will be set to zero in the particular time zone with which the instance is associated.

java.sql.Time - class supports only Time (i.e. hour, minute, second, and millisecond) component. The date components should be set to the "zero epoch" value of January 1, 1970 and should not be accessed.

java.sql.TimeStamp – class supports both Date (i.e. year/month/date etc) and the Time (hour, minute, second,millisecond and nanosecond) components.

Note: the subtle difference between java.util.Date and java.sql.Date. The java.sql.Date does not have a time component. If you need both date and time, then should use either java.util.Date or java.sql.TimeStamp. To keep track of time Java counts the number of milliseconds from January 1, 1970 and stores it as a long value in java.util.Date class. The GregorianCalendar class provides us a way to represent an arbitrary date. The GregorianCalendar class also provides methods for anipulating dates (date arithmetic, date comparisons etc).

 

private static SimpleDateFormat sdf = new SimpleDateFormat(

                            "yyyy-MM-dd HH:mm:ss");

private static final int FIVEMIN = 5 * 60 * 1000;

private static final int ONEYEAR = 365 * 24 * 60 * 60 * 1000;

 

/* check time format: yyyy-MM-dd HH:mm:ss */

                   Date scheDate = null;

                   try {

                                     String time = req.getTime();

                                     scheDate = sdf.parse(time);

                            }

                   } catch (Exception e) {

                            log.error(e, e);

                // do something.

                   }

 

                   /* check time between 5 minutes and 1 year */

                   GregorianCalendar gc = new GregorianCalendar();

                   if (scheDate != null) {

                            if (scheDate.getTime() < (gc.getTimeInMillis() + FIVEMIN)

                                               || scheDate.getTime() > (gc.getTimeInMillis() + ONEYEAR)) {

                                     // do something.

                            }

                   }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值