日期vs时间戳vs日历? [英] Date vs TimeStamp vs calendar?

查看:148
本文介绍了日期vs时间戳vs日历?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有时会被Java中不同的Date类型和实际使用困惑。这里
i我试图总结我的理解

I sometimes get confused by the different Date types in java and their practical usage. Here i am trying to summarize my understanding

java.sql.Date: - 一个薄的包装一个毫秒值,允许JDBC将其标识为SQL DATE值

java.sql.Date :- A thin wrapper around a millisecond value that allows JDBC to identify this as an SQL DATE value

java.sql.Timestamp: - A java.util.Date周围的薄包装,允许JDBC API将其标识为SQL
TIMESTAMP值。它增加了保存SQL TIMESTAMP小数秒值的能力,通过允许将分数秒的规格为
到精确度为纳秒。

java.sql.Timestamp :- A thin wrapper around java.util.Date that allows the JDBC API to identify this as an SQL TIMESTAMP value. It adds the ability to hold the SQL TIMESTAMP fractional seconds value, by allowing the specification of fractional seconds to a precision of nanoseconds

我已经看到大部分项目喜欢Timestamp而不是日期。我认为这样做的主要原因是Timestamp可以保持这个值,直到
纳秒精度,而Data可以持续到毫秒。正确?

I have seen most of the projects prefer Timestamp instead of date. I think the main reason for this is that Timestamp can hold the value till nano seconds precision whereas Data can hold till milli seconds. Correct?

日历: - 此类专为日期操作而设计,例如: - 用于在特定时刻之间转换在时间上和一个
的一组日历字段,如YEAR,MONTH,DAY_OF_MONTH,HOUR等等,并且用于操纵日历字段,例如获得
的下一周的日期。虽然我没有知道为什么当只有一个实现存在时,这个类是抽象的,即GregorianCalendar。

Calendar :- This class is designed for date manipulation for example :- for converting between a specific instant in time and a set of calendar fields such as YEAR, MONTH, DAY_OF_MONTH, HOUR, and so on, and for manipulating the calendar fields, such as getting the date of the next week.Though i dont know why this class is abstract when only one implementation exists i.e GregorianCalendar.

推荐答案

java。 sql.Timestamp 围绕 java.util.Date 的简单包装器,允许JDBC API将其标识为SQL TIMESTAMP值。

java.sql.Timestamp A thin wrapper around java.util.Date that allows the JDBC API to identify this as an SQL TIMESTAMP value.

如果您检查 java.sql.Timestamp JavaDoc ,这个类 java.util扩展是非常明确的。日期(如 java。 sql.Date )。而在现实世界的项目中,您必须在将数据存储在数据库中时通常 java.util.Date ,而且主要是 java.sql.Timestamp 因为它存储日期和时间值,而 java.sql.Date 只存储日期值。

If you check java.sql.Timestamp JavaDoc, it is very explicit that this class extends from java.util.Date (as java.sql.Date does). And in real world projects you must plain java.util.Date when storing the data in your database and mostly java.sql.Timestamp since it stores date and time value, while java.sql.Date just stores date value.

另一方面, java.util.Calendar 是抽象的,因为除了 java.util.GregorianCalendar 。如果您从HotSpot看到代码 Calendar#getInstance ,您将看到它调用 createCalendar(TimeZone.getDefaultRef(),Locale.getDefault(Locale .Category.FORMAT)),此方法代码使用3个不同的日历: BuddhistCalendar JapaneseImperialCalendar GregorianCalendar 。该代码从JDK 7源代码复制:

On the other hand, java.util.Calendar is abstract since there are more implementations of this apart from java.util.GregorianCalendar. If you see the code of Calendar#getInstance from HotSpot, you will see that it calls createCalendar(TimeZone.getDefaultRef(), Locale.getDefault(Locale.Category.FORMAT)), and this method code uses 3 different calendars: BuddhistCalendar, JapaneseImperialCalendar and GregorianCalendar. This code is copied from JDK 7 source:

private static Calendar createCalendar(TimeZone zone,
                                       Locale aLocale) {
    Calendar cal = null;

    String caltype = aLocale.getUnicodeLocaleType("ca");
    if (caltype == null) {
        // Calendar type is not specified.
        // If the specified locale is a Thai locale,
        // returns a BuddhistCalendar instance.
        if ("th".equals(aLocale.getLanguage())
                && ("TH".equals(aLocale.getCountry()))) {
            cal = new BuddhistCalendar(zone, aLocale);
        } else {
            cal = new GregorianCalendar(zone, aLocale);
        }
    } else if (caltype.equals("japanese")) {
        cal = new JapaneseImperialCalendar(zone, aLocale);
    } else if (caltype.equals("buddhist")) {
        cal = new BuddhistCalendar(zone, aLocale);
    } else {
        // Unsupported calendar type.
        // Use Gregorian calendar as a fallback.
        cal = new GregorianCalendar(zone, aLocale);
    }

    return cal;
}

现在,为什么直接用 Calendar 而不是 GregorianCalendar ?因为在提供而不是直接使用实现时,必须使用抽象类和接口。这更好地解释了:编程到界面是什么意思?

Now, why to work directly with Calendar instead of GregorianCalendar? Because you must work with abstract classes and interfaces when provided instead of working directly with implementations. This is better explained here: What does it mean to "program to an interface"?

除此之外,如果您使用日期和时间,我建议使用图书馆,如 Joda- Time 已经处理并解决了当前Java Date API的许多问题,并提供了在 java.util.Date 中检索此日期和时间对象的方法。味道。

Apart from this, if you will work with date and times, I recommend using a library like Joda-Time that already handles and solves lot of the problems with the current Java Date API and also provides methods to retrieve this date and times object in java.util.Date flavor.

这篇关于日期vs时间戳vs日历?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆