字符串到日期,不一致

时间:2018-08-31 07:52:12

标签: java string date simpledateformat datetime-parsing

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


public class formatTest {
    public static void main(String[] args){

        String str  = "2017-08-07 6:6:6";
        SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss");
        try {
            Date d = sdf.parse(str);
            Date date = sdf.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
}

1 个答案:

答案 0 :(得分:1)

将YYYY更改为yyyy

    String str = "2017-08-07 06:06:06";
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");