I am trying to use the Android SimpleDateFormat
like this:
String _Date = "2010-09-29 08:45:22" SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd"); try { Date date = fmt.parse(_Date); return fmt.format(date); } catch(ParseException pe) { return "Date"; }
The result is good and I have: 2010-09-29
But if I change the SimpleDateFormat
to
SimpleDateFormat("dd-MM-yyyy");
the problem is that I will got 03-03-0035 !!!!
Why and how to get the format like dd-MM-yyyy
?
Creating SimpleDateFormat instance String pattern = "MM-dd-yyyy"; SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern); In the example above the String pattern is the pattern which will be used to format a date and the output will be generated in that pattern as “MM-dd-yyyy”.
SimpleDateFormat is a concrete class for formatting and parsing dates in a locale-sensitive manner. It allows for formatting (date → text), parsing (text → date), and normalization. SimpleDateFormat allows you to start by choosing any user-defined patterns for date-time formatting.
Don't use SimpleDateFormat . Java 8 has a better and more enhanced DateTimeFormatter which is also thread-safe. You should also avoid using Date and Calendar classes, and try to use Java 8 DateTime classes like OffsetDateTime , ZonedDateTime , LocalDateTime , LocalDate , LocalTime etc.
Class SimpleDateFormat. Deprecated. A class for parsing and formatting dates with a given pattern, compatible with the Java 6 API.
I assume you would like to reverse the date format?
SimpleDateFormat can be used for parsing and formatting. You just need two formats, one that parses the string and the other that returns the desired print out:
SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd"); Date date = fmt.parse(dateString); SimpleDateFormat fmtOut = new SimpleDateFormat("dd-MM-yyyy"); return fmtOut.format(date);
Since Java 8:
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd").withZone(ZoneOffset.UTC); TemporalAccessor date = fmt.parse(dateString); Instant time = Instant.from(date); DateTimeFormatter fmtOut = DateTimeFormatter.ofPattern("dd-MM-yyyy").withZone(ZoneOffset.UTC); return fmtOut.format(time);
Below is all date formats available, read more doc here.
Symbol Meaning Kind Example D day in year Number 189 E day of week Text E/EE/EEE:Tue, EEEE:Tuesday, EEEEE:T F day of week in month Number 2 (2nd Wed in July) G era designator Text AD H hour in day (0-23) Number 0 K hour in am/pm (0-11) Number 0 L stand-alone month Text L:1 LL:01 LLL:Jan LLLL:January LLLLL:J M month in year Text M:1 MM:01 MMM:Jan MMMM:January MMMMM:J S fractional seconds Number 978 W week in month Number 2 Z time zone (RFC 822) Time Zone Z/ZZ/ZZZ:-0800 ZZZZ:GMT-08:00 ZZZZZ:-08:00 a am/pm marker Text PM c stand-alone day of week Text c/cc/ccc:Tue, cccc:Tuesday, ccccc:T d day in month Number 10 h hour in am/pm (1-12) Number 12 k hour in day (1-24) Number 24 m minute in hour Number 30 s second in minute Number 55 w week in year Number 27 G era designator Text AD y year Number yy:10 y/yyy/yyyy:2010 z time zone Time Zone z/zz/zzz:PST zzzz:Pacific Standard
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With