Common Java class libraries-date operations

Source: Internet
Author: User

1. The Date class is defined in the java. util package. It is simple to use and can be directly output as an instantiated object. If you want to output the time in your own format, you can use the Calendar class. [Java] import java. util. date; public class DateDemo01 {public static void main (String args []) {Date date = new Date (); // instantiate the Date object System directly. out. println ("current date:" + date) ;}; 2. Calendar class [java] import java. util. *; public class DateDemo02 {public static void main (String args []) {Calendar calendar = new GregorianCalendar (); // instantiate the Calendar class Object System. out. println ("YEAR:" + calendar. get (Calendar. YEAR); System. out. println ("MONTH:" + (calendar. get (Calendar. MONTH) + 1); System. out. println ("DAY_OF_MONTH:" + calendar. get (Calendar. DAY_OF_MONTH); System. out. println ("HOUR_OF_DAY:" + calendar. get (Calendar. HOUR_OF_DAY); System. out. println ("MINUTE:" + calendar. get (Calendar. MINUTE); System. out. println ("SECOND:" + calendar. get (Calendar. SECOND); System. out. println ("MILLISECOND:" + calendar. get (Calendar. MILLISECOND) ;}}; 3. DateFormat class public abstract class DateFormat extends Format [java] import java. text. dateFormat; import java. util. date; public class DateDemo03 {public static void main (String args []) {DateFormat df1 = null; // declare a DateFormat df2 = null; // declare a DateFormat df1 = DateFormat. getDateInstance (); // obtain the date's DateFormat object df2 = DateFormat. getDateTimeInstance (); // obtain the DateFormat Object System of the date and time. out. println ("DATE:" + df1.format (new Date (); // format System by DATE. out. println ("DATETIME:" + df2.format (new Date (); // formatted by Date and time}; [java] import java. text. dateFormat; import java. util. date; import java. util. locale; public class DateDemo04 {public static void main (String args []) {DateFormat df1 = null; // declare a DateFormat df2 = null; // declare a DateFormat df1 = DateFormat. getDateInstance (DateFormat. YEAR_FIELD, new Locale ("zh", "CN"); // obtain the DateFormat object df2 = DateFormat of the date. getDateTimeInstance (DateFormat. YEAR_FIELD, DateFormat. ERA_FIELD, new Locale ("zh", "CN"); // obtain the DateFormat Object System of the date and time. out. println ("DATE:" + df1.format (new Date (); // format System by DATE. out. println ("DATETIME:" + df2.format (new Date (); // format by Date and time}; 4. SimpleDateFormat class [java] import java. text. *; import java. util. *; public class DateDemo05 {public static void main (String args []) {String strDate = "10:11:30. 345 "; // prepare the first template and extract the datetime String pat1 =" yyyy-MM-dd HH: mm: ss From the String. SSS "; // prepare the second template, and convert the extracted date number to the specified format String pat2 =" yyyy MM dd HH mm min ss sec SSS millisecond "; simpleDateFormat sdf1 = new SimpleDateFormat (pat1); // instantiate the template object SimpleDateFormat sdf2 = new SimpleDateFormat (pat2); // instantiate the template object Date d = null; try {d = sdf1.parse (strDate); // extract the date from the given string} catch (Exception e) {// If the provided string format is incorrect, handle exceptions. e. printStackTrace (); // print exception information} System. out. println (sdf2.format (d); // convert the date to a new format }};

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.