java基础系列--Date类
原创作品,可以转载,但是请标注出处地址:http://www.cnblogs.com/V1haoge/p/7126930.html
1、Date类概述
Date类是从JDK1.1就开始存在的老类,其提供了针对日期进行操作的诸多方法,但其却一直饱受诟病,不同的起始编号,国际化的低支持,JDK官方也认识到这个问题,后台提出使用Calendar类进行日期操作,日期的格式化交给DateFormat,虽然我们已经不再使用Date类中的大多数方法,但是还有一部分保留的内容指的我们一谈。
2、构造器
Date类之前有6大构造器,其中四个已经标注弃用,我们我不再看他,我们重点看另外两个:
/**
* Allocates a <code>Date</code> object and initializes it so that
* it represents the time at which it was allocated, measured to the
* nearest millisecond.
*
* @see java.lang.System#currentTimeMillis()
*/
public Date() {
this(System.currentTimeMillis());
} /**
* Allocates a <code>Date</code> object and initializes it to
* represent the specified number of milliseconds since the
* standard base time known as "the epoch", namely January 1,
* 1970, 00:00:00 GMT.
*
* @param date the milliseconds since January 1, 1970, 00:00:00 GMT.
* @see java.lang.System#currentTimeMillis()
*/
public Date(long date) {
fastTime = date;
}
第一个构造器是无参构造器,通过调用System的currentTimeMillis()方法来获取当前时间戳,这个时间戳是从1970年到当前时间的毫秒级数据,第二个构造器,可以将一个毫秒级的数据定义为Date格式的日期。
3、常用方法
Date中定义了诸多的日期操作方法,但是大多数都已弃用,只剩余为数不多的几个方法:
/**
* Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT
* represented by this <tt>Date</tt> object.
*
* @return the number of milliseconds since January 1, 1970, 00:00:00 GMT
* represented by this date.
*/
public long getTime() {
return getTimeImpl();
} /**
* Sets this <code>Date</code> object to represent a point in time that is
* <code>time</code> milliseconds after January 1, 1970 00:00:00 GMT.
*
* @param time the number of milliseconds.
*/
public void setTime(long time) {
fastTime = time;
cdate = null;
}
/**
* Tests if this date is before the specified date.
*
* @param when a date.
* @return <code>true</code> if and only if the instant of time
* represented by this <tt>Date</tt> object is strictly
* earlier than the instant represented by <tt>when</tt>;
* <code>false</code> otherwise.
* @exception NullPointerException if <code>when</code> is null.
*/
public boolean before(Date when) {
return getMillisOf(this) < getMillisOf(when);
}
/**
* Tests if this date is after the specified date.
*
* @param when a date.
* @return <code>true</code> if and only if the instant represented
* by this <tt>Date</tt> object is strictly later than the
* instant represented by <tt>when</tt>;
* <code>false</code> otherwise.
* @exception NullPointerException if <code>when</code> is null.
*/
public boolean after(Date when) {
return getMillisOf(this) > getMillisOf(when);
}
上面显示的四个方法是Date类中现在还在使用的几个常用方法:
long getTime()方法:返回从1970年00:00:00到Date对象所代表时间的毫秒级数据
void setTime(long time)方法:设置一个Date对象用来代表从1970年00:00:00开始的一段毫秒级数据后所代表的时间点
boolean before(Date when)方法:判断Date对象所代表的时间点是否在when所代表的时间点之前
boolean after(Date when)方法:判断Date对象所代表的时间点是否在when所代表的时间点之后
4、其他
Date类实现了java.io.Serializable接口,可以执行序列化与反序列化操作,在Date类中定义了writeObject(ObjectOutputStream s)方法和readObject(ObjectInputStream s)方法,分别用于在Date对象进行序列化和反序列化操作时将对象所代表的时间戳(long型数据)进行保存与获取,因为fastTime字段采用transient修饰,其内容会被序列化机制过滤掉,而这个字段内保存的是Date对象所代表时间的时间戳(long型)
有关内容详情见《Java常用API解析——序列化API(一)》
private transient long fastTime;
/**
* Save the state of this object to a stream (i.e., serialize it).
*
* @serialData The value returned by <code>getTime()</code>
* is emitted (long). This represents the offset from
* January 1, 1970, 00:00:00 GMT in milliseconds.
*/
private void writeObject(ObjectOutputStream s)
throws IOException
{
s.writeLong(getTimeImpl());
}
/**
* Reconstitute this object from a stream (i.e., deserialize it).
*/
private void readObject(ObjectInputStream s)
throws IOException, ClassNotFoundException
{
fastTime = s.readLong();
}
5、实例解析:
public static void main(String[] args) {
Date now = new Date();//获取当前时间
Date when = new Date(10201020097865L);//根据时间戳定义指定时间点
boolean b1 = now.after(when);
boolean b2 = now.before(when);
Long d1 = now.getTime();
Long d2 = when.getTime();
System.out.println("now值为:"+now);
System.out.println("when值为:"+when);
System.out.println("b1值为:"+b1);
System.out.println("b2值为:"+b2);
System.out.println("d1值为:"+d1);
System.out.println("d2值为:"+d2);
}
结果为:
now值为:Thu Jul 06 13:39:12 CST 2017
when值为:Tue Apr 04 16:41:37 CST 2293
b1值为:false
b2值为:true
d1值为:1499319552116
d2值为:10201020097865
6、总结
Date类现在并不推荐使用,Java推荐了Calendar和DateFormat,甚至SimpleDateFormat来替代它,Date中仅剩的几个方法仍然还很实用,尤其是before与after方法,可以很方便的判断两个时间点的先后,当然判断的条件是将你的时间转换成Date格式,使用Date剩余的两个构造器实现即可,当然也可以使用推荐的SimpleDateFormat方法进行简单的格式化日期格式字符串的方式得到Date格式的时间点,这些会在稍后了解到!
java基础系列--Date类的更多相关文章
- java基础之Date类
Date类: Date类概述 类 Date 表示特定的瞬间,精确到毫秒. 构造方法 public Date() public Date(long date) 成员方法 public long getT ...
- Java基础教程——Date类和Calendar类
Date类和Calendar类都是关于日期的类,都在java.util包中,使用时需要import. Date java.util.Date类的对象用来表示时间和日期,用得最多的是获取系统当前日期和时 ...
- JAVA基础之Date类、DateFormat类及Calendar类
个人理解: 关于Date类,进行截取或者转换时一定要注意好数据类型,long类型后面要加上L.时间的原点是1970年.用DateFormat则完成日期与文本之间的转换,特别注意的是:月是用M,时是用H ...
- java基础系列--Calendar类
原创作品,可以转载,但是请标注出处地址:http://www.cnblogs.com/V1haoge/p/7136575.html 1.Calendar概述 Java官方推荐使用Calendar来替换 ...
- Java基础系列--07_Object类的学习及源码分析
Object: 超类 (1)Object是类层次结构的顶层类,是所有类的根类,超类. 所有的类都直接或者间接的继承自Object类. 所有对象(包括数组)都实现这个类的方法 (2)Object ...
- 夯实Java基础系列9:深入理解Class类和Object类
目录 Java中Class类及用法 Class类原理 如何获得一个Class类对象 使用Class类的对象来生成目标类的实例 Object类 类构造器public Object(); register ...
- 夯实Java基础系列14:深入理解Java枚举类
目录 初探枚举类 枚举类-语法 枚举类的具体使用 使用枚举类的注意事项 枚举类的实现原理 枚举类实战 实战一无参 实战二有一参 实战三有两参 枚举类总结 枚举 API 总结 参考文章 微信公众号 Ja ...
- Java基础系列2:深入理解String类
Java基础系列2:深入理解String类 String是Java中最为常用的数据类型之一,也是面试中比较常被问到的基础知识点,本篇就聊聊Java中的String.主要包括如下的五个内容: Strin ...
- 夯实Java基础系列11:深入理解Java中的回调机制
目录 模块间的调用 多线程中的"回调" Java回调机制实战 实例一 : 同步调用 实例二:由浅入深 实例三:Tom做题 参考文章 微信公众号 Java技术江湖 个人公众号:黄小斜 ...
随机推荐
- 详谈JAVA中的file类与IO流
File类 位置于java.io包构造方法:File(String parent, String child)new file("d:\\","a.txt") ...
- Python之向日志输出中添加上下文信息
除了传递给日志记录函数的参数(如msg)外,有时候我们还想在日志输出中包含一些额外的上下文信息.比如,在一个网络应用中,可能希望在日志中记录客户端的特定信息,如:远程客户端的IP地址和用户名.这里我们 ...
- JMETER 不同线程组 变量值 的参数传递
线程组 1 在线程组1中使用__setProperty函数设置jmeter属性值(此值为全局变量值),将所需变量值如${token}设置为jmeter属性值,即newtoken,示例: 1.添加- ...
- WebSocket和kafka实现数据实时推送到前端
一. 需求背景 最近新接触一个需求,需要将kafka中的数据实时推送到前端展示.最开始想到的是前端轮询接口数据,但是无法保证轮询的频率和消费的频率完全一致,或造成数据缺失等问题.最终确定用利 ...
- java利用WatchService实时监控某个目录下的文件变化并按行解析(注:附源代码)
首先说下需求:通过ftp上传约定格式的文件到服务器指定目录下,应用程序能实时监控该目录下文件变化,如果上传的文件格式符合要求,将将按照每一行读取解析再写入到数据库,解析完之后再将文件改名. 一. 一开 ...
- 是时候开始用C#快速开发移动应用了
从2015年接触Xamarin到至今已经2个年头,我对Xamarin的技能没有长进多少,但它却已经足够成熟到在跨平台移动开发工具中占有一席之地.在扫了一些资料之后,突然发现国外有很多移动端的应用已经是 ...
- 基于FPGA的彩色图像转灰度算法实现
昨天才更新了两篇博客,今天又要更新了,并不是我垃圾产,只不过这些在上个月就已经写好了,只是因为比赛忙,一直腾不出时间整理出来发表而已,但是做完一件事情总感觉不写一博文总结一下就少点什么,所以之后的一段 ...
- 华为A199:近期不会再买华为的手机了
为了支持国货,也省点钱,买了个华为A199: 缺点: 没有google play market很复杂的刷机后才能装Gmail 不过也有亮点: 自带录音功能,老htc通过软件也只能录单方向的哦关机闹 ...
- iHover – 30+ 纯 CSS3 实现的超炫的图片悬停特效
iHover 是一个令人印象深刻的图片悬停效果集合,完全基于 CSS3 实现,无依赖,能够搭配 Bootstrap 3 很好地工作.基于 SCSS 技术构建(包括文件),便于修改变量.有模块化的代码, ...
- 日常踩坑 searter
目录 es7中的async, await Django生成二维码并转为base64 Django配置404页面 很傻逼的坑 no module named pil 其他 es7中的async, awa ...