Date、TimeStamp和Calendar的使用

本文详细介绍了Java中Date、Timestamp和Calendar类的使用方法及其区别。包括Date类的基本使用、构造函数及常用方法;Timestamp类的特点,如纳秒级别的精度;以及Calendar类如何维护和操作日期相关域。

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

Date、TimeStamp和Calendar都是用来表示时间的,当时在日常的使用还是比较容易混淆使用场景.

1. Date

Date一般用在应用中只需要获取当前的时间戳,并没有对时间相关操作的需要,例如当前时间一周前.如果是这种情形就可以使用Date来满足开发的需求,同时结合使用SimpleDateFormat来格式化时间的输出格式。

Date类的构造函数存在两种形式:
1. Date():创建表示当前时间的实例;
2. Date(long millisssSinceEpoch):创建给定时间的实例,参数表示从GMT 1970年1月1日00:00:00以来的毫秒数;注意这里表示的时间是绝对的,并不依赖所在的区域(locale)

Prior to JDK 1.1, the class Date had two additional functions. It allowed the interpretation of dates as year, month, day, hour, minute, and second values. It also allowed the formatting and parsing of date strings. Unfortunately, the API for these functions was not amenable to internationalization. As of JDK 1.1, the Calendar class should be used to convert between dates and time fields and the DateFormat class should be used to format and parse date strings. The corresponding methods in Date are deprecated.

Date当中的大部分方法都已经被废弃了,主要的原因是其缺乏国际化的支持,但其中还有两个方法仍可以使用:
1. long getTime():返回自1970年1月1日00:00:00 GMT已经过去了多少毫秒;
2. String toString():使用默认的区域返回当前时区下的日期/时间字符串,格式为“dow mon dd hh:mm:ss zzz yyyy”,其中“dow”表示一周的第几天,“mon”表示月份, “zzz”表示时区,toString方法返回的规定格式的时间,你可以使用SimpleDateFormat去控制具体的显示格式:

import java.text.SimpleDateFormat;
import java.util.Date;
public class DateTest {
    public static void main(String[] args) {
        Date date = new Date();
        System.out.println("toString()=" + date); //固定的格式: dow mon dd hh:mm:ss zzz yyyy

        //SimpleDateFormat可以用来控制日期/时间的输出格式
        //E (表示一周的第几天), 3E、2E或E仅显示三个字符(例如:Mon),4E即以上都显示全称(例如:Monday)
        //M(月份), M(数字), MM(带有补0的数字), 3M显示三个字符(例如:Jau), >3M显示全称(例如:January)
        //h(小时), h, hh(带有补0的数字)
        //m(分钟)
        //s(秒)
        //a(am/pm)
        //H(0~23中的小时)
        //z(时区) 
        SimpleDateFormat dateFormatter = new SimpleDateFormat("E, y-M-d ' at' h:m:s a z");
        System.out.println("Format 1: " + dateFormatter.format(date));

        dateFormatter = new SimpleDateFormat("E yyyy.MM.dd 'at' hh:mm:ss a zzz");
        System.out.println("Format 2: " + dateFormatter.format(date));

        dateFormatter = new SimpleDateFormat("EEEE, MMMM d, yyyy");
        System.out.println("Fomrat 3: " + dateFormatter.format(date));
    }
}

执行的结果:

toString()=Sun Aug 21 22:36:57 CST 2016
Format 1: Sun, 2016-8-21  at 10:36:57 PM CST
Format 2: Sun 2016.08.21 at 10:36:57 PM CST
Fomrat 3: Sunday, August 21, 2016

2 Timestamp

在java jdbc的api中定义了三个跟时间操作相关的类:java.sql.Date、java.sql.Timestamp和java.sql.Time.它们和ANSI SQL中的DATE、TIME和TIMESTAMP向对应.

而这三者的之间也是很容易进行区分:
1. java.sql.Time:仅仅表示时间;
2. java.sql.Date:仅仅表示日期;
3. java.sql.Timestamp:表示日期和时间;

另外java.util.Date和java.sql.Timestamp的区别主要在于Timestamp可以表示纳秒级别的时间精度.

3 Calendar

Calendar类提供如下支持:
1. 维护一组日期相关的域的信息,例如: YEAR, MONTH, DAY_OF_MONTH, HOUR, MINUTE, SECOND, MILLISECOND;
2. 提供操作上面域的方法;

to be continued

参考:
1. What is difference between java.sql.Time, java.sql.Timestamp and java.sql.Date - JDBC interview Question
2. 深入理解Java:SimpleDateFormat安全的时间格式化
3. Date and Time - Creation, Operation and Formatting

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值