Java Date and time

Source: Internet
Author: User
Tags dateformat locale string format

One: Java.util.Data bag

The data object represents an instant that is accurate to milliseconds.

I'll just cover a few of the remaining methods (the common denominator of these methods is the Date conversion to millisecond values):

Construction Method:

    • Date(): At the bottom call System.currentTimeMillis() as a date parameter
    • Date(long date): A Date object is generated based on the specified long integer (the number of milliseconds since 1970-1-1 00:00:00).

Method

    • boolean after(Date when): Tests whether this date is after the specified date;
    • boolean before(Date when): Tests whether this date is before the specified date;
    • long getTime(): Gets the millisecond value from 1979-01-01 00:00:00 to date object;
    • void setTime(long time): Sets the time, meaning the same.

II: Java.util.calendar Bag

Because data has many drawbacks, a calendar abstract class is provided to handle the date and time. is a template for all calendar classes, so we can inherit the calendar to implement other calendars (such as the lunar calendar).

Java provides a default implementation of the calendar Gregorian java.util.GregorianCalendar calendars (in fact, the JDK also provides a Japanese calendar by default java.util.JapaneseImperialCalendar ), which is what we call the Gregorian calendar. Using

Calendar.getInstance();Gets the default GregorianCalendar , and getInstance() the inside of the method is called cal = new GregorianCalendar(zone, aLocale); to generate a Gregorian calendar instance.

The following are common methods:

  • void Add (int field, int amount) Adds or subtracts a given calendar field by the specified amount of time, according to the rules of the calendar. field is a class variable for the calendar class, such as:Calendar.DATECalendar.MONTHCalendar.HOURCalendar.DAY_OF_WEEK。但需要指出的是Calendar.MONTH月份的起始值不是1, 而是0(一月:0, 二月:1 …), Calendar.DAY_OF_WEEK代表的星期, 起始值是周日(周日:1, 周一:2 …) ;让amount为正数, 如果要减少某字段的值, 让amount为负数. 且当超出他的允许范围时, 会发生进位.
  • int get (int field) returns the value of the given Calendar field.
  • int getactualmaximum (int field) returns the time value for a calendar calendar, returning the maximum value that a specified calendar field can have.
  • int getactualminimum (int field) A time value for a given calendar calendar that returns the minimum value that a specified calendar field can have
  • void Roll (int field, int amount) adds the specified (signature) quantity to the specified calendar field without changing the larger field.
  • void set (int field, int value) sets the given Calendar field to the given value.
  • void Set (int year, int month, int date) sets the month value for the calendar word Chanyan, month, and month.
  • void Set (int year, int month, int date, int hourofday, int minute, int second) sets the value of the word Chanyan, month, day, month, hour, minute, and second.
  • void Settimeinmillis (Long Millis) sets the current time of this calendar from a given long value.
  • Long Gettimeinmillis () returns the time value of this calendar in milliseconds.

Detailed Documentation: https://docs.oracle.com/javase/8/docs/api/java/util/Calendar.html

Three: Java.text.DateFormat

The Java.text.DateFormat package is used for date formatting and is an abstract class

Method:

    • static DateFormat getdateinstance () Gets the default format of the default formatting style for the date formatter.
    • static DateFormat getdatetimeinstance () Gets the date/time formatter with the default formatting style for the locale.
    • static DateFormat gettimeinstance () Gets the time formatter for the default formatting style with the default locale.

But since we seldom use classes directly in our actual development DateFormat , we are more commonly used for their subclassesSimpleDateFormat。

Four: Java.text.SimpleDateFormat.

java.text.SimpleDateFormatYou can format date in a very flexible way, or you can use it to parse dates strings in various formats. SimpleDateFormat When you create an object , you need to pass in a pattern string, which is not a regular expression, but a date template string.

Method:

    • string format (date date) formats the date as a date/time string.
    • Date Parse (string source) parses the text from the beginning of the given string to generate the date.

Detailed Documentation: Https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html#number

Java Date and time

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.