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 ...
随机推荐
- .NET中数据访问方式(一):LINQ
语言集成查询(Language-Integrated Query),简称LINQ,.NET中的LINQ体系如下图所示: 在编程语言层次,LINQ对于不同的数据源提供了相同的查询语法,方便了程序员操 ...
- jquery处理checkbox(复选框)是否被选中
现在如果一个复选框被选中,是用checked=true,checked="checked"也行 要用prop代替attr会更好,虽然在jQuery1.6之前版本的attr()方法能 ...
- Angular2入门-数据绑定
▓▓▓▓▓▓ 大致介绍 Angular2中数据绑定的方式默认是以单向方式,数据绑定的方式可以分为: 1.属性绑定和插值表达式 组件类-> 模板 2.事件绑定:模板 -> 组件类 3.双向绑 ...
- c++:自己动手实现线程安全的c++单例类
前段时间使用c++做项目开发,需要根据根据配置文件路径加载全局配置文件,并对外提供唯一访问点.面对这样一个需求,自然的就想到了使用单例模式来创建一个单例配置对象,供外部调用.一开始想使用boost中自 ...
- 测试页面,页面里边一次加载50张不同的图片,每张5M以上,查看浏览器的内存使用情况
测试页面 1.需要你写个测试页面,页面里边一次加载50张不同的图片,每张5M,查看浏览器的内存使用情况 2.可以10张 递增的方式测试 3.图片需要缩放,比如所有图片缩放成600*800的比例 目的 ...
- KVO 模式详解
KVO:观察者模式.当指定的被观察对象属性被修改时,允许对象接收到通知的机制. p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; ...
- Instrument详解
Instruments用户指南介绍Instruments是应用程序用来动态跟踪和分析Mac OS X和iOS代码的实用工具.这是一个灵活而强大的工具,它让你可以跟踪一个或多个进程,并检查收集的数据.这 ...
- JS中的几种函数
函数可以说是js中最具特色的地方,在这里我将分享一下有关函数的相关知识: 包装函数: (function foo(){...})作为函数表达式意味着foo只能在...所代表的位置中被访问 ...
- java写文件读写操作(IO流,字符流)
package copyfile; import java.io.*; public class copy { public static void main(String[] args) throw ...
- fuse on TDH4.8
一.安装依赖包 yum install autoconf.noarch yum install automake yum install libtool* yum install m4 yum ins ...