tl;dr
OffsetDateTime.parse(
"September, 25 2012 02:09:42 +0000" ,
DateTimeFormatter.ofPattern( "MMMM, d uuuu HH:mm:ss Z" , Locale.US )
).format(
DateTimeFormatter.ofPattern( "hh:mm a 'on' dd/MM/uuuu" , Locale.US )
)
02:09 AM on 25/09/2012
Avoid legacy date-time classes
The other Answers are now outmoded, using troublesome old date-time classes that are now legacy. The old classes are supplanted by the java.time classes. For earlier Android, see the last bullets below.
java.time
Define a formatting pattern to match your input string.
String input = "September, 25 2012 02:09:42 +0000" ;
DateTimeFormatter f = DateTimeFormatter.ofPattern( "MMMM, d uuuu HH:mm:ss Z" , Locale.US ) ; // Specify a locale for the human language by which to parse the name of the month.
Parse as an OffsetDateTime
given that your input specifies an offset-from-UTC but not a full time zone.
OffsetDateTime odt = OffsetDateTime odt = OffsetDateTime.parse( input , f );
odt.toString(): 2012-09-25T02:09:42Z
To generate a string in an alternate format, define a DateTimeFormatter
with a custom formatting pattern. Pay attention to the uppercase/lowercase of your formatting code characters, and study closely the documentation. Note that colons, spaces, and slashes are known by the formatter, so no need to escape those characters with the single-quote marks.
DateTimeFormatter fOutput = DateTimeFormatter.ofPattern( "hh:mm a 'on' dd/MM/uuuu" , Locale.US ) ;
String output = odt.format( fOutput) ;
02:09 AM on 25/09/2012
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?
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.