The conversion method between Date and String in Java

  • 2020-05-12 02:33:26
  • OfStack

At the time of registration website, we often need to fill out personal information, such as name, age, date of birth, date of birth on the page of the value passed to the backend is a string, and we really need a date when the database type, displayed on the page, in turn, need to be retrieved from the database of date of birth, the type to date at this time, and then need to convert the date type string displayed in the page, Java API provides us with the date of the transfer with string class DateForamt. DateForamt is an abstract class, so you usually use its subclass SimpleDateFormat. SimpleDateFormat has four constructors, the second one being used most often.

In the constructor, pattern is the time mode. What is the specific mode? It is explained in API, as follows

1. Date to string (format)


package com.test.dateFormat;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.junit.Test;

public class Date2String {
  @Test
  public void test() {
    Date 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 years 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 to date (parse)


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 string = "2016-10-24 21:59:06";
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    System.out.println(sdf.parse(string));
  }
}

Mon Oct 24 21:59:06 CST 2016

When running a string to a date, be aware that the given pattern must match the given string format, otherwise an java.text.ParseException exception will be thrown. For example, this is an error


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 string = "2016-10-24";
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    System.out.println(sdf.parse(string));
  }
}

However, the given schema is less than a string


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 string = "2016-10-24 21:59:06";
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    System.out.println(sdf.parse(string));
  }
}

Mon Oct 24 00:00:00 CST 2016

You can see that the minutes and seconds are all 0, and it's not being resolved, and that's ok.


Related articles: