Java API Date Time Series 1 ----- Jdk7 previous date and time classes

  

The main categories are:

Date class is responsible for representing the time in the computer, indicates the time is a larger concept, existing systems are basically timed 1970.1.1 use the number of milliseconds from 00:00:00 to the current time, this time called for the epoch. If not explicitly stated in the text, it refers to the number of milliseconds from a few milliseconds to the corresponding period of 1970. Date of internal class in Java is actually a number of milliseconds, external performance as a Date object.

Calendar is a utility class that is responsible for the Date class to modify and other operations, and extract specific information such as time, date from the Date class.

DateFormat is responsible for the conversion date, such as reading a string of a specific format, converted into a date object, or a date object into a string in the specified format.

1 Date represents time. Internal mainly when a number of milliseconds from the value stored epoch long. The vast majority methods are outdated.

This refers to: java.util.Date date format: year, month, day, hour

    public class Date{
        private transient long fastTime;
        Date(){
            this(System.currentTimeMillis());
        }
        Date(long date){
            fastTime = date;
        }
        //……
    }

2 TimeZone time zone (24, such as the Asia / shanghai)

// Get the default time zone 
TimeZone.getDefault ();

3 Locale countries (or regions) and languages ​​(such as zh_CN)

// Get the default country and language 
Locale.getDefault ();

. 4 Calendar date and time are the main classes of operation, it is an abstract class that provides a plurality of static methods for obtaining a Calendar instance.

Date with similar internal Calendar also expressed a number of milliseconds of time, but also defines an array (length 17), represents the value of each field in the calendar.

proteted long time;
proteted int fields[];

fields following values ​​are stored in these fields, to Calendar

    Calendar.YEAR,
    Calendar.MONTH,
    Calendar.DAY_OF_MONTH,
    Calendar.DAY_OF_WEEK,
    Calendar.HOUR_OF_DAY,
    Calendar.MINUTE,
    Calendar.SECOND,
    Calendar.MILLISECOND

These values ​​may be obtained by instances of Calendar (Calendar will be converted according to time zone, language regions).

    // empty constructor, it will obtain the current. Calendar.getInstance ( "Asia / Shanghai", "zh_CN") 
    Calendar Calendar = Calendar.getInstance ();
     int Day = Calendar.get (Calendar.DAY_OF_MONTH);

Calendar also supports reducing the time to increase based on the field (a negative number indicates a decrease).

    Calendar.getInstance().add(Calendar.MONTH,-2);

In conclusion, Calendar did a very tedious work, automatically switch between milliseconds and then absolute time according to the calendar field TimeZone and Locale.

5 DateFormat (thread-safe) Date and provides conversion between a string representation, two main methods format (Date d), parse (String s)

Date string representation of TimeZone, Locale are relevant.
At the same time formatting styles associated with two, one is the date formatting style, is a time formatting style.
DataFormat is an abstract method, but also create objects using factory methods.

    DateFormat.getTimeInstance();
    DateFormat.getDateInstance();
    DateFormat.getDateTimeInstance();

GetTimeInstance wherein only the processing time, only getDateInstance, processing date, a processing result getDateTimeInstance dates and times, the following three kinds of objects

    Calendar calendar = Calendar.getInstance();
    
    //结果是21:34:20
    DateFormat.getTimeInstance().format(calendar.getTime());
    //结果是2019-02-20
    DateFormat.getDateInstance().format(calendar.getTime());
    //结果是2019-02-20 21:34:20
    DateFormat.getDateTimeInstance().format(calendar.getTime());

DateFormat Although convenient, but more precise control date string format, you should use SimpleDateFormat.

6 SimpleDateFormat (thread safe) is a subclass of DateFormat, with the main difference is the parent class: a subclass can customize the date format.

    String pattern = "when E HH yyyy dd day, MM month mm ss a" ; 
    the SimpleDateFormat SimpleDateFormat = new new the SimpleDateFormat (pattern);
     // output is at 21:00 on February 20th 2019 Wed 44 minutes and 42 seconds 
    OS.print ( simpleDateFormat.format (calendar.getTime ()));

pattern of English characters az, AZ represents a special meaning, the other character is output.

pattern variable parameters Corresponding practical significance Examples
yyyy Year 4 of the 2019
MM Two-digit month 02
dd date 20
HH / dd 24-hour / 12-hour 21/09
mm minute 55
ss second 55
E Day of the week wed
a Morning and afternoon, with the general use hh PM

SimpleDateFormat can also easily be converted into a string Date.

    String str = "19 on February 20, 09 hours 58 minutes 33 seconds 111" ; 
    a Date DATE = new new the SimpleDateFormat ( "dd day when HH yy mm on M ss a month the SSS" ) .parse (STR);
     // Results February 2019 09 hours 58 minutes 33 seconds 20 
    String Result = new new the SimpleDateFormat ( "date YYYY, mM-dd HH mm ss a time") .format (date);

7.java.sql.Date inheritedjava.util.Date,只保留了日期

java.sql.DateClass JDBC APIis used, the date format: year, month, day. If you need to java.sql.PreparedStatementset the date on or from the java.sql.ResultSetacquisition date, and you will be java.sql.Datedealing with.
You can use java.util.Dateanything to do also apply tojava.sql.Date。

long time = System.currentTimeMillis();
java.sql.Date date = new java.sql.Date(time);

java.sql.DateAnd the java.util.Datebiggest difference is that java.sql.Datethe date represented only retain the date, and no time.

For example, if you create a use 2009-12-24 23:20 java.sql.Date, then one time (23:20) will be cut off. If you need to keep time, use  java.sql.Timestamp instead java.sql.Date.

8.java.sql. Time inheritedjava.util.Date,只保留了时间

同java.sql.DateClass JDBC APIis used, the Date format: minutes and seconds.

9.java.sql.Timestamp inherited java.util.Date,to java.util.Date class was expanded class provides getNanos () method

同java.sql.DateClass JDBC APIis used, the date format: date when minutes and seconds nanoseconds.

 

Reference: https: //www.jianshu.com/p/1478af429a1e

     https://blog.csdn.net/zhao123h/article/details/53012791

     http://ifeve.com/java-sql-date/

Guess you like

Origin www.cnblogs.com/xkzhangsanx/p/12032719.html