Getting started with date Processing in java

Source: Internet
Author: User
Java getting started Date Processing in Java-general Linux technology-Linux programming and kernel information. The following is a detailed description. 1. Date Template

The template is defined as follows:

Year y, such as 1996

M month, such as July or 07

D The day of the month, such as 12

H hour (in 24 format), such as 0 and 17

M minutes, such as 32

S banknote clock, such as 55

S notes, such as 978

E day of the week, such as Tuesday

D The day of the year, such as 189

W week in year (Number) 27

W week in month (Number) 2

A am/pm marker (Text) PM

K hour in day (1 ~ 24) (Number) 24

K hour in am/pm (0 ~ 11) (Number) 0

Z time zone (Text) Pacific Standard Time

'Escape for text (Delimiter)

''Single quote (Literal )'

2. Date Format

Use the SimpleDateFormat class to format a date. If the date format is incorrect, an exception occurs.

Import java. text .*;

Import java. util .*;

Public class TestDate {

Public static void main (String [] args ){

String dStr = "2001.12.12-08.23.21 ";

Date d = null;

SimpleDateFormat sdf = new SimpleDateFormat ("yyyy. MM. dd-HH. mm. ss ");

Try {

Http://www.54he.com he Haiping hp54@163.com

D = sdf. parse (dStr );

} Catch (ParseException pe ){

System. out. println (pe. getMessage ());

}

System. out. println (d );

System. out. println (d. getTime ());

}

}

Find the SimpleDateFormat class in the java. text package and give it a template, which can be parse into a Date, and then use Date

Class getItme () method to obtain a Long data.

3. Get the current date
Public String GetDateTime ()

{

Calendar cal = Calendar. getInstance ();

SimpleDateFormat formatter = new SimpleDateFormat ("yyyy-MM-dd HH: mm: ss ");

String mDateTime = formatter. format (cal. getTime ());

Return (mDateTime );

}

Public java. SQL. Date GetDate ()

{

Java. SQL. Date mDate;

Calendar cal = Calendar. getInstance ();

SimpleDateFormat formatter = new SimpleDateFormat ("yyyy-MM-dd HH: mm: ss ");

String mDateTime = formatter. format (cal. getTime ());

Return (java. SQL. Date. valueOf (mDateTime ));

}
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.