Java 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 Date Time API

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.

时间: 2024-10-13 09:55:40

Java Date Classes的相关文章

Java Date, Calendar and Time API - Tutorial

Java Date and Time API This article explains the API for using Calendar, Date and Time in Java and how to format the output of a date. Table of Contents 1. Overview 2. Format date 3. Working with Dates and Calendars 3.1. Calendar 3.2. Date and Date C

jackson/fastjson、mybatis、mysql date/datatime/timestamp、java Date/Timestamp关系详解

jackson/fastjson序列化/反序列化: 默认情况下,jackson/fastjson将java Date/Timestamp类型序列化为时间戳,也就是1970年1月1日0点以来的毫秒数.如果要显示为用户友好表示: Jackson 可以: SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); objectMapper.setDateFormat(sdf)或者:@JsonFormat(locale

Java Date类和Calendar类的一个控制台打印日期的小程序

Java Date类和Calendar类的一个打印日期的小程序,可以直接用. package com.boy.Idate.calendar; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; /** * 控制台可视化日历 * @author 田硕 */ public class VisualCalendar {

java Date和Calendar类

最近在无聊的看书,遇到一编程题目,问题描述如下: 黑色星期五源于西方迷信:耶稣基督死于星期五,而13也是一个不吉利的数字.黑色星期五即该日同时是星期五又是13号,求未来几年内这样的日子. 基于该问题会涉及到java中的关于时间类的部分,故尝试通过该题目总结现阶段的java Date和calendar类的问题. 一.Date类 查阅API文档可知,Date类源于jdk1.0版本,并在jdk1.1版本中其绝大多数方法被Calendar类中方法所代替.Date类构造函数public且未abstrct,

常用的Java Date 方法

常用的Java Date 方法 1.计算某一月份的最大天数: Calendar time=Calendar.getInstance(); time.clear(); time.set(Calendar.YEAR,year); //year为int time.set(Calendar.MONTH,i-1);//注意,Calendar对象默认一月为0 int day=time.getActualMaximum(Calendar.DAY_OF_MONTH);//本月份的天数 注:在使用set方法之前,

java Date函数总结

java.util.Date为java时间接口,已知的实现类包括Date,Time,TimeStamp Calendar calendar=Calendar.getInstance(); DateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date dateA=new Date(); //初始化dataA为当前时间 //将Date转化为String String dateAString=df.format(dateA);

Top 15 Java Utility Classes

In Java, a utility class is a class that defines a set of methods that perform common functions. This post shows the most frequently used Java utility classes and their most commonly used methods. Both the class list and their method list are ordered

java date类型和calendar类型区别

Date类 在JDK1.0中,Date类是唯一的一个代表时间的类,但是由于Date类不便于实现国际化,所以从JDK1.1版本开始,推荐使用Calendar类进行时间和日期处理.这里简单介绍一下Date类的使用. 1.使用Date类代表当前系统时间 Date d = new Date(); System.out.println(d); 使用Date类的默认构造方法创建出的对象就代表当前时间,由于Date类覆盖了toString方法,所以可以直接输出Date类型的对象,显示的结果如下: Sun Ma

Java Date and Calendar examples

This tutorial shows you how to work with java.util.Date and java.util.Calendar. 1. Java Date ExamplesFew examples to work with Date APIs. Example 1.1 – Convert Date to String. SimpleDateFormat sdf = new SimpleDateFormat("dd/M/yyyy"); String date