Java string turn date or date to string

Source: Internet
Author: User
Tags string format

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 year, MM is the month, DD is the date, as for HH:mm:ss don't 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 the minute; HH is a 24-hour system, while HH is a 12-hour system.

1. String Turn date

2008-07-10 19:20:00 to convert it to a date, you can use date date = Sdf.parse ("2008-07-10 19:20:00");

2. Date-turn string

If you turn today's date into a string, you can use string str = Sdf.format (new date ());

The format of this string content resembles the 2008-07-10 19:20:00.

Through this API, we can turn the date into the string format we want, such as to export the date as July 10, 2008, so we can write:

SimpleDateFormat SDF = new SimpleDateFormat ("yyyy mm month DD day");

String str = sdf.format (new Date ());

STR will be exported in accordance with the format we have set.

A simple example with a good writing:

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

public class Convertdemo {

/**
* Date converted to string
* @param date 
* @return str/public
static String Dat   ETOSTR (date date) { 
simpledateformat format = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");
String str = format.format (date);
return str;
} 

/**
* String converted to date
* @param str
* @return Date
/public static date Strtodate (String str) {
c22/> simpledateformat format = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");
Date date = null;
try {
date = Format.parse (str);
catch (ParseException e) {
e.printstacktrace ();
} return
date;
}
public static void Main (string[] args) { 
Date date = new Date ();
SYSTEM.OUT.PRINTLN ("date-turn string:" + convertdemo.datetostr (date));
System.out.println ("String Turn Date:" + convertdemo.strtodate (convertdemo.datetostr

(date)); 
}

   
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.