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.
最新文章
- IIS7.5使用web.config设置伪静态的二种方法
- 关于spark的一些简单认识。
- 配置WebSite的IIS时遇到的问题与解决方法
- PAT 1018. 锤子剪刀布 (20)
- C# web api返回类型设置为json的两种方法
- std的find和reverse_iterator联合使用
- IIS管理器的快捷方式在哪里?
- 网络IPC:套接字之非阻塞和异步I/O
- Docker 第三篇--构建Image
- 解决html代码文本复制问题,js技术
- iOS开发基础篇-transform属性
- 【题解】Luogu P5313 僕たちはひとつの光([Ynoi2012]D2T2)
- Azure系列2.1 —— com.microsoft.azure.storage.blob
- 微信支付 php兼容问题
- Android 开发 技术大纲
- github与gitlab与git三个基佬的故事
- file: /SourceCache/IOKitUser_Sim/IOKitUser-920.1.11/hid.subproj/IOHIDEventQueue.c, line: 512
- JSON类库 Flexjson学习
- httpclient 使用代理
- ASP.NET中母版页引用外部js或css文件无效,提示对象未定义解决方法