Java Time-related

Source: Internet
Author: User

Java time focuses on these classes, viewing Java API 1.6

Java.util.Calendar

CalendarA class is an abstract class that provides methods for converting specific moments to and from a set of transformations, such as,,,, and so on, YEAR MONTH DAY_OF_MONTH HOUR 日历字段 and provides methods for manipulating calendar fields, such as getting the date of the next week. The instantaneous value is expressed as a millisecond, which is the offset from the calendar element (that is, Greenwich Mean Time January 1, 1970, 00:00:00.000, Gregorian calendar).

The class also provides additional fields and methods for implementing a specific calendar system outside the scope of the package. These fields and methods are defined as protected .

Like other locale-sensitive classes, Calendar A class method is provided getInstance to obtain a generic object of this type. Calendar getInstance method returns an Calendar object whose Calendar field has been initialized by the current date and time:

     Calendar RightNow = Calendar.getinstance ();

Calendarobjects can generate all the Calendar field values required to implement date-time formatting for a specific language and calendar style, such as Japanese-Grigory, Japanese-traditional calendars. Calendardefines the range of return values for some calendar fields, and the meanings of those values. For example, for all calendars, the first month of the calendar system has a value of MONTH == JANUARY . Other values are defined by a specific subclass (for example ERA ). For details about this, see the documentation for each field and the subclass document.

Java.util.Date

The class Date represents a specific moment, accurate to milliseconds.

In the class Date all methods that can accept or return the year, month, date, hour, minute, and second values, the following representation is used:

    • The year y is represented by an integer y - 1900 .
    • The month is represented by an integer from 0 to 11, 0 is a month, 1 is February, and so 11 is December.
    • The date (day of January) is represented by integers 1 through 31 in the usual manner.
    • The hour is represented by an integer from 0 to 23. Therefore, from midnight to 1 a.m., the time is 0, from noon to 1 p.m. the time is 12 points.
    • Minutes are represented by integers from 0 to 59 in the usual manner.
    • Seconds are represented by integers from 0 to 61, and values 60 and 61 occur only for leap seconds, although that is only true for Java implementations that actually track leap seconds correctly. It is extremely unlikely that two leap seconds will occur in the same minute as the current introduction of leap seconds, but this specification follows the date and time conventions of ISO C

Java.util.TimeStamp

TimeStamp is a subclass of date

A java.util.Date class-related thin wrapper (thin wrapper) that allows the JDBC API to identify the class as a SQL TIMESTAMP value. It adds TIMESTAMP the ability to save the SQL fractional seconds value by allowing fractional seconds to the specification of nanosecond precision. Timestamp also provides the ability to format and parse operations of the JDBC escape syntax that supports timestamp values.

The accuracy of the computed Timestamp object is either:

    • 19 , which is the number of characters in the YYYY-MM-DD hh:mm:ss format
    • 20 + s , which is the number of characters in Yyyy-mm-dd hh:mm:ss.[fff ...] format, which s represents the scale of a given Timestamp (its fractional seconds precision).

Note: This type consists java.util.Date of a separate nanosecond value. Only integer seconds are stored in the java.util.Date component. Fractional seconds (nanoseconds) exist independently. java.sql.Timestampwhen you pass an object that is not an instance, the Timestamp.equals(Object) method never returns true , because the nanosecond component of the date is unknown. Therefore, the java.util.Date.equals(Object) method is asymmetric relative to the method Timestamp.equals(Object) . In addition, the hashcode method uses the underlying java.util.Date implementation and therefore does not include nanoseconds in its calculations.

Because Timestamp of the difference between a class and the above java.util.Date class, it is recommended that the code generally not treat Timestamp the value as java.util.Date an instance. Timestampthe java.util.Date inheritance relationship between and is actually the implementation of inheritance, not type inheritance.

Java.text.SimpleDateFormat

SimpleDateFormat format = new SimpleDateFormat (' Yyyy-mm-dd HH:mm:ss ");

String datestring = Format.format (New Date ());

Date date = Format.parse ("2017-07-05 22:56:55");

Mutual transformation

date<-------------------------->calendar

Date date = Calendar.gettime ();

Calendar Calendar =calendar.getinstance ();

Calendar.settime (New Date ());

Part of the date taken 

int year =calendar.get (calendar.year);   int Month=calendar.get (calendar.month) +1;   int day =calendar.get (calendar.day_of_month);   int hour =calendar.get (calendar.hour_of_day);   int minute =calendar.get (calendar.minute);   int second =calendar.get (calendar.second);
Add 1 when you get the month

Subtract 5 days from the current time

Calendar.add (calendar.day_of_month,-5);

And Apach's Common-lang packet processing.

Http://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/time/DateUtils.html

Java Time-related

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.