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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38410976/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 20:17:42  来源:igfitidea点击:

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

javadatesimpledateformat

提问by sparrow

I have a String like "2015-07-16 17:07:21". I want to convert it into a Date of same format. I tried something like this:

我有一个像"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"); 

But output was different format like Thu Jul 16 17:00:21 IST 2015. How can I get it work as i need. can anyone help me. I know this might be a duplicate but i didn't find any luck.

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

采纳答案by sinclair

From the Java API https://docs.oracle.com/javase/8/docs/api/java/util/Date.html

来自 Java API https://docs.oracle.com/javase/8/docs/api/java/util/Date.html

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

类 Date 表示特定的时刻,精度为毫秒。

Try to understand the difference/connection of Dateand DateFormat.

试着去理解的差异/连接DateDateFormat

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) );

  // ....
}

Output: (Please note that the Date stays the same, just its representation (format) changes)

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

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

回答by guleryuz

try format string like: yyyy-MM-dd HH:mm:ss

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

M : month, m: minute

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)));
}

also, a date does not have a format, it has a value representing a point in time. if you want to display it in some format you should use SimpleDateFormat

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

回答by CodeIt

See this code:

看到这个代码:

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)
}

Date class always stores date in its default format. The SimpleDateFormathelps to format date using its object. It doesn't make any changes to the date class object. Rather it converts the Date classto User definedformat which doesn't have any effect on the Date class object. I guess it is clear.

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

回答by CK Wolfe

Try to use this:

尝试使用这个:

    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();
    }
}

Output:

输出:

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

Note:For complete date and time patterns, please refer to this http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html

注意:有关完整的日期和时间模式,请参阅此 http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html