Common API of the Java Date class Introduction

Data class

java.util.Date: indicates the date and time of the class
class Date represents a specific moment, with millisecond precision.
Ms: millisecond 1000 msec = 1 sec

The date is converted into milliseconds:

  • The current date: 2019-07-18
  • Time origin (0 ms): 1970-01-01 00:00:00 (Greenwich)
  • Is to calculate the current date to the time between the origin experienced a total of how many milliseconds

Note:
China belongs to the East eight districts, the time will increase 8 hours

  • 1970-01-01 08:00:00

Converting milliseconds Date:

  • 1 day = 24 * 60 * 60 = 86,400 seconds = 86400 * 1000 = 86400000 ms

use:

Date class configuration parameters methods 
Date DATE = new new Date (); 
Date (); // Get current system time and date 
--------- 
Date DATE = new new Date (0L );  Date (Long DATE ); // pass milliseconds, the conversion of milliseconds to a date date  -----------  date dATE = new new date (); Long Time = date.getTime (); Long the getTime () converts the date to milliseconds (equivalent to System.currentTimeMillis () method) returns the number of milliseconds since January 1, 1970 00-00-00GMT this Date object represents.

 

DateFormat class

java.text.DateFormat: a date / time formatting subclasses of abstract classes

effect:

  • Format (that is, the date -> text), parsing (text -> date)

Member method:

  • String format (Date date) according to the designated mode, the date Date, formatted to conform to the pattern string
  • Date parse (String source) to conform to the schema string, resolves to Date Date
  • DateFormat class is an abstract class, you can not directly create objects to use, you can use a subclass of DateFormat class
java.text.SimpleDateFormat extends DateFormat

The DateFormat class method format, the text is formatted date

Steps for usage:

  1. Create simpleDateFormat object constructor transmission mode specified
  2. The method of calling format SimpleDateFormat object, the method specified in the configuration mode, the mode Date date formatted to conform to a string (text)
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
Date date = new Date();
String d = sdf.format(date);

 

Class Calendar

Description:
java.util.Calendar class; calendar class

  • Calendar class is an abstract class, a method of operating inside it provides many calendar field (YEAR, MONTH, DAY_OF_MONTH, HOUR )
  • Calendar class can not be used directly to create objects, inside there is a static method called getInstance (), which returns a subclass object class Calendar
  • When static Calendar getInstance () uses the default time zone and locale to get a calendar.
Calendar c = Calendar.getInstance (); // Polymorphic

Calendar class of common member method:

  • public int get (int field); Returns the value of a given calendar field.
  • public void set (int field, int value); given calendar field to the given value
  • public abstract void add (int field, int amount); According to the rules of the calendar, add or subtract an amount of time specified for the given calendar field.
  • public Data getTime (); return a time value times Calendar Date object (from epoch to millisecond offset) FIG.
// public int get (int field) ; return value of a given calendar field. 
C = Calendar Calendar.getInstance (); 
int year = c.get (Calendar.YEAR); 
int = month The c.get (the Calendar.MONTH); 

/// public void SET (Field int, int value); given the calendar field to the given value 
// set the year 9999 
// set the month September 
c.set (Calendar.YEAR, 9999 );  c.set (of the Calendar.MONTH, 9 ); 
 // increase in  c.add (Calendar.YEAR, 2 ); // the calendar object, the object into a date date date = c.getTime ();

 

 

Guess you like

Origin www.cnblogs.com/cainiao-chuanqi/p/11210341.html
Recommended