java.util.Date

本文详细介绍了 Java 中 Date 类的功能及使用方法,包括毫秒级精度的时间表示、与 Calendar 和 DateFormat 类的关系,以及如何处理日期和时间字段的转换。

java.util
Class Date

java.lang.Object
  java.util.Date
All Implemented Interfaces:
Cloneable, Comparable, Serializable
Direct Known Subclasses:
Date, Time, Timestamp

public class Date extends Object implements Serializable, Cloneable, Comparable

The classDaterepresents a specific instant in time, with millisecond precision.

这个类指定了一个时间,用毫秒表示

Prior to JDK1.1, the classDatehad 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 JDK1.1, theCalendarclass should be used to convert between dates and time fields and theDateFormatclass should be used to format and parse date strings. The corresponding methods inDateare deprecated.

在jdk1.1前,这个类附加了两个功能,它允许时间有年月日时分和秒的值。它也允许用字符串格式化时间。

不幸地,是它不这些API函数不支持国际化。jdk1.1,calendar类可以用来转换在日期和时间字段,和DateFormat类可以用来转换

时间字符传,对应的方法在date是被定义的

Although theDateclass is intended to reflect coordinated universal time (UTC), it may not do so exactly, depending on the host environment of the Java Virtual Machine. Nearly all modern(现代的) operating systems assume that 1day= 24×60×60= 86400 seconds in all cases. In UTC, however, about once every year or two there is an extra(额外的) second, called a "leap second." The leap second is always added as the last second of the day, and always on December 31 or June 30. For example, the last minute of the year 1995 was 61 seconds long, thanks to an added leap second. Most computer clocks are not accurate enough to be able to reflect the leap-second distinction.

通过这个date类得到的时间,它也许不是精确的,依赖在这java虚拟机。几乎现在的操作时间是:

1day=24x60x60=86400秒。然而,每隔一两年就有额外的秒,它总是加到这天的最后秒上,

和一直在12月的号或者6月的30号,例如,这1995年最后一分钟是61秒长,由于加了跳跃的妙。

大多数计算机的钟都不能精确的反应leap-second的区别

Some computer standards(规定) are defined in terms of Greenwich mean time (GMT)

大多数计算机规定的是格林威治时间

### Java 中 `util.Date` 类的使用说明 #### 基本概念 `java.util.Date` 是 Java 提供的一个用于表示特定瞬间的时间/日期类。它包含了自 1970 年 1 月 1 日 00:00:00 GMT 开始经过的毫秒数[^2]。 #### 创建 `Date` 对象 可以通过无参构造函数创建一个代表当前时间的 `Date` 对象,或者通过指定毫秒数来创建过去或未来的某个时刻: ```java // 当前时间 Date currentDate = new Date(); // 特定时间(基于毫秒) long specificTimeInMillis = 1672531199000L; // 示例时间戳 Date specificDate = new Date(specificTimeInMillis); ``` #### 获取日期信息 可以调用 `Date` 的方法获取具体的年份、月份、天数等信息。例如,`getDate()` 方法返回该日期对应的当天数值[^1]。 ```java int dayOfMonth = currentDate.getDate(); System.out.println("Day of the month: " + dayOfMonth); ``` 要注意的是,部分旧版方法已被标记为过时 (deprecated),推荐使用更现代的方式替代它们。 #### 字符串表示形式 利用 `toString()` 可快速得到人类可读的形式展示当前实例所指代的具体时间点[^3]: ```java String dateString = currentDate.toString(); System.out.println(dateString); ``` 对于更加精确控制输出格式的求,则借助于其他工具比如 SimpleDateFormat 或者新的 DateTimeFormatter API 来完成定制化的格式化工作。 #### 数据库交互中的转换 当涉及到数据库操作时,经常要把标准类型的 `java.util.Date` 转换成适合 SQL 查询使用的 `java.sql.Date` 形式[^4]。下面给出了一种简单的实现方式: ```java java.sql.Date sqlDate = new java.sql.Date(currentDate.getTime()); ``` 这里的关键在于提取原始对象内部存储的实际时间值并通过另一种构造器重新封装成目标类型。 #### 结合 Calendar 进行复杂运算 如果仅依靠基础功能无法满足复杂的业务逻辑求的话,那么引入辅助性的组件——即来自同一命名空间下的另一个重要成员 `Calendar` 就显得尤为重要了[^5]。它可以方便地执行诸如加减时间段之类的任务而无手动计算细节层面的东西。 ```java Calendar calendarInstance = Calendar.getInstance(); calendarInstance.setTime(currentDate); // 添加一天 calendarInstance.add(Calendar.DAY_OF_MONTH, 1); Date nextDay = calendarInstance.getTime(); ``` 以上就是关于 `java.util.Date` 主要特性和典型应用场景的一些介绍以及相应代码片段演示。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值