Q:

Need help making a better code, this one is so ugly

I have a query with the following data
85343
125242
65252
125655
185658
235758

Those are 24 hour times without the format. I tried parsing it with HHmmss to then give it the format hh:mm:ss a it kind of worked with the hours 12+ but with 12- it went to hell giving wrong times. I worked it out with the following code:
DateFormat dateFormat2 = new SimpleDateFormat("Hmmss");
DateFormat dateFormat21 = new SimpleDateFormat("HHmmss");
DateFormat dateFormat5 = new SimpleDateFormat("hh:mm:ss a");

System.out.println((Query.getString(1).length() == 5) ? 
        dateFormat5.format(dateFormat2.parse(Query.getString(1))) 
        : dateFormat5.format(dateFormat21.parse(Query.getString(1))));

it solves the problem with the time errors as I get the correct times 
8:53:43AM 
12:52:42PM 
6:52:52 AM 
12:56:55 PM 
6:56:58 PM 
11:57:58PM

Is there anyway to make this better?

A:

I wouldn't use two different DateFormat for parsing but only one :
DateFormat dateFormat21 = new SimpleDateFormat("HHmmss");
DateFormat dateFormat5 = new SimpleDateFormat("hh:mm:ss a");
String s = Query.getString(1);
if (s.length()==5) s = "0"+s;
System.out.println(dateFormat5.format(dateFormat21.parse(s)));

But there's no real problem in your code apart calling getString more than necessary.

