The input string is like mentioned below
20110913T100702.631 GMT
The out put needed is in the format like this
Tuesday, September 13, 2011 17:52:PM
Can you please help me on this.开发者_开发问答 (In this example the input value and the out put value are not connected those are 2 separate values)
Try:
private String formatDate() throws Exception {
DateFormat inputFormat = new SimpleDateFormat("yyyyMMdd'T'HHmmss.SSS Z");
DateFormat outputFormat = new SimpleDateFormat("EEEEE', 'MMMMM' 'dd', 'yyyy' 'h:mm:a");
Date date = inputFormat.parse("20110913T100702.631 GMT ");
return outputFormat.format(date);
}
You need class SimpleDateFormat or DateFormat http://javatechniques.com/blog/dateformat-and-simpledateformat-examples/
tl;dr
ZonedDateTime.parse (
"20110913T100702.631 GMT" ,
DateTimeFormatter.ofPattern( "uuuuMMdd'T'HH:mm:ss.SSS z" )
).format(
DateTimeFormatter.ofLocalizedDateTime( FormatStyle.FULL , FormatStyle.SHORT )
.withLocale( Locale.US )
)
Tuesday, September 13, 2011 10:07 AM
ISO 8601
Your input string nearly complies with a format defined as a “basic” version of the standard ISO 8601 format. The word basic means minimizing the use of the separators otherwise used by the more common “expanded” version of the ISO 8601 format.
String input = "20110913T100702.631 GMT" ;
Using java.time
The other Answers use the troublesome old legacy date-time classes, now supplanted by the java.time classes.
The java.time classes use many of the standard ISO 8601 formats by default when parsing/generating strings. So often there is no need to specify a formatting pattern. But this particular basic format is not supported by default, so we must specify a formatting pattern.
DateTimeFormatter fInput = DateTimeFormatter.ofPattern ( "uuuuMMdd'T'HHmmss.SSS z" );
Parse as an ZonedDateTime
.
ZonedDateTime zdt = ZonedDateTime.parse ( input, fInput );
To generate a string representing the value of the ZonedDateTime
object in your desired format, you could define a specify formatting pattern. But I suggest you instead let java.time automatically localize for you.
To localize, specify:
FormatStyle
to determine how long or abbreviated should the string be.Locale
to determine (a) the human language for translation of name of day, name of month, and such, and (b) the cultural norms deciding issues of abbreviation, capitalization, punctuation, separators, and such.
So…
// Generate output string
Locale locale = Locale.US; // Or Locale.CANADA_FRENCH, Locale.ITALY, etc.
DateTimeFormatter fOutput =
DateTimeFormatter.ofLocalizedDateTime ( FormatStyle.FULL, FormatStyle.SHORT ) // Specify format style of date portion, then time-of-day portion.
.withLocale ( locale );
String output = zdt.format ( fOutput );
Dump to console.
// Dump to console
System.out.println ( "input: " + input );
System.out.println ( "zdt.toString(): " + zdt );
System.out.println ( "output: " + output );
See this code run live at IdeOne.com.
input: 20110913T100702.631 GMT
zdt.toString(): 2011-09-13T10:07:02.631Z[GMT]
output: Tuesday, September 13, 2011 10:07 AM
About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date
, Calendar
, & SimpleDateFormat
.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
Where to obtain the java.time classes?
- Java SE 8, Java SE 9, and later
- Built-in.
- Part of the standard Java API with a bundled implementation.
- Java 9 adds some minor features and fixes.
- Java SE 6 and Java SE 7
- Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
- Android
- The ThreeTenABP project adapts ThreeTen-Backport (mentioned above) for Android specifically.
- See How to use ThreeTenABP….
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval
, YearWeek
, YearQuarter
, and more.
精彩评论