Methods for converting date and string in Java _java

Source: Internet
Author: User

When we register the website, we often need to fill in personal information, such as name, age, date of birth, and so on, when the value of the date of birth on the page is passed to the background is a string, and we do need a date type when we put it in the database, in turn, we need to get the date of birth from the database when it is displayed The type is a date type, and then the date type needs to be turned into a string to be displayed on the page, and the Java API provides us with a class Dateforamt for the date and string to be transported to each other. Dateforamt is an abstract class, so it usually uses its subclass SimpleDateFormat. SimpleDateFormat has 4 constructors, most often the second.

The pattern in the constructor is the time pattern, and the specific patterns are described in the API, as follows

1. Date-turn string (formatted)

Package Com.test.dateFormat;

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

Import Org.junit.Test;

public class Date2string {
  @Test public
  void Test () {
    date = new Date ();
    SimpleDateFormat SDF = new SimpleDateFormat ("Yyyy-mm-dd");
    System.out.println (Sdf.format (date));
    SDF = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");
    System.out.println (Sdf.format (date));
    SDF = new SimpleDateFormat ("YYYY year mm month DD Day HH:mm:ss");
    System.out.println (Sdf.format (date));
  }


2016-10-24
2016-10-24 21:59:06
October 24, 2016 21:59:06

2, String date (parsing)

Package Com.test.dateFormat;

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

Import Org.junit.Test;

public class String2date {
  @Test public
  void Test () throws ParseException {
    string = "2016-10-24 21:59: ";
    SimpleDateFormat SDF = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");
    System.out.println (Sdf.parse (String));
  }

Mon Oct 21:59:06 CST 2016

When manipulating a string to date, it is important to note that the given pattern must match the given string format. Otherwise, the java.text.ParseException exception is thrown, for example, the following is wrong, the string does not give a time and seconds, then SimpleDateFormat of course can not give you the time to parse out the value of the seconds

Package Com.test.dateFormat;

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

Import Org.junit.Test;

public class String2date {
  @Test the public
  void Test () throws ParseException {
    string string = "2016-10-24"; 
   
    simpledateformat SDF = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");
    System.out.println (Sdf.parse (String));
  }


   

However, given a pattern that is less than a string, you can

Package Com.test.dateFormat;

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

Import Org.junit.Test;

public class String2date {
  @Test public
  void Test () throws ParseException {
    string = "2016-10-24 21: 59:06 ";
    SimpleDateFormat SDF = new SimpleDateFormat ("Yyyy-mm-dd");
    System.out.println (Sdf.parse (String));
  }

Mon Oct 00:00:00 CST 2016

It can be seen that every minute is 0, without being parsed.

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.