Using java I need to parse this 2014-08-31 13:53:42.0
to 31-AUG-14 01.53.42 PM
Asked
Active
Viewed 77 times
-6

Burkhard
- 14,596
- 22
- 87
- 108

pasupula phaneesh
- 13
- 4
-
5Do search for `SimpleDateFormat` – MadProgrammer Sep 01 '14 at 07:31
-
but allready i have a date in String format "2014-08-31 13:53:42.0" i need this to convert – pasupula phaneesh Sep 01 '14 at 07:34
-
1You're missing the point. Trying to "format" a `String` is right pain in the backend. Start by getting the `String` value into a more malleable format which will provide you with more options as how to format it the way you want to. `SimpleDateFormat` doesn't just format a value, it can convert it from a `String` to a `Date` object... – MadProgrammer Sep 01 '14 at 07:37
1 Answers
5
Start by doing some research into java.text.SimpleDateFormat
which can be used to parse String
values of a verity of formats into a java.util.Date
.
You can then use another SimpleDateFormat
to format the value to the format that you want, for example
try {
String in = "2014-08-31 13:53:42.0";
SimpleDateFormat sdfIn = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
Date date = sdfIn.parse(in);
System.out.println(date);
SimpleDateFormat sdfOut = new SimpleDateFormat("dd-MMM-yy hh:mm.ss a");
System.out.println(sdfOut.format(date));
} catch (ParseException ex) {
}
Which outputs
Sun Aug 31 13:53:42 EST 2014
31-Aug-14 01:53.42 PM

MadProgrammer
- 343,457
- 22
- 230
- 366
-
I think it's better if we don't provide full solutions for question like that. – Maroun Sep 01 '14 at 07:33
-
@MarounMaroun If you can find a duplicate, please tell me and I'll delete the answer and close the question – MadProgrammer Sep 01 '14 at 07:35
-
Your answer is good since it contains explanation and not only code, it's detailed one, +1. But I think that OP might always think that he shouldn't do research and ask questions here with minimal understanding. – Maroun Sep 01 '14 at 07:36
-
@MarounMaroun I'd prefer more research as well, it's a common problem with a common solution ;) - But teaching myself Objective C at the moment, what might seem simple to your mean, isn't always that simple for other people. That's when I search SO ;) – MadProgrammer Sep 01 '14 at 07:38
-
@MarounMaroun What you say is true but Stackoverflow is meant to provide answer to coding queries asked by users, if a user asks his homework or anything else without research its his loss. Maybe this user did a lot of research and could not find solution, or maybe he didn't. If we start assuming many questions may remain unanswered. +1 for the answer BTW – Mustafa sabir Sep 01 '14 at 07:41