-2

I have a date coming like this as String

2017-05-20T18:00:56Z

which I want to convert something like this Sat 20th May, 06:00

How can I do that? I tried below code but that doesn't seem to be working, also there are many similar post on same but isn't working out for me

        SimpleDateFormat myFormat = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss.SSS'Z'");
        try {
            Date myDate = myFormat.parse(actorList.get(position).getPublishedat());
            holder.publishedat.setText(myDate.toString());
        } catch (ParseException e) {
            e.printStackTrace();
        }
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
user45678
  • 1,504
  • 6
  • 29
  • 58
  • 1
    You've only *parsed* a date here, so what errors do you get when trying to format a new date string? – OneCricketeer May 21 '17 at 16:28
  • Hi, I am getting no error. It just that I want to format my JSON String date "yyyy-MM-dd'T'hh:mm:ss.SSS'Z'" something into "Sat 20th May, 06:00" – user45678 May 21 '17 at 16:30
  • 1
    Okay, so why did you think that `myDate.toString()` would figure out what format you wanted? – OneCricketeer May 21 '17 at 16:32
  • Please be specific about what similar posts you have consulted and why they didn’t work for you. It will help us understand your problem better and it’ll be much easier to guide you in the right direction then, – Ole V.V. May 21 '17 at 19:45
  • @user45678 Search Stack Overflow before posting. Always assume basic questions on common topics have already been asked and answered. – Basil Bourque May 22 '17 at 04:28

2 Answers2

2

This solution skips the old SimpleDateFormat and Date and instead uses the newer classes described in JSR-310. They are generally much more programmer-friendly.

    String formattedDate = Instant.parse(publishedAt)
            .atOffset(ZoneOffset.UTC)
            .format(DateTimeFormatter.ofPattern("EEE dd'th' MMM, hh:mm", Locale.ENGLISH));

This prints

Sat 20th May, 06:00

Please note that we have now lost whether the time was in AM or PM. 06:00 could mean 6 AM or 6 PM. You may want to consider once more whether this was what you wanted. If not, just modify the format pattern string in the code to your needs, and ask if in doubt. I am printing the time in UTC time zone, assuming this was what you intended.

You will also notice that I give no explicit format for parsing the string. This is because your string conforms with ISO 8601, which is what the JSR-310 classes “understand” as their default.

This is all very nice when the date is May 20th. What about the following day? We don’t want 21th May, but 21st. This is where the JSR-310 classes show one of their real advantages: it is straightforward and safe to get the day of month so we can check whether for instance it’s the 1st, the 2nd or the 21st day of a month:

    OffsetDateTime dateTime = Instant.parse(publishedAt).atOffset(ZoneOffset.UTC);
    String numberSuffix = "th";
    if (dateTime.getDayOfMonth() == 1 || dateTime.getDayOfMonth() == 21
            || dateTime.getDayOfMonth() == 31) {
        // print for example 01st or 21st
        numberSuffix = "st";
    } else if (dateTime.getDayOfMonth() == 2 || dateTime.getDayOfMonth() == 22) {
        numberSuffix = "nd";
    } else if (dateTime.getDayOfMonth() == 3 || dateTime.getDayOfMonth() == 23) {
        numberSuffix = "rd";
    }
    String formatPattern = "EEE dd'" + numberSuffix + "' MMM, hh:mm";
    DateTimeFormatter formatter 
            = DateTimeFormatter.ofPattern(formatPattern, Locale.ENGLISH);

With this change we can also have a result of

Sun 21st May, 06:00

To use the JSR-310 classes on Android you need to get the ThreeTenABP. I include a couple of links below. For anyone using Java 8 or later, they are built-in in the java.time package.

Links

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

Please try like this

SimpleDateFormat myFormat = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss.SSS'Z'");
String displayformat="EEE dd MMM, hh:mm";
SimpleDateFormat destFormat=new SimpleDateFormat(displayformat);

        try {
            Date myDate = myFormat.parse(actorList.get(position).getPublishedat());
            holder.publishedat.setText(destFormat.format(myDate));
        } catch (ParseException e) {
            e.printStackTrace();
        }

Hope this help u .if u have any questions u can ask.

Lokesh Desai
  • 2,607
  • 16
  • 28
  • FYI, the troublesome old date-time classes such as `java.util.Date`, `java.util.Calendar`, and `java.text.SimpleTextFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [java.time](https://docs.oracle.com/javase/8/docs/api/java/time/package-summary.html) classes. Much of the java.time functionality is back-ported to Java 6 & 7 in the [ThreeTen-Backport](http://www.threeten.org/threetenbp/) project. Further adapted for Android in the [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP) project. – Basil Bourque May 22 '17 at 04:29
  • 1
    Thank u for inform me i will check this out – Lokesh Desai May 22 '17 at 04:42