Java Learning Note (Date class, DateFormat Class)

Source: Internet
Author: User
Tags date1 dateformat time and date

Date, time, and date classes, which are described in the util package instead of in the SQL package

Date: Represents a specific time instantaneous, accurate to milliseconds (1000 milliseconds = 1 seconds)

Time and date operations are based on millisecond values

Time Origin:January 1, 1970, 0:0 0 seconds, corresponding millisecond value is 0

Gets the millisecond value of the current time:

 Public class Datedemo {    publicstaticvoid  main (string[] args) {        long L = system.currenttimemillis ();         // gets the millisecond value of the current date         System.out.println (l);         // 1515639742854     }}

How to construct the date class:

 Packagedemo;Importjava.util.Date; Public classDatedemo { Public Static voidMain (string[] args) {function1 ();    Function2 (); }     Public Static voidfunction1 () {Date Date=NewDate ();        SYSTEM.OUT.PRINTLN (date); //output: Thu Jan 11:09:41 CST 2018    }     Public Static voidfunction2 () {Date Date=NewDate (123456);        SYSTEM.OUT.PRINTLN (date); //output: Thu Jan 08:02:03 CST 1970    }}

Method of the Date class:

 Packagedemo;Importjava.util.Date; Public classDatedemo { Public Static voidMain (string[] args) {function1 ();    Function2 (); }     Public Static voidfunction1 () {//date turns to millisecondsDate Date =NewDate (); LongTime =Date.gettime ();        System.out.println (time); //1515642454307    }     Public Static voidfunction2 () {//Millisecond Turn DateDate Date =NewDate ();        SYSTEM.OUT.PRINTLN (date); //Thu Jan 11:47:34 CST 2018Date.settime (123123);        SYSTEM.OUT.PRINTLN (date); //Thu Jan 08:02:03 CST 1970    }}

We find that the date format is not what we used to be.

So here's a DateFormat class that provides formatting date functionality:

DateFormat is an abstract class that mainly uses its subclass SimpleDateFormat class

Example:

 Packagedemo;ImportJava.text.DateFormat;ImportJava.text.SimpleDateFormat;Importjava.util.Date;//Formatting the date Public classSimpledateformatdemo { Public Static voidMain (string[] args) {function (); }         Public Static voidfunction () {DateFormat date=NewSimpleDateFormat ("yyyy mm month DD Day hh point mm min ss sec"); String Date1= Date.format (NewDate ());        System.out.println (date1); //output: January 11, 2018 12 point 01分钟30秒    }}

You can also convert a string to a date:

Example:

 Packagedemo;ImportJava.text.DateFormat;Importjava.text.ParseException;ImportJava.text.SimpleDateFormat;Importjava.util.Date;//Formatting the date Public classSimpledateformatdemo { Public Static voidMain (string[] args)throwsparseexception {function (); }     Public Static voidfunction ()throwsParseException {//Here are the exceptions, which are described laterDateFormat date1 =NewSimpleDateFormat ("Yyyy-mm-dd"); //must be a standard-format stringDate date2 = Date1.parse ("2017-11-25");        System.out.println (DATE2); //output: Sat 00:00:00 CST    }}

In real-world development, users are not allowed to enter dates themselves because of the risk of formatting errors

You usually make a date control for the user to choose instead of letting the user enter

Java Learning Note (Date class, DateFormat 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.