2

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.

  • Are you seeing any exception? If so, please post the stacktrace in your question. – Ole V.V. Dec 19 '17 at 12:25
  • I recommend `LocalDateTime.parse(date).format(DateTimeFormatter.ofPattern("MM/dd/uuuu"))`. Use [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP) to use this on Android. – Ole V.V. Dec 19 '17 at 12:28
  • Possible duplicate of [Change date format in a Java string](https://stackoverflow.com/questions/4772425/change-date-format-in-a-java-string). Use your search engine to find many more similar questions and answers. – Ole V.V. Dec 19 '17 at 12:31

6 Answers6

8

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

Ramesh sambu
  • 3,577
  • 2
  • 24
  • 39
  • 1
    Ah, so it was necessary two `SimpleDateFormat`. It worked for. But what a mess just for a simple format. Java. –  Dec 19 '17 at 12:12
  • 2
    Not a mess Read [SimpleDateFormat](https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html). – ADM Dec 19 '17 at 12:27
  • Can’t resist, please excuse: The `SimpleDateFormat` class *is* a mess. In 2017 no one should struggle with one. Let alone two. The constructive suggestion? [JSR-310, the modern Java date and time API also known as `java.time`](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Dec 20 '17 at 10:39
2

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);
Harshit kyal
  • 365
  • 1
  • 5
  • 24
2

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.

aslamhossin
  • 1,217
  • 12
  • 22
1

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

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

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;

        }
Vidhi Dave
  • 5,614
  • 2
  • 33
  • 55
0

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());
ahuemmer
  • 1,653
  • 9
  • 22
  • 29
  • Thaks for wanting to contribute. Please don’t try this. It’s better to stay far away from the notoriously troublesome and long outdated `SimpleDateFormat` class and friends. Furthermore in case of parse error, this will give you today^s date, which is probably not what you want and which you may only discover long after your app has been deployed. – Ole V.V. Apr 01 '22 at 03:11