Java study notes 22 (Date and DateFormat), learning notes dateformat

Source: Internet
Author: User

Java study notes 22 (Date and DateFormat), learning notes dateformat

Date, time, and Date.UtilPackage, not in the SQL package

Date: indicates the specified time, accurate to milliseconds (1000 milliseconds = 1 second)

The time and date operations are based on the millisecond value.

Time origin:January 1, 1970 00:00:00, corresponding to the millisecond value 0

Get the millisecond value of the current time:

Public class DateDemo {public static void main (String [] args) {long l = System. currentTimeMillis (); // obtain the millisecond value of the current date System. out. println (l); // 1515639742854 }}

 

Construction Method of the Date class:

Package demo; import java. util. date; public class DateDemo {public static void main (String [] args) {function1 (); function2 ();} public static void function1 () {Date date = new Date (); System. out. println (date); // output: Thu Jan 11 11:09:41 CST 2018} public static void function2 () {Date date = new Date (123456); System. out. println (date); // output: Thu Jan 01 08:02:03 CST 1970 }}

 

Date method:

Package demo; import java. util. date; public class DateDemo {public static void main (String [] args) {function1 (); function2 ();} public static void function1 () {// convert Date to millisecond date Date = new date (); long time = Date. getTime (); System. out. println (time); // 1515642454307} public static void function2 () {// convert the millisecond to Date date Date = new Date (); System. out. println (date); // Thu Jan 11 11:47:34 CST 2018 date. setTime (123123); System. out. println (date); // Thu Jan 01 08:02:03 CST 1970 }}

 

 

We found that the Date format obtained by the Date class is not what we are used,

Therefore, the DateFormat class provides the date Formatting Function:

DateFormat is an abstract class. It mainly uses its subclass SimpleDateFormat class.

Example:

Package demo; import java. text. dateFormat; import java. text. simpleDateFormat; import java. util. date; // format the public class SimpleDateFormatDemo {public static void main (String [] args) {function ();} public static void function () {DateFormat date = new SimpleDateFormat ("MM dd, yyyy, HH: mm min ss seconds"); String date1 = date. format (new Date (); System. out. println (date1); // output:, January 1, January 11, 2018, 30 seconds }}

 

You can also convert a string to a date:

Example:

Package demo; import java. text. dateFormat; import java. text. parseException; import java. text. simpleDateFormat; import java. util. date; // format the public class SimpleDateFormatDemo {public static void main (String [] args) throws ParseException {function ();} public static void function () throws ParseException {// here is an exception. The following section describes DateFormat date1 = new SimpleDateFormat ("yyyy-MM-dd "); // The string must be in the standard format Date date2 = date1.parse (""); System. out. println (date2); // output: Sat Nov 25 00:00:00 CST 2017 }}

In actual development, users are not allowed to enter their own date, because there is a risk of incorrect format.

Generally, a date control is used for user selection, instead of for user input.

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.