Parsing Date and time from "yyyy-MM-dd hh:mm:ss.SSS" -------> "hh:mm aa" in android

10,412

Solution 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

Solution 2

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

Solution 3

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

Solution 4

Use this readFormat

SimpleDateFormat readFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Share:
10,412

Related videos on Youtube

Nj_96
Author by

Nj_96

Updated on June 04, 2022

Comments

  • Nj_96
    Nj_96 almost 2 years

    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
      

      */

    • rupinderjeet
      rupinderjeet about 6 years
      try simpleDateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
    • Ole V.V.
      Ole V.V. about 6 years
      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.
      Ole V.V. about 6 years
      As an aside consider throwing away the long outmoded and notoriously troublesome SimpleDateFormat and friends, and adding 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.
  • Ratilal Chopda
    Ratilal Chopda about 6 years
    you can parse your date 2018-02-05 17:00:52.503 instead of my date 2018-02-05 17:08:52.503
  • Ratilal Chopda
    Ratilal Chopda about 6 years
    @NithinN.j please check my Edit answer.
  • Ratilal Chopda
    Ratilal Chopda about 6 years
    @Nithin N.j Thank you, Happy to help you
  • Nj_96
    Nj_96 about 6 years
    It works.I just declared DateFormat readFormat , DateFormat writeFormat instead of SimpleDateFormat readFormat and SimpleDateFormat writeFormat . Thanks for the help bro...
  • Ole V.V.
    Ole V.V. about 6 years
    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.