Common JavaSE-JDK classes - Date Time API

Before 1.JDK8 date and time API

1.1 java.lang.System 类

  • Public static long currentTimeMillis System class provides () returns the current time at 0:00 on January 1, 1970 0 seconds milliseconds time difference.
    This method is adapted to calculate the time difference.
  • The main criteria to calculate World Time are:
    UTC (Coordinated Universal Time)
    GMT (Greenwich Mean Time)
    CST (Central Standard Time)

1.2 java.util.Date class

It represents a specific moment, with millisecond precision

  • Constructor:
    a Date (): use the Object constructor to create a no-argument can obtain the current local time.
    Date (long date)
  • Common method
    getTime (): Returns the number of milliseconds since January 1, 1970 00:00:00 GMT represented by this Date object.
    toString (): put this Date object is converted to the form of String: dow mon dd hh: mm : ss zzz yyyy wherein: dow week in a day (Sun, Mon, Tue, Wed , Thu, Fri, Sat), zzz is the time standard.
    Many other methods are outdated.

1.3 java.text.SimpleDateFormat类

  • Date class international API not easily, largely abandoned, the java.text.SimpleDateFormat class is not associated with a locale-specific manner to format and parsed class date.
  • It allows for formatting: date  text analysis: Date Text 
  • Format:
    SimpleDateFormat (): the default mode and locale create objects
    public SimpleDateFormat (String pattern): The constructor to create an object that can be specified with the parameter pattern format, the object call:
    public String format (a Date DATE): Methods Format target date of time
  • Analytical:
    public the parse a Date (String Source): from a given start parsing the text string to generate a date.

1.4 java.util.Calendar (calendar) class

  • Calendar is an abstract base class, for performing primary functions between each operation date fields.
  • Calendar acquired example of a method
    using Calendar.getInstance () method
    calls a subclass GregorianCalendar constructor.
  • Examples of a Calendar is an abstract representation of the system time, time to get the information you want by get (int field) method. For example YEAR, MONTH, DAY_OF_WEEK, the HOUR_OF_DAY, MINUTE, SECOND,
    public void SET (Field int, int value)
    public void the Add (Field int, int AMOUNT)
    public a Date Final the getTime ()
    public void the setTime Final (a Date DATE)
    • Note:
      When obtaining Month: January is 0, February is 1, and so, on December 11
      when acquiring the week: Sunday is 1, Tuesday is 2. . . . Saturday is 7

2.JDK8 the date and time API

  • JDK 1.0 contains a java.util.Date class, but most of the methods have been introduced after the Calendar class is deprecated in JDK 1.1. The Calendar is not much better than Date. The problems they face are:
    variability: like date and time of such classes should be immutable.
    Offset properties: Date of years from the beginning of 1900, and the month are zero-based.
    Formatting: Formatting is only useful for Date, Calendar is not.
    In addition, they are not thread-safe; can not handle leap seconds and so on.
  • Java 8 absorbed the essence of Joda-Time, in order to begin to create a new excellent API for Java.
    The new java.time includes all, local time (LocalTime), local date and time (LocalDateTime), time zone (ZonedDateTime) and duration (Duration) classes on local date (LocalDate). The historic class Date Added toInstant () method for converting Date into a new representation. These new localized date and time API simplifies the management of localized date and time.
  • The new date and time the API
    java.time - contains the value of the object based package
    java.time.chrono - providing access to different calendar systems
    java.time.format - formatting and parsing the time and date
    java.time.temporal - a frame comprising a bottom and extended features
    java.time.zone - contains the class time zone support
    Note: most developers only use the base package and format packages may also use temporal package. Thus, while there are 68 new public type, most developers will probably only use one-third of them.

2.1localdat to, localtime, my localdateti

  • LocalDate, LocalTime, LocalDateTime class is more important where several classes, examples are immutable objects, respectively, using the ISO-8601日历系统的日期, time, date, and time. They provide a simple local date or time, does not include the current time information and does not contain information related to the time zone.
    Date LocalDate on behalf of IOS format (yyyy-MM-dd) can store birthdays, anniversaries and other dates.
    LocalTime represents a time, instead of the date.
    LocalDateTime is used to indicate the date and time, which is one of the most commonly used classes.
    Note: ISO-8601日历系统是国际标准化组织制定的现代公民的日期和时间的表示法, that is the Gregorian calendar.
now() / * now(ZoneId zone) 静态方法,根据当前时间创建对象/指定时区的对象
of() 静态方法,根据指定日期/时间创建对象
getDayOfMonth()/getDayOfYear() 获得月份天数(1-31) /获得年份天数(1-366)
getDayOfWeek() 获得星期几(返回一个 DayOfWeek 枚举值)
getMonth() 获得月份, 返回一个 Month 枚举值
getMonthValue() / getYear() 获得月份(1-12) /获得年份
getHour()/getMinute()/getSecond() 获得当前对象对应的小时、分钟、秒
withDayOfMonth()/withDayOfYear()/
withMonth()/withYear() 将月份天数、年份天数、月份、年份修改为指定的值并返回新的对象
plusDays(), plusWeeks(), 
plusMonths(), plusYears(),plusHours() 向当前对象添加几天、几周、几个月、几年、几小时
minusMonths() / minusWeeks()/
minusDays()/minusYears()/minusHours() 从当前对象减去几月、几周、几天、几年、几小时

2.2 Instantaneous: Instant

  • Instant: instantaneous point a timeline. This may be used to time stamp events recorded in the application.
  • In dealing with time and date, we usually think of the year, month, day, hour, minute and second. However, this is only a model of time, it is human-oriented. The second common model is for the machine, or is continuous. In this model, a point in the timeline represented by a large number, which is conducive to computer
    processing. In UNIX, this number from the beginning of 1970, in seconds units; Similarly, in Java, also from 1970, but in milliseconds.
  • java.time packet type value provided by the machine views Instant, does not provide the processing time unit in the human sense. Instant indicates that the timeline, without any context information, e.g., time zone. Conceptually, it simply represents the number of seconds since at 0:00:00 on January 1, 1970 (UTC) began. Because java.time package is calculated based on the nanosecond, so that the accuracy can be achieved Instant nanosecond.
now() 静态方法,返回默认UTC时区的Instant类的对象
ofEpochMilli(long epochMilli) 静态方法,返回在1970-01-01 00:00:00基础上加上指定毫秒数之后的Instant类的对象
atOffset(ZoneOffset offset) 结合即时的偏移来创建一个 OffsetDateTime
toEpochMilli() 返回1970-01-01 00:00:00到当前时间的毫秒数,即为时间戳

2.3 formatting and parsing date or time

java.time.format.DateTimeFormatter class: This class provides three formatting methods:

  • Predefined standard format. Such as: ISO_LOCAL_DATE_TIME; ISO_LOCAL_DATE; ISO_LOCAL_TIME
  • Localization related formats. Such as: ofLocalizedDateTime (FormatStyle.LONG)
  • Custom format. Such as: ofPattern ( "yyyy-MM-dd hh: mm: ss")
ofPattern(String pattern) 静态方法 , 返 回 一 个 指 定 字 符 串 格 式 的
DateTimeFormatter
format(TemporalAccessor t) 格式化一个日期、时间,返回字符串
parse(CharSequence text) 将指定格式的字符序列解析为一个日期、时间

2.4 Other API

  • ZoneId: This class contains all the information of the time zone, a time zone ID, such as Europe / Paris
  • ZonedDateTime: ISO-8601日历系统时区的日期时间 one, such as 2007-12-03T10: 15: 30 + 01: 00 Europe / Paris. Wherein each of the time zones corresponding ID, area ID are "region {} / {city}" format, for example: Asia / Shanghai etc.
  • Clock: clock to provide immediate access to the current date and time of use of the area.
  • Duration: Duration, for calculating two "time" interval
  • Date interval: Period, for calculating two "Date" spacer
  • TemporalAdjuster: Time corrector. Sometimes we may need to obtain for example: to adjust the date "next working day" and so on.
  • TemporalAdjusters: This class provides an implementation of a large number of common TemporalAdjuster through the static methods (firstDayOfXxx () / lastDayOfXxx () / nextXxx ()).

2.5 with the conventional conversion process date

class To legacy Class From left class
java.time.Instant与java.util.Date Date.from(instant) date.toInstant ()
java.time.Instant与java.sql.Timestamp Timestamp.from(instant) timestamp.toInstant()
java.time.ZonedDateTime与java.util.GregorianCalendar GregorianCalendar.from(zonedDateTime) cal.toZonedDateTime()
java.time.LocalDate与java.sql.Time Date.valueOf(localDate) date.toLocalDate()
java.time.LocalTime与java.sql.Time Date.valueOf(localDate) date.toLocalDate()
java.time.LocalDateTime与java.sql.Timestamp Timestamp.valueOf(localDateTime) timestamp.toLocalDateTime()
java.time.ZoneId 与 java.util.TimeZone Timezone.getTimeZone(id) timeZone.toZoneId()
java.time.format.DateTimeFormatter与java.text.DateFormat formatter.toFormat() no
Published 337 original articles · won praise 77 · views 570 000 +

Guess you like

Origin blog.csdn.net/feicongcong/article/details/104899285