user4383363
user4383363

Reputation:

Formatting a String date into a different format in Java

I am trying to format a JSON which has a date key in the format:

date: "2017-05-23T06:25:50"

My current attempt is the following:

private String formatDate(String date) {
  SimpleDateFormat format = new SimpleDateFormat("MM/DD/yyyy");
  String formattedDate = "";

  try {
    formattedDate = format.format(format.parse(String.valueOf(date)));
  } catch (ParseException e) {
    e.printStackTrace();
  }

  return formattedDate;
}

But in this occasion, the result isn't shown in my app, the TextView is invisible and I couldn't log anything.

I really tried other solutions, but no success for me. I don't know if it's because the incoming value is a string.

Upvotes: 2

Views: 7698

Answers (6)

Try this:

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

Calendar c = Calendar.getInstance();
try {
    c.setTime(sdf.parse(dt));
} catch (ParseException e) {
    e.printStackTrace();
}

SimpleDateFormat sdf1 = new SimpleDateFormat("dd/MM/yyyy");
String output = sdf1.format(c.getTime());

Upvotes: 0

Anonymous
Anonymous

Reputation: 86272

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

Upvotes: 1

aslamhossin
aslamhossin

Reputation: 1277

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.

Upvotes: 2

Vidhi Dave
Vidhi Dave

Reputation: 5684

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;

        }

Upvotes: 0

Harshit kyal
Harshit kyal

Reputation: 350

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);

Upvotes: 2

Ramesh sambu
Ramesh sambu

Reputation: 3539

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

Upvotes: 8

Related Questions