I could not find a correctly and clean working solution for my Date which is formatted like this:
201406082159
(8th of June, 21:59 here)
Last I tried was this:
SimpleDateFormat format2 = new SimpleDateFormat("YYYYMMDDHHMM", Locale.ENGLISH);
Sadly it's not working and printing out
Sun Dec 29 21:00:00 CET 2013
Minutes are set up by m
not M
Years are set up by y
not Y
Days (in month) are set up by d
not D
(in year)
SimpleDateFormat format2 = new SimpleDateFormat("yyyyMMddHHmm", Locale.ENGLISH);
Source
I would suggest having a look at the api of SimpleDateFormat class.
http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
It would help you resolve your current question and you will certainly be able to find the correct pattern in the future if you have a different date format to parse.
You are using outmoded legacy classes. Use java.time classes for your date-time work.
DateTimeFormatter
Define a formatting pattern.
String input = "201406082159";
DateTimeFormatter f = DateTimeFormatter.ofPattern( "uuuuMMddHHmm" );
LocalDateTime
Your input lacks information about any offset-from-UTC or time zone. So we parse as a LocalDateTime
.
LocalDateTime ldt = LocalDateTime.parse( input , f );
ZonedDateTime
If you know the context of the data intends a certain time zone, apply it. Specify a proper time zone name. Never use the 3-4 letter abbreviation such as CET
or EST
or IST
as they are not true time zones, not standardized, and not even unique(!).
ZoneId zoneId = ZoneId.of( "Europe/Paris" );
ZonedDateTime zdt = ldt.atZone( zoneId );
About java.time
The java.time framework is built into Java 8 and later. These classes supplant the old troublesome date-time classes such as java.util.Date
, .Calendar
, & java.text.SimpleDateFormat
.
The Joda-Time project, now in maintenance mode, advises migration to java.time.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations.
Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport and further adapted to Android in ThreeTenABP.
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time.