Java Learning notes-dateformat and SimpleDateFormat

Source: Internet
Author: User
Tags dateformat

SimpleDateFormat is a subclass of DateFormat, it gives me some ways to complete the conversion of strings and time objects!

DateFormat is an abstract class and cannot create its own instance. It has only one subclass SimpleDateFormat

DateFormat belongs to Java.text class, should add import Java.text.DateFormat before use;

SimpleDateFormat Similarly, should add import Java.text.SimpleDateFormat before use;

Format

Package mydate;

Import Java.text.DateFormat;
Import Java.text.SimpleDateFormat;
Import Java.util.Date;

public class Mycode02 {

public static void Main (string[] args) {
DateFormat df = new SimpleDateFormat ("yyyy mm month DD Day Hh:mm:ss");
Date A = new date ();
String str = Df.format (a); Converts a time object into a string according to a formatted string
System.out.println (str);

}

}

Output Result:

November 13, 2016 02:45:06

Parse (parse)

Package mydate;

Import Java.text.DateFormat;
Import java.text.ParseException;
Import Java.text.SimpleDateFormat;
Import Java.util.Date;

public class Mycode02 {

public static void Main (string[] args) {
String str = "1977-7-7"; Green part must format consistent, otherwise error!
DateFormat df = new SimpleDateFormat ("YYYY-MM-DD"); Create a formatter
Date A;
try {
A = Df.parse (str);
System.out.println (a);
} catch (ParseException e) {
E.printstacktrace ();
}
}

}

Java Learning notes-dateformat and SimpleDateFormat

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.