Java Learning note 07--Date operation class

Source: Internet
Author: User

Java Learning note 07--Date operation class

First, Date class

The date class is defined in the Java.util package, and the date class itself is very simple to output its instantiated object directly.

[Java]View Plaincopy
    1. Public class T {
    2. public static void Main (string[] args) {
    3. Date date = new Date ();
    4. System.out.println ("Current date:" +date); //Current date: Thu 23:00:57 CST
    5. }
    6. }


Second, Calendar class

Use this class to make the date accurate to milliseconds

Public abstract class Calendar

extends Object The Calendar class is an abstract class, and since it is an abstract class, it must not be used directly, it is necessary to use the concept of object polymorphism to instantiate this type of object through an upward transformation relationship. get a full date through the Calendar class, using its subclasses. [Java]View Plaincopy
  1. Public class T {
  2. public static void Main (string[] args) {
  3. Calendar calendar = new GregorianCalendar ();
  4. //calendar C = calendar.getinstance ();
  5. SYSTEM.OUT.PRINTLN ("Year:" +calendar.get (Calendar.year));
  6. System.out.println ("Month:" + (Calendar.get (calendar.month) +1));
  7. System.out.println ("Day:" +calendar.get (Calendar.day_of_month));
  8. System.out.println ("Time:" +calendar.get (Calendar.hour_of_day));
  9. System.out.println ("points:" +calendar.get (Calendar.minute));
  10. System.out.println ("Seconds:" +calendar.get (Calendar.second));
  11. System.out.println ("MS:" +calendar.get (Calendar.millisecond));
  12. }
  13. }


Third, DateFormat class

This class is a date formatted class, specially formatted for the operation of the date, because the Java.util.Date class itself already contains the full date, so just need to install some good format for this date to format the display.

Public abstract classDateFormat

This class is also an abstract class, and the DateFormat class itself provides an operation that can be instantiated directly for it.

Get date DateFormat object public static final DateFormat getdateinstance ()

Gets the date and time of the DateFormat object public static final DateFormat getdatetimeinstance ()

Use the DateFormat class directly to complete the conversion of date.

Public final String format (date date)

[Java]View Plaincopy
  1. Public class T {
  2. public static void Main (string[] args) {
  3. DateFormat df1 = Dateformat.getdateinstance (); //Get date of DateFormat object
  4. DateFormat DF2 = Dateformat.getdatetimeinstance (); //Get the date and time of the DateFormat object
  5. System.out.println (Df1.format (new Date ()));
  6. System.out.println (Df2.format (new Date ()));
  7. }
  8. }


Four, SimpleDateFormat class

[Java]View Plaincopy
  1. Public class T2 {
  2. public static void Main (string[] args) throws parseexception {
  3. String str = "1990-04-13";
  4. String Parten = "Yyyy-mm-dd";
  5. SimpleDateFormat sdf1 = new SimpleDateFormat (Parten);
  6. SimpleDateFormat SDF2 = new SimpleDateFormat ("yyyy year mm DD day");
  7. Date date = Sdf1.parse (str); //Parse string, convert string to date type
  8. System.out.println (Sdf2.format (date));
  9. System.out.println ("cascading form:" +new SimpleDateFormat ("yyyy mm month DD Day"). Format (new SimpleDateFormat ("  Yyyy-mm-dd "). Parse (str));
  10. System.out.println ("----sad dividing line------");
  11. String s = "2008-08-08";
  12. Date d = new SimpleDateFormat ("Yyyy-mm-dd"). Parse (s);
  13. System.out.println (new SimpleDateFormat ("yyyy mm month DD Day"). Format (d));
  14. }
  15. }


Java Learning note 07--Date operation class

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.