Formatting a String date into a different format in Java

北城余情 提交于 2019-11-29 12:57:58

Use this code to format your `2017-05-23T06:25:50'

String strDate = "2017-05-23T06:25:50";
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
Date convertedDate = new Date();
try {
    convertedDate = dateFormat.parse(strDate);
    SimpleDateFormat sdfnewformat = new SimpleDateFormat("MMM dd yyyy");
    String finalDateString = sdfnewformat.format(convertedDate);
} catch (ParseException e) {
    e.printStackTrace();
}

The converted finalDateString set to your textView

This will work for you.

String oldstring= "2017-05-23T06:25:50.0";
Date datee = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss").parse(oldstring);

Use this function :

 private String convertDate(String dt) {

            //String date="2017-05-23T06:25:50";

            try {
                SimpleDateFormat spf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); //date format in which your current string is
                Date newDate = null;
                newDate = spf.parse(dt);
                spf = new SimpleDateFormat("MM/DD/yyyy"); //date format in which you want to convert
                dt = spf.format(newDate);
                System.out.println(dt);

                Log.e("FRM_DT", dt);

            } catch (ParseException e) {
                e.printStackTrace();
            }
            return dt;

        }

This code worked for me :

 public static String getNewsDetailsDateTime(String dateTime) {
    @SuppressLint("SimpleDateFormat") DateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
    Date date = null;
    try {
        date = format.parse(dateTime);
    } catch (ParseException e) {
        e.printStackTrace();
    }

    @SuppressLint("SimpleDateFormat") String strPublishDateTime = new SimpleDateFormat("MMM d, yyyy h:mm a").format(date);
    return strPublishDateTime;
}

Out put format was : Dec 20 , 2017 2.30 pm.

TL;DR

private static final DateTimeFormatter DATE_FORMATTER 
        = DateTimeFormatter.ofPattern("MM/dd/uuuu");

private static String formatDate(String date) {
    return LocalDateTime.parse(date).format(DATE_FORMATTER);
}

Now formatDate("2017-05-23T06:25:50") returns the desired string of 05/23/2017.

java.time

In 2017 I see no reason why you should struggle with the long outdated and notoriously troublesome SimpleDateFormat class. java.time, the modern Java date and time API also known as JSR-310, is so much nicer to work with.

Often when converting from one date-time format to another you need two formatters, one for parsing the input format and one for formatting into the output format. Not here. This is because your string like 2017-05-23T06:25:50 is in the ISO 8601 format, the standard that the modern classes parse as their default, that is, without an explicit formatter. So we only need one formatter, for formatting.

What went wrong in your code

When I run your code, I get a ParseException: Unparseable date: "2017-05-23T06:25:50". If you didn’t notice the exception already, then you have a serious flaw in your project setup that hides vital information about errors from you. Please fix first thing.

A ParseException has a method getErrorOffset (a bit overlooked), which in this case returns 4. Offset 4 in your string is where the first hyphen is. So when parsing in the format MM/DD/yyyy, your SimpleDateFormat accepted 2017 as a month (funny, isn’t it?), then expected a slash and got a hyphen instead, and therefore threw the exception.

You’ve got another error in your format pattern string: Uppercase DD is for day-of-year (143 in this example). Lowercase dd should be used for day-of-month.

Question: Can I use java.time on Android?

Yes you can. It just requires at least Java 6.

  • In Java 8 and later the new API comes built-in.
  • In Java 6 and 7 get the ThreeTen Backport, the backport of the new classes (ThreeTen for JSR 310).
  • On Android, use the Android edition of ThreeTen Backport. It’s called ThreeTenABP.

Links

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!