2

Need to convert the time to a specified format but getting a wrong result.Please help

DateFormat readFormat = new SimpleDateFormat( "yyyy-MM-dd hh:mm:ss.sss"); 
DateFormat writeFormat = new SimpleDateFormat( "hh:mm aa");   //Expecting like 05:00 

String from = "2018-02-05 17:00:52.503";
String t0   = "2018-02-05 17:00:55.372";

Date date = null;  //IST
Date date2=null;
try {
    date = readFormat.parse(from);
    date2 =readFormat.parse(to);
} catch (ParseException e) {       
    e.printStackTrace();
}

if (date != null) {                       
    from = writeFormat.format(date);    //Expecting a result of 05.00 pm         
    to   = writeFormat.format(date2);   //Expecting a result of 05:00 pm

}
  • List item## Need out put like this ##

    /*But getting an output as:

                        from= "05:08 pm"    instead of 05:00 pm
                        to  = "05:06 pm"    instead of 05:00 pm
    

    */

Ram Koti
  • 2,203
  • 7
  • 26
  • 36
Nj_96
  • 116
  • 1
  • 1
  • 11
  • try `simpleDateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));` – rupinderjeet Feb 06 '18 at 06:07
  • Just read your own title. :-) You need to watch the case of those format pattern letters closely. It’s so easy to use lowercase where uppercase should have been used or vice versa. You need uppercase `HH` and uppercase `SSS`. – Ole V.V. Feb 06 '18 at 08:46
  • 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. – Ole V.V. Feb 06 '18 at 08:47

4 Answers4

1

SimpleDateFormat readFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSS");

sanemars
  • 767
  • 5
  • 18
  • This is an improvement over the format pattern string in the question, but still will not parse `2018-02-07 12:42:43.296` correctly. Also code-only answers are seldom helpful, better to explain how you are solving the problem, for us all to learn. – Ole V.V. Feb 07 '18 at 07:43
1

Use this readFormat

SimpleDateFormat readFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
josh_gom3z
  • 294
  • 1
  • 13
1

Try this

SimpleDateFormat input = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
SimpleDateFormat output = new SimpleDateFormat("hh:mm aa");

        Date d = null;
        Date d1 = null;
        try 
        {
            d = input.parse("2018-02-05 17:08:52.503");
            d1 = input.parse("2018-02-05 17:06:55.372");

        } catch (ParseException e) {
            e.printStackTrace();
        }
        String formatted = output.format(d);
        String formatted1 = output.format(d1);

        Log.i("DATE", "" + formatted);
        Log.i("DATE1", "" + formatted1);

OUTPUT

I/DATE: 05:08 PM
I/DATE1: 05:06 PM

EDIT

SimpleDateFormat input = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
SimpleDateFormat output = new SimpleDateFormat("hh:mm aa");

        Date d = null;
        Date d1 = null;
        try 
        {
            d = input.parse("2018-02-05 17:00:52.503");
            d1 = input.parse("2018-02-05 17:00:55.372");

        } catch (ParseException e) {
            e.printStackTrace();
        }
        String formatted = output.format(d);
        String formatted1 = output.format(d1);

        Log.i("DATE", "" + formatted);
        Log.i("DATE1", "" + formatted1);

OUTPUT

I/DATE: 05:00 PM
I/DATE1: 05:00 PM
Ratilal Chopda
  • 4,162
  • 4
  • 18
  • 31
1

Your question has been answered already. I just wanted to demonstrate that java.time, the modern Java date and time API, is doing a somewhat better effort to be helpful with the very common incorrect case of format pattern letters for parsing. Let’s try to use your format pattern string with the modern DateTimeFormatter:

    DateTimeFormatter readFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss.sss");

This line throws java.lang.IllegalArgumentException: Too many pattern letters: s. So it doesn’t accept three lowercase s? It’s not telling us the case is incorrect, after all it cannot read our mind; but it does point to where one of the bugs is. Let’s correct:

    DateTimeFormatter readFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss.SSS");
    String from = "2018-02-05 17:00:52.503";
    LocalDateTime dateTime = LocalDateTime.parse(from, readFormatter);

This time we get a couple of lines further down, then hit a java.time.format.DateTimeParseException: Text '2018-02-05 17:00:52.503' could not be parsed: Invalid value for ClockHourOfAmPm (valid values 1 - 12): 17. Clock hour of AM/PM? We didn’t intend that, again it is very precise about where the bug is. Correct this one too:

    DateTimeFormatter readFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");

This time the string gets parsed into 2018-02-05T17:00:52.503 as desired.

On newer Android you can use java.time directly. For older Android add ThreeTenABP to your project and make sure you import the date and time classes from org.threeten.bp with subpackages.

Links

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161