-4

I am getting the json response as "publishedAt": "2017-09-12T01:03:08Z" and i want to format this response in simple date likeAug 12-09-2017 3:08and set it into TextView i am using something like this

Date dateObj = new Date(currentNews.getTimeInString());

TextView dateAndTimeView = (TextView) listItemView.findViewById(R.id.date_and_time);
        String formattedDateAndTime = formatDateAndTime(dateObj);
        dateView.setText(formattedDate);

private String formatDateAndTime(Date dateObj){
        SimpleDateFormat dateFormat = new SimpleDateFormat("dd MMM, yyyy");
        return dateFormat.format(dateObj);
    }
  • 1
    what you tried so far ?? – IntelliJ Amiya Sep 12 '17 at 05:59
  • Please share your attempt in code. – codyogden Sep 12 '17 at 06:03
  • Even if on Android you’re tempted to use the long outdated classes `Date` and `SimpleDateFormat` (because nothing better comes built-in), I would certainly warn against using the deprecated `Date(String)` constructor. Still better, stop using those classes and start using [the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). To use it on Android you will need **ThreeTenABP**, see [this question](https://stackoverflow.com/questions/38922754/how-to-use-threetenabp-in-android-project). – Ole V.V. Sep 12 '17 at 08:42
  • Thanks for the code. And welcome to Stack Overflow, by the way. You should also spell out how your code’s behaviour differs from what you want. If you see any error message and/or stacktrace, please quote them verbatim. – Ole V.V. Sep 12 '17 at 09:00
  • “Questions seeking debugging help (*"why isn't this code working?"*) must include … a specific problem or error …. Questions without a clear problem statement are not useful to other readers." Quoted from [What topics can I ask about here?](https://stackoverflow.com/help/on-topic) – Ole V.V. Sep 12 '17 at 09:02

4 Answers4

1

Did you tried SimpleDateFormat? https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html

Something like (pseudocode, can`t test it here)

String givenDate = "2017-09-12T01:03:08Z";
SimpleDateFormat sdf= new SimpleDateFormat("MM-dd-yyyy HH:mm:ss");
try {
    Date date = sdf.parse(givenDate );
    System.out.println(date);
} catch (ParseException e) {
    e.printStackTrace();
}
jhen
  • 1,164
  • 1
  • 11
  • 24
1

Please use this method:

public String getDateFromUTC(String ourDate)
    {
        try
        {
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
            formatter.setTimeZone(TimeZone.getTimeZone("UTC"));
            Date value = formatter.parse(ourDate);

            SimpleDateFormat dateFormatter = new SimpleDateFormat("dd-MM-yyyy hh:mm aa"); //this format changeable
            dateFormatter.setTimeZone(TimeZone.getDefault());
            ourDate = dateFormatter.format(value);

            //Log.d("OurDate", OurDate);
        }
        catch (Exception e)
        {
            ourDate = "00-00-0000 00:00";
        }
        return ourDate;
    }
AskNilesh
  • 67,701
  • 16
  • 123
  • 163
Vicky Kumar
  • 154
  • 6
1

TL;DR

    String formattedDateAndTime = Instant.parse("2017-09-12T01:03:08Z")
            .atZone(ZoneId.of("America/Nome"))
            .format(DateTimeFormatter.ofPattern("EEE dd-MM-uuuu H:mm", Locale.ENGLISH));

Time zone is crucial

As the code stands, it produces Mon 11-09-2017 17:03. However, please substitute the correct region and city for your desired time zone, and this will be reflected in the output. If you want to use the JVM’s time zone setting, use ZoneId.systemDefault(); however, be warned that other code running in the JVM may change this setting outside of your control, so it’s not perfectly reliable.

ThreeTenABP

I have taken my own medicine from my comment: thrown away Date and SimpleDateFormat. So the above code requires ThreeTenABP. All the details are in this question: How to use ThreeTenABP in Android Project.

ISO 8601

I am further exploiting the fact that the string from you JSON response is in ISO 8601 format, which the modern date and time classes “understand” as their default. Had it been in some other format, a separate DateTimeFormatter would have been needed for parsing it; but not here (with the outdated classes, two different SimpleDateFormats would be needed).

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

Try this code .

SimpleDateFormat formatter = new SimpleDateFormat(" EEE dd-MM-yyyy HH:mm");
KeLiuyue
  • 8,149
  • 4
  • 25
  • 42
Shaifali Rajput
  • 1,279
  • 12
  • 30