Java string to date or date to string

Source: Internet
Author: User

Java string to date or date to string

In the article, the API is SimpleDateFormat, it belongs to Java.text.SimpleDateFormat, so please remember import come in!

Usage: SimpleDateFormat SDF = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");

This line is the most important, it established the format of the conversion, YYYY is the full ad year, MM is the month, DD is the date, as for HH:mm:ss do not need me to explain it!

PS: Why some format uppercase, some format lowercase, it is afraid to avoid confusion, such as MM is the month, MM is divided; HH is a 24-hour system, and HH is 12-hour system 1. String to date: 2002-10-8 15:30:22 to turn it into a date, you can use

Date date = Sdf.parse ("2002-10-8 15:30:22");

2. Date-to-string if you turn today's date into a string available

String datestr = Sdf.format (New Date ());

The contents of this string will resemble 2002-10-08 14:55:38

With this API, we can turn the date into the string format we want, for example, if we want to export the date to October 08, 2002,

We can write this:

SimpleDateFormat SDF = new SimpleDateFormat ("yyyy mm month DD day"); String datestr = Sdf.format (New Date ());

DATESTR will output according to the format we set.
Attached SimpleDateFormat parser = new SimpleDateFormat ("Eeee, MMMM dd, yyyy"); SimpleDateFormat formatter = new SimpleDateFormat ("EEE. Mm/dd ");

Java string to date or date to string

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.