Java Date Classes
References:
[1] http://tutorials.jenkov.com/java-date-time/index.html
[2] https://docs.oracle.com/javase/tutorial/datetime/iso/period.html
1. Java 8 and Java 7 Date classes
The main change in the Java 8 date time API is that date and time is now no longer represented by a single number of milliseconds since Jan. 1st 1970, but by the number of seconds and nanoseconds since Jan. 1st 1970. The number of seconds can be both positive and negative and is represented by a long. The number of nanoseconds is always positive and is represented by an int. You will see this new date and time representation in many of the classes in the new Java date time API.
The java.time package also contains a set of subpackages which contain more utilities etc. For instance the java.time.chrono contains classes to work with Japanese, Thai, Taiwanese and Islamic calendars. The java.time.format package contains classes used to parse and format dates from and to strings.
The core of the Java 8 date time API consists of the following classes:
Instant |
Represents an instant in time on the time line. In the Java 7 date time API an instant was typically represented by a number of millseconds since Jan. 1st. 1970. In Java 8 the Instant class represents an instant in time represented by a number of seconds and a number of nanoseconds since Jan. 1st 1970. |
Duration |
Represents a duration of time, for instance the time between two instants. Like the Instant class a Duration represents its time as a number of seconds and nanoseconds. |
LocalDate |
Represents a date without time zone information - e.g. a birthday, official holiday etc. |
LocalDateTime |
Represents a date and time without time zone information |
LocalTime |
Represents a local time of day without time zone information. |
TemporalAdjuster |
|
ZonedDateTime |
Represents a date and time including time zone information |
Period |
|
DateTimeFormatter |
Formats date time objects as Strings. For instance a ZonedDateTime or aLocalDateTime. |
Java 7 has the following date and time classes and methods. Each of these classes are also explained in their own pages, later. See the links at the bottom of this page, or at the top right of every page.
System.currentTimeMillis() |
A static method that returns the current date and time as milliseconds since January 1st 1970 |
java.util.Date |
A class that represents a date and time. Most methods in this class is deprecated. |
java.sql.Date |
A class that represents a date. All time information cut off. This date class is used with JDBC. |
java.sql.Timestamp |
A class that represents a date and time. This date and time class is used with JDBC. |
java.util.Calendar |
A base class for calendar classes. Has methods to do date and time arithmethics like adding a day or month to another date. |
java.util.GregorianCalendar |
A concrete class subclassing java.util.Calendar, representing the Gregorian calendar which is used in most of the western world today. Has all the methods from java.util.Calendar to do date and time arithmethics. |
java.util.TimeZone |
The Java TimeZone class is a class that represents time zones, and is helpful when doing calendar arithmetics across time zones. |
java.text.SimpleDateFormat |
A class that can help you parse String's into Date's, and format Date's as String's. |
If you need to do simple timing the System.currentTimeMillis() method will do just fine.
If you just need an object to hold a date, for instance as a property in a simple domain model object, you can use the java.util.Date class.
If you need to read and write the date and time to a database, use the java.sql.Date andjava.sql.Timestamp classes.
If you need to do date calculations like adding days or months to another date, or check what weekday (monday, tuesday etc.) a given date is, or convert dates and times between time zones, use thejava.util.Calendar and java.util.GregorianCalendar classes.
Java Date Classes的更多相关文章
- Top 15 Java Utility Classes
In Java, a utility class is a class that defines a set of methods that perform common functions. Thi ...
- 所活天数!java Date应用
package cn.jiu.com; import java.text.ParseException; import java.text.SimpleDateFormat; import java. ...
- jackson/fastjson、mybatis、mysql date/datatime/timestamp、java Date/Timestamp关系详解
jackson/fastjson序列化/反序列化: 默认情况下,jackson/fastjson将java Date/Timestamp类型序列化为时间戳,也就是1970年1月1日0点以来的毫秒数.如 ...
- Java Date and Calendar examples
Java Date and Calendar examples This tutorial shows you how to work with java.util.Date and java.uti ...
- Java Nested Classes(内部类~第一篇英文技术文档翻译)
鄙人最近尝试着翻译了自己的第一篇英文技术文档.Java Nested Classes Reference From Oracle Documentation 目录 嵌套类-Nested Classes ...
- 一篇文章概括 Java Date Time 的使用
本文目的:掌握 Java 中日期和时间常用 API 的使用. 参考:Jakob Jenkov的英文教程Java Date Time Tutorial 和 JavaDoc 概览 Java 8 新增 AP ...
- Java Inner Classes
When thinking about inner classes in java, the first thing that comes to my mind is that, WHY do we ...
- Effective Java Chapter4 Classes and Interface
MInimize the accessibility of classes and members 这个叫做所谓的 information hiding ,这么做在于让程序耦合度更低,增加程序的健壮性 ...
- Java Date Calendar DateFormat Details
From https://www.ntu.edu.sg/home/ehchua/programming/java/DateTimeCalendar.html Date and Time - Creat ...
随机推荐
- PPT自动载入图片并矩阵分布
最近有学生问到,能不能快速的向PPT一个页面里插入成百张图片,并让它们按统一大小的矩形排布到页面上.我写了以下代码可以在第1页中按照指定横向和纵向矩形数目,填充指定路径下的图片. Sub LoadPi ...
- 老李分享:大数据测试中java和hadoop关系
Hadoop的创始人是Doug Cutting, 同时也是著名的基于Java的检索引擎库Apache Lucene的创始人.Hadoop本来是用于著名的开源搜索引擎Apache Nutch,而Nutc ...
- 用 config drive 配置网络 - 每天5分钟玩转 OpenStack(173)
上一节最后问了大家一个问题:如果 subnet 没有开 DHCP,会是怎样一个情况? 在其他条件不变的情况下,cloud-init 依然会完成那 3 个步骤,也就是说网卡还是会被配置成 dhcp 模式 ...
- C语言求最小公倍数和最大公约数三种算法(经典)
把以前写的一些经验总结汇个总,方便给未来的学弟学妹们做个参考! --------------------------永远爱你们的:Sakura 最小公倍数:数论中的一种概念,两个整数公有的倍数成为他们 ...
- JSON的数据类型
数据类型简介 在计算机中,我们需要知道正在处理什么类型的数据,因为不同类型的数据有着不同的操作途径.可以让两个阿拉伯数字相乘,但是不能让两个单词相乘. 在计算机科学中,有一种数据类型被称为原始数据类型 ...
- node压力测试
压力测试 ab测试(ApacheBench); 介绍: 这是apache提供的压测工具; 使用: 启动node服务; 我用的XAMPP,进入bin文件夹,打开命令行,执行下面命令: // -n: 总请 ...
- JavaScript学习总结(一)DOM文档对象模型
一.文档(D) 一个网页运行在浏览器中,他就是一个文档对象. 二.对象(O) "对象"是一种自足的数据集合.与某个特定对象相关联的变量被称为这个对象的属性,只能通过某个对象调用的函 ...
- C++中的类继承(4)继承种类之单继承&多继承&菱形继承
单继承是一般的单一继承,一个子类只 有一个直接父类时称这个继承关系为单继承.这种关系比较简单是一对一的关系: 多继承是指 一个子类有两个或以上直接父类时称这个继承关系为多继承.这种继承方式使一个子类可 ...
- 时间同步方法及几个可用的NTP服务器地址
大家都知道计算机电脑的时间是由一块电池供电保持的,而且准确度比较差经常出现走时不准的时候.通过互联网络上发布的一些公用网络时间服务器NTP server,就可以实现自动.定期的同步本机标准时间. 依靠 ...
- Centos7:利用crontab定时执行任务
cron服务是Linux的内置服务,但它不会开机自动启动.可以用以下命令启动和停止服务: /sbin/service crond start /sbin/service crond stop /sbi ...