如何将字符串转换为格式为 yyyy-MM-dd HH:MM:ss 的日期

How to convert a String to Date of format yyyy-MM-dd HH:MM:ss

我有一个像 "2015-07-16 17:07:21" 这样的字符串。我想将它转换成相同格式的日期。我试过这样的事情:

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:MM:ss");
Date date = sdf.parse("2015-07-16 17:07:21"); 

但输出格式与 Thu Jul 16 17:00:21 IST 2015 不同。我怎样才能让它按我的需要工作。谁能帮我。我知道这可能是重复的,但我没有找到任何运气。

尝试格式化字符串,例如:yyyy-MM-dd HH:mm:ss

M:月,m:分钟

public static void main(String [] args){
    String dateString = "2015-07-16 17:07:21";
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Date date;
    try {
        date = sdf.parse(dateString);
    } catch (ParseException e) {
        e.printStackTrace();
        return;
    } 
    System.out.println(sdf.format(date));
    System.out.println(dateString.equals(sdf.format(date)));
}

另外,日期没有格式,它有一个代表时间点的值。如果你想以某种格式显示它,你应该使用 SimpleDateFormat

来自JavaAPIhttps://docs.oracle.com/javase/8/docs/api/java/util/Date.html

The class Date represents a specific instant in time, with millisecond precision.

尝试理解DateDateFormat的difference/connection。

public static void main(String [] args) throws ParseException{
    String dateString = "2015-07-16 17:07:21";
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    // use SimpleDateFormat to define how to PARSE the INPUT
    Date date = sdf.parse(dateString);

    // at this point you have a Date-Object with the value of
    // 1437059241000 milliseconds
    // It doesn't have a format in the way you think

    // use SimpleDateFormat to define how to FORMAT the OUTPUT
    System.out.println( sdf.format(date) );

    sdf = new SimpleDateFormat();
    System.out.println( sdf.format(date) );

  // ....
}

输出:(请注意日期保持不变,只是其表示(格式)发生变化)

2015-07-16 17:07:21
7/16/15 5:07 PM

查看此代码:

public static void main(String [] args){
    String dateString = "2015-07-16 17:07:21";
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Date date;
    try {
        date = sdf.parse(dateString);
    } catch (ParseException e) {
        e.printStackTrace();
        return;
    } 
    System.out.println(date); // Displays date in it's default format Jul 16 17:00:21 IST 2015
    System.out.println(dateString.equals(date)); // Returns false since 'Jul 16 17:00:21 IST 2015' is not equal to dateString (2015-07-16 17:07:21)
}

日期 class 始终以默认格式存储日期。 SimpleDateFormat 有助于使用其对象格式化日期。它不会对日期 class 对象进行任何更改。相反,它将 Date class 转换为 User defined 格式,这对 Date class 对象没有任何影响。我想已经很清楚了。

尝试使用这个:

    public static void main(String[] args) {
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String dateInString = "2015-07-16 17:07:21";
    try {

        Date date = formatter.parse(dateInString);

        System.out.println(date);
        System.out.println(formatter.format(date));

    } catch (ParseException e) {
        e.printStackTrace();
    }
}

输出:

Thu Jul 16 17:07:21 IST 2015
2015-07-16 17:07:21

注: 完整的日期和时间模式,请参考这个http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html