如何将字符串转换为日期格式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