Date Processing exercises in Java

Source: Internet
Author: User
Tags dateformat

Package net. HFH;

Import java. SQL. Ti

Package net. HFH;

Import java. SQL. timestamp;
Import java. Text. simpledateformat;
Import java. util. calendar;
Import java. util. date;
Import java. util. gregoriancalendar;
Import java. util. timezone;

Public class dateformat {

Public static void main (string [] ARGs ){

Long L1 = system. currenttimemillis ();
Date = new date (); // many methods of date are outdated

/*
* Simpledateformat SDF = new simpledateformat ("yyyy-mm-dd hh: mm: SS ");
* String STR = SDF. Format (date );
*/
// Serves the same purpose as the preceding two statements
String STR = new simpledateformat ("yyyy-mm-dd hh: mm: SS: SSS ")
. Format (date); // view the manual
System. Out. println (STR );

// System. Out. println (system. currenttimemillis ());

// Retrieve related information such as year, month, and day in the current date
Calendar c = calendar. getinstance (); // create an instance
Int year = C. Get (calendar. year); // obtain the year
System. Out. println (year );
Int month = C. Get (calendar. month); // obtain the month
System. Out. println (month );

// Convert the date obtained from other places, such as how to operate from the database.
Calendar C1 = calendar. getinstance ();
C1.set (2005, 4, 1 );
Int year1 = c1.get (calendar. year );
System. Out. println ("year1 ----" + year1 );

/*
// Why cannot I display the time correctly?
Date d = new date (, 3); Calendar C2 = calendar. getinstance ();
C2.settime (d); int year2 = c2.get (calendar. year );
System. Out. println (New simpledateformat ("YYYY"). Format (d ));
System. Out. println (year2 );
*/

// Retrieve and format the time from the database
/*
Date = Rs. getdate ("cdate ");
Simpledateformat SDF = new simpledateformat ("HH: mm: SS"); // when this cannot be displayed, all are the same
System. Out. println (SDF. Format (date ));
*/

/*
// The related date and time can be correctly displayed.
Timestamp Ts = Rs. gettimestamp ("cdate"); // Timestamp
Simpledateformat SDF = new simpledateformat ("HH: mm: SS ");
Simpledateformat sdf1 = new simpledateformat ("yyyy-mm-dd hh: mm: SS ");
System. Out. println (SDF. Format (TS ));
System. Out. println (sdf1.format (TS ));
*/

// Method for obtaining the current time
// System. currenttimemillis ();
// Date d = new date ();
// Calendar C2 = calendar. getinstance ();

// Convert string to date format
// String sdate = "2008-8-8 ";
// Java. SQL. Date SD =
// Java. SQL. Date. valueof (sdate); // This method is commonly used, for example, convert the string retrieved from the database to the Time Format
// System. Out. println ("SD ---" + SD );
// String sdate1 = ". 0 ";
// Timestamp Ts = timestamp. valueof (sdate1 );
// System. Out. println ("Ts =" + TS );

// Time Zone Processing
// Obtain the time of Japan
Timezone tzjapan = timezone. gettimezone ("Japan"); // obtain the time zone
Calendar cjapan = new gregoriancalendar (tzjapan); // The internal parameter is ID. You can use the following method to obtain the ID:
System. Out. println (cjapan. Get (calendar. hour_of_day ));

// Obtain each valid ID
// Method 1
String [] Ss = timezone. getavailableids ();
For (INT I = 0; I <ss. length; I ++ ){
System. Out. println (ss [I]);
}
// Method 2
/*
For (string str2: timezone. getavailableids ()){
System. Out. println (str2 );
}
*/

Long L2 = system. currenttimemillis ();
System. Out. println ("execution time:" + (l2-l1) + "millisecond ");
}

}

Mestamp;
Import java. Text. simpledateformat;
Import java. util. calendar;
Import java. util. date;
Import java. util. gregoriancalendar;
Import java. util. timezone;

Public class dateformat {

Public static void main (string [] ARGs ){

Long L1 = system. currenttimemillis ();
Date = new date (); // many methods of date are outdated

/*
* Simpledateformat SDF = new simpledateformat ("yyyy-mm-dd hh: mm: SS ");
* String STR = SDF. Format (date );
*/
// Serves the same purpose as the preceding two statements
String STR = new simpledateformat ("yyyy-mm-dd hh: mm: SS: SSS ")
. Format (date); // view the manual
System. Out. println (STR );

// System. Out. println (system. currenttimemillis ());

// Retrieve related information such as year, month, and day in the current date
Calendar c = calendar. getinstance (); // create an instance
Int year = C. Get (calendar. year); // obtain the year
System. Out. println (year );
Int month = C. Get (calendar. month); // obtain the month
System. Out. println (month );

// Convert the date obtained from other places, such as how to operate from the database.
Calendar C1 = calendar. getinstance ();
C1.set (2005, 4, 1 );
Int year1 = c1.get (calendar. year );
System. Out. println ("year1 ----" + year1 );

/*
// Why cannot I display the time correctly?
Date d = new date (, 3); Calendar C2 = calendar. getinstance ();
C2.settime (d); int year2 = c2.get (calendar. year );
System. Out. println (New simpledateformat ("YYYY"). Format (d ));
System. Out. println (year2 );
*/

// Retrieve and format the time from the database
/*
Date = Rs. getdate ("cdate ");
Simpledateformat SDF = new simpledateformat ("HH: mm: SS"); // when this cannot be displayed, all are the same
System. Out. println (SDF. Format (date ));
*/

/*
// The related date and time can be correctly displayed.
Timestamp Ts = Rs. gettimestamp ("cdate"); // Timestamp
Simpledateformat SDF = new simpledateformat ("HH: mm: SS ");
Simpledateformat sdf1 = new simpledateformat ("yyyy-mm-dd hh: mm: SS ");
System. Out. println (SDF. Format (TS ));
System. Out. println (sdf1.format (TS ));
*/

// Method for obtaining the current time
// System. currenttimemillis ();
// Date d = new date ();
// Calendar C2 = calendar. getinstance ();

// Convert string to date format
// String sdate = "2008-8-8 ";
// Java. SQL. Date SD =
// Java. SQL. Date. valueof (sdate); // This method is commonly used, for example, convert the string retrieved from the database to the Time Format
// System. Out. println ("SD ---" + SD );
// String sdate1 = ". 0 ";
// Timestamp Ts = timestamp. valueof (sdate1 );
// System. Out. println ("Ts =" + TS );

// Time Zone Processing
// Obtain the time of Japan
Timezone tzjapan = timezone. gettimezone ("Japan"); // obtain the time zone
Calendar cjapan = new gregoriancalendar (tzjapan); // The internal parameter is ID. You can use the following method to obtain the ID:
System. Out. println (cjapan. Get (calendar. hour_of_day ));

// Obtain each valid ID
// Method 1
String [] Ss = timezone. getavailableids ();
For (INT I = 0; I <ss. length; I ++ ){
System. Out. println (ss [I]);
}
// Method 2
/*
For (string str2: timezone. getavailableids ()){
System. Out. println (str2 );
}
*/

Long L2 = system. currenttimemillis ();
System. Out. println ("execution time:" + (l2-l1) + "millisecond ");
}

}

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.