Measure the test taker's knowledge about Date Processing in Java.

Source: Internet
Author: User
Tags dateformat time and date

Processing date in Java:

First, ask the following questions:

1. What is UTC, UT? GMT?

World Time, standard time, Greenwich Mean Time.

2. How to get the current system time?

Method 1: system. currenttimemills (); returns the number of milliseconds between the first system time and 00:00:00, January 1, January 1, 1970.

 

Method 2: Create a date object, date d = new date ();

Method 3: Create a calendar object, which is calendar c = calendar. getinstance ();

 

3. Time Format problems?

The following types are used to solve the time format problem:

Java. Text. simpledateformat

Java. Text. dateformat

4. How to convert a string to date or time?

Use the valueof () method to convert a string to the date format in a specific format.

This string s must adopt the following format:

String S = "1970-12-30 08:24:37. 0 ";

Timestamp Ts = timestamp. valueof (s );

 

 

5. How to process the date or time in the database?

The following describes the functions and relationships of these classes in detail.

 

First, we need to clarify the differences between Java. util. Date and Java. SQL. Date:

Java. util. date can be used anywhere except SQL statements.


Java. SQL. date is used for SQL statements. It only contains the date and no time.

 

In fact:

Java. SQL. date only has a date

Java. SQL. Time only has time

Java. SQL. timestamp (both date and time) are subclasses of Java. util. Date (Packaging class, which can be applied in SQL ).

 

 

Inheritance relationship: Java. Lang. Object -- Java. util. date -- Java. SQL. Date/Java. SQL. Time/
Java. SQL. Timestamp
The specific conversion relationship is Java. util. Dated = new java. util. Date (New java. SQL. Date ());

 

 

 

1. java. util. Date

Parent class. Is the class used to process time in Java.

 

2. java. SQL. Date

Java. SQL. date is the data type set to match SQL date. The normalized java. SQL. date only contains the year, month, and day information, and the time, minute, and second milliseconds are cleared. The format is similar to yyyy-mm-dd. When we call the getdate () method of resultset to obtain the returned value, the Java program uses Java. SQL. Date of the "standard" to format the value in the database. Therefore, the information of the nonstandard part in the database will be stolen.

You can use getdate () to obtain the date in the resultset.

 

3. java. SQL. Time

Java. SQL. Time is the data type set to work with SQL time.

You can use the date in gettime () resultset to obtain the time.

 

4. java. SQL. Timestamp

Get the date and time at the same time.

You can get the timestamp through gettimestamp. Contains the time and date.

 

Next we will talk about the classes used to format the date:

5. java. Text. simpledateformat

First, pay special attention to the class, not in Java. util, but in Java. Text.

 

Simpledateformat F = new simpledateformat ("yyyy-mm-DDHH: mm: SS ");

This time date can be simpledateformatformat ()

 

Simpledateformat usage:

 

Import java. Text. simpledateformat;

Import java. util .*;

Java. util. Date = new java. util. Date ();

// If you want to obtain the format of yyyymmdd

Simpledateformat sy1 = new simpledateformat ("yyyymmdd ");

String dateformat = sy1.format (date );

// If You Want To separately obtain the year, month, and day

Simpledateformat Sy = new simpledateformat ("YYYY ");

Simpledateformat Sm = new simpledateformat ("mm ");

Simpledateformat SD = new simpledateformat ("DD ");

 

String syear = Sy. Format (date );

String SMON = Sm. Format (date );

String SDAY = SD. Format (date );

 

 

6. java. util. Calendar

Calendar is a class added after jdk1.2 as a supplement to Java. util. Date. The calendar provides many methods to replace the methods in Java. util. Date. You can use the calendar class to convert a certain time point to year, month, day, and time.

The usage is as follows:

Calendar calendar = calendar. getinstance ();

// Obtain the current time and declare the time variable

Int year = calendar. Get (calendar. year );

// Get the year

Int month = calendar. Get (calendar. month );

// Get the month, but add 1 to the month

Month = month + 1;

Int date = calendar. Get (calendar. date );

// Obtain the date

Stringtoday = "" + year + "-" + month + "-" + date + "";

 

Related Article

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.