How to change Date format type between dates?

人走茶凉 提交于 2020-01-06 20:05:20

问题


how to change this date type "Thu Feb 02 12:00:00 GMT-12:00 2012" to another date type "yyyy-mm-dd HH:mm:ss"? If "Thu Feb 02 12:00:00 GMT-12:00 2012" is the String type, how to convert Date type of "yyyy-MM-dd HH:mm:ss"?

java code:

DateFormat inputDF = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy");
   DateFormat outputDF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
   Date dd = inputDF.parse("Thu Feb 02 12:00:00 GMT-12:00 2012");
   String strDate = outputDF.format(dd);

The output is String type. I wanna get Date type output so I added following code.

   Date outputDate = outputDF.parse(strDate);

But I get again Date with this "EEE MMM dd HH:mm:ss Z yyyy" format. I want to get Date with this "yyyy-MM-dd HH:mm:ss" format and also want Date Type.

I also want to get the output type is Date type format.


回答1:


DateFormat inputDF = new SimpleDateFormat(
                "EEE MMM dd HH:mm:ss Z yyyy");
        DateFormat outputDF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println(outputDF.format(inputDF.parse("Thu Feb 02 12:00:00 GMT-12:00 2012")));



回答2:


String != Date

Your main problem is in thinking of a string as a date. It is not. Consider that "$12.34" is not a number, it is a textual representation of a number and currency type formatted for presentation. So too do you have a textual representation of a date-time and time zone.

String → Date-Time → String

So first step is parsing that string as a date-time object. Then use that date-time object to generate a new textual representation of its date-time value.

java.time

You are using the old date-time classes from the earliest version of Java. These classes, while a valiant industry-first effort, have proven to be confusing and flawed. Avoid them.

The java.time framework built into Java 8 and later supplants the troublesome old java.util.Date/.Calendar classes. The new classes are inspired by the highly successful Joda-Time framework, intended as its successor, similar in concept but re-architected. Defined by JSR 310. Extended by the ThreeTen-Extra project. See the Oracle Tutorial.

Formatter

We must specify a coded pattern to define a formatter with which we will parse your input strings. The codes are similar to the old java.text.SimpleDateFormat class but not exactly the same. So carefully study the doc. Note the use of O for localized zone-offset.

Note that on the formatter we use a second argument to specify a Locale. That Locale specifies the human language used to parse and translate the name of the month and day. If omitted the JVM’s current default Locale is used. That default can change at any moment, so I suggest always specifying the expected Locale.

String input = "Thu Feb 02 12:00:00 GMT-12:00 2012";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern ( "EEE MMM dd HH:mm:ss O yyyy" , Locale.ENGLISH );
ZonedDateTime zdt = ZonedDateTime.parse ( input , formatter );

Dump to console.

System.out.println ( "zdt: " + zdt );

2012-02-02T12:00-12:00

That output is standard ISO 8601 format. I suggest using these standard formats where possible because of they are easy to read, unambiguous, and, well, standard.

The format you want is close to standard but replaces the T with a SPACE. And your format omits any offset-from-UTC or time zone. Omitting the time zone is risky as human readers may assume the wrong time zone. But if you insist, define such a pattern yourself.

DateTimeFormatter formatterOutput = DateTimeFormatter.ofPattern ( "yyyy-MM-dd HH:mm:ss" );
String output = zdt.format ( formatterOutput );

Dump to console.

System.out.println ( "zdt: " + zdt + " output: " + output );

zdt: 2012-02-02T12:00-12:00 output: 2012-02-02 12:00:00

Better yet, let java.time localize for you. Note how we call withLocale to give the formatter a Locale whose language will be used for the name of month and name of day.

DateTimeFormatter formatterLocalized = DateTimeFormatter.ofLocalizedDateTime ( FormatStyle.FULL );
formatterLocalized = formatterLocalized.withLocale ( Locale.CANADA_FRENCH );
String outputLocalized = zdt.format ( formatterLocalized );

Dump to console.

System.out.println ( "zdt: " + zdt + " output: " + output + " outputLocalized: " + outputLocalized );

zdt: 2012-02-02T12:00-12:00 output: 2012-02-02 12:00:00 outputLocalized: jeudi 2 février 2012 12 h 00 -12:00




回答3:


String strDate = "Thu Feb 02 12:00:00 GMT-12:00 2012";
DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy - HH:mm:ss");
dateFormat.format(strDate);

And to get the result in Date, You can use the following :

Date d = new Date(dateFormat.format(strDate));



回答4:


You can't do that. The Date object does not have a representation format. It only has the data. The formatter has the format. Formatting is used to display data.




回答5:


String dateStart = "Thu Feb 02 12:00:00 GMT-12:00 2012";
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date d1 = format.parse(**dateStart**);
System.out.prinln("formatted date.." + d1);


来源:https://stackoverflow.com/questions/10796893/how-to-change-date-format-type-between-dates

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!