简体   繁体   中英

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

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

    */

的SimpleDateFormat readFormat =新的SimpleDateFormat( “YYYY-MM-DD HH:MM:SS SSS”);

使用这个readFormat

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM