Java Time Class

Source: Internet
Author: User

First, Date class

Date: Represents a specific moment, accurate to milliseconds
Construction Method:
(1) Public date (): Creates a Date object based on the current default millisecond value
(2) Public date (long date): Creates a Date object based on the given millisecond value
Member Methods:
(1) public long getTime (): Gets the time that returns the number of milliseconds that this Date object represents since January 1, 1970 00:00:00 GMT.
(2) Public long settime (long): Sets the time that represents the point in time milliseconds after January 1, 1970 00:00:00 GMT.
Note: General acquisition time, we are more commonly used: System.currenttimemillis () method.

what the date class needs to know :
1. Get a millisecond value from date
GetTime ()
2. Convert a millisecond value to date
A: Construction Method implementation: Public date (long date)
B: Member method implementation: Public long settime (long time)

Second, DateFormat class

in program development
String ———————— > Date
Date Control Transformation Database
Date ———————— > String
DateFormat class: Date/Time format subclass abstract class, we use specific subclasses: SimpleDateFormat
member Methods:
1. Format:date--> String
Public final String format (date date)
2. Parsing:string--> Date
Public Date Parse (String source)
the specific implementation class of the DateFormat class: SimpleDateFormat
the construction method of SimpleDateFormat class
(1) SimpleDateFormat (): Default date/Time mode
(2) SimpleDateFormat (String pattern): Given date/time pattern
Date/Time mode:
Year Y
Month M
Day D
Hours H
Min m
Seconds s

Date--String: Formatting
Create Date Object
Date d = new Date ();
Creating a formatted Object
First type: default date/Time mode
SimpleDateFormat SDF = new SimpleDateFormat ();
Public final String format (date date)
String s = sdf.format (d);
System.out.println (s);
Second type: given date/time pattern
SimpleDateFormat SDF = new SimpleDateFormat ("yyyy mm month DD Day HH:mm:ss");
Public final String format (date date)
String s = sdf.format (d);
System.out.println (s);
String--Date: Parse
String str = "2016-09-30 19:29:30";
Note: The string format must match the given string format when parsing the string to a date
SimpleDateFormat sdf2 = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");
Date dd = sdf2.parse (str);
SYSTEM.OUT.PRINTLN (DD);

Case : custom encapsulation, date and string conversion of tool classes to each other
Import java.text.ParseException;
Import Java.text.SimpleDateFormat;
Import Java.util.Date;

/**
* Tool class for converting dates and strings to each other
* @author
*/
public class Dateutil {
Private Dateutil () {
}

           /**
            * Method: Turn a Date object into a string
&N Bsp           * @param d formatted Date object
            * @param format passed over Format to be converted
            * @return formatted string
            */
            public static string datetostring (Date D, string format) {
    &NB Sp            //SimpleDateFormat SDF = new SimpleDateFormat (format);
                 //return Sdf.format (d);
                  return new SimpleDateFormat (format). Format (d);
           }

/**
* Method Function: Parse a string into a Date object
* @param s parsed string
* @param format to be converted
* @return The parsed Date object
* @throws ParseException
*/
public static Date Stringtodate (string s, string format) throws ParseException {
return new SimpleDateFormat (format). Parse (s);
}
}

Third, Calender class

Calendar class : An abstract class that provides methods for converting a specific moment to a group of calendar fields such as year, MONTH, Day_of_month, HOUR, and provides methods for manipulating calendar fields, such as getting the date of the next week.
Calendar field:
(1) public static final int year: Indicates the year's Get and set field numbers, indicating years.
(2) public static final int month: A field number that indicates the get and set of the month, indicating the month of the year.
(3) public static final int date:get and set field numbers indicating the day of the one month.
(4) public static final int hour_of_day:get and set field numbers indicating the hour of the day.
(5) public static final int Minute:get and set number of fields indicating the minute of the hour.
(6) public static final int second:get and set field numbers, indicating the seconds in a minute.
(7) public static final int Day_of_year:get and set number of fields indicating the number of days in the current year.
(8) public static final int day_of_month:get and set field numbers indicating the day of the one month.
(9) public static final int day_of_week:get and set field numbers indicating the day of the one week. The values for this field are SUNDAY, MONDAY, Tuesday, Wednesday, Thursday, FRIDAY, and SATURDAY.

Common methods:

(1) public static calendar getinstance (): Get a calendar using the default time zone and locale
such as: Calendar RightNow = Calendar.getinstance ();
(2) public int get (int field): Returns the value of the given Calendar field. Each calendar field in the Calendar class is a static member variable and is of type int.
Get year
int year = Rightnow.get (calendar.year);
Get month
int month = Rightnow.get (calendar.month);
Get Day
int date = Rightnow.get (calendar.date);
SYSTEM.OUT.PRINTLN (Year + "years" + (month + 1) + "Month" + Date + "Day");
(3) public void Add (int field,int amount): Operates on the current calendar based on the given calendar field and the corresponding time.
5 years later, 10 days ago
C.add (Calendar.year, 5);
C.add (Calendar.date,-10);
(4) Public final void set (int year,int month,int date): Sets the month and day of the current calendar
C.set (2016, 10, 30);///NOTE: The month is counted starting from 0
Caseget how many days in February of any year
Analysis:
A: Keyboard entry for any year
B: Set Calendar object's Month day
Year: a input data
Month: 2
Day: 1
C: Push the time forward one day, the last day of February
D: Get this day output
Code implementation:
Scanner sc = new Scanner (system.in);
System.out.println ("Please enter the Year:");
int year = Sc.nextint ();
Calendar C = calendar.getinstance ();
C.set (year, 2, 1);
C.add (Calendar.date,-1);
System.out.println (C.get (calendar.date));

expansion : publicstatic Calendar getinstance () method design idea Expansion
The idea of this approach:
Abstract class Person {
public static Person Getperson () {
return new Student ();
}
}
Class Student extends Person {
}

Java Time Class

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.