如何将字符串转换为日期格式yyyy-MM-dd HH:MM:ss [英] How to convert a String to Date of format yyyy-MM-dd HH:MM:ss

查看:239
本文介绍了如何将字符串转换为日期格式yyyy-MM-dd HH:MM:ss的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串,如2015-07-16 17:07:21。我想将其转换为相同格式的日期。我尝试过这样的东西。

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.

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

但输出格式不同于Thu 7月16日17:00:21 IST 2015 。我如何可以让我按需要工作。任何人都可以帮助我我知道这可能是重复的,但我没有找到任何运气。

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.

推荐答案

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

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


类Date表示一个特定的时间,以毫秒的精度。

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

尝试了解 Date DateFormat

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

这篇关于如何将字符串转换为日期格式yyyy-MM-dd HH:MM:ss的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆