I receive this "10/1/2018, 1:27:42 PM" as date from server. Now want to convert it to SimpleDateFormat("yyyy-MM-dd HH:mm:ss"). But to do this i need to know the format of existing date string. How i can find a correct format of "10/1/2018, 1:27:42 PM"?
Asked
Active
Viewed 456 times
-2
-
1it's `"MM/dd/yyyy, HH:mm:ss a"` – Jeel Vankhede Oct 01 '18 at 18:00
-
Or, @JeelVankhede, perhaps it’s `d/M/yyyy h:mm:ss a`? `d/M` in unilkely since the question was asked on October 1. Single `M` and single `d` are both likely. Single lowercase `h` is for sure. – Ole V.V. Oct 02 '18 at 03:39
-
@OleV.V. May be or may not be, it was unclear from question. – Jeel Vankhede Oct 02 '18 at 04:42
-
@gouravsarswa Why are you asking us? What are you expecting us to tell you that you cannot already see from the example string? Ask the folks responsible for the server from which you got it. Or did I misunderstand the intention of your question? – Ole V.V. Oct 02 '18 at 08:09
-
As an aside consider throwing away the long outmoded and notoriously troublesome `SimpleDateFormat` and friends, and adding [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP) to your Android project in order to use `java.time`, the modern Java date and time API. It is so much nicer to work with. `LocalDateTime.parse("10/1/2018, 1:27:42 PM", DateTimeFormatter.ofPattern("M/d/u, h:mm:ss a", Locale.ENGLISH)).format(DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss"))` gives you `2018-10-01 13:27:42`. – Ole V.V. Oct 02 '18 at 08:29
1 Answers
-1
You need to use SimpleDateFormat.toPattern() to get the pattern. Use this function in this way -
SimpleDateFormat sdf = new SimpleDateFormat();
// get the current date and time
Calendar cal = Calendar.getInstance();
String dateToday = sdf.format(cal.getTime());
// get the pattern used by the formatter and print it
String pattern = sdf.toPattern();
System.out.println("Pattern:"+pattern);

Anirban Roy
- 180
- 6