-1

I am making a NEWS Android application. All the data I am fetching from NewaApi using JSON parsing. I am also collecting the date information from the API in 'YYYY-MM-DD' format. I want to convert the format into DD-MM-YYYY. Here is my code for the Adapter class.

public class NewsAdapter extends ArrayAdapter<NewsData> {

public NewsAdapter(Context context, List<NewsData> news) {
    super(context, 0, news);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View listItemView = convertView;
    if (listItemView == null) {
        listItemView = LayoutInflater.from(getContext()).inflate(
                R.layout.news_list, parent, false);
    }


    NewsData currentNews = getItem(position);

    TextView headlineView = listItemView.findViewById(R.id.headline);
    headlineView.setText(currentNews.getmHeadline());

    String originalTime = currentNews.getmDate_time();
    String date;


    if (originalTime.contains("T")) {
        String[] parts = originalTime.split("T");
        date = parts[0];
    } else {
        date = getContext().getString(R.string.not_avilalble);
    }


    TextView dateView = listItemView.findViewById(R.id.date);
    dateView.setText(date);

    String imageUri=currentNews.getmImageUrl();
    ImageView newsImage = listItemView.findViewById(R.id.news_image);

    Picasso.with(getContext()).load(imageUri).into(newsImage);


    return listItemView;
}


}

A am also adding the image of how the format looks in JSON.

xingbin
  • 27,410
  • 9
  • 53
  • 103
SIDDHARTH VARSHNEY
  • 521
  • 1
  • 6
  • 17
  • 1
    What is stopping you to parse and then format? There is already many question how to do this ... – Selvin Sep 03 '18 at 10:52

3 Answers3

2

If it's in pattern yyyy-MM-dd, you can parse it as LocalDate;

If it's in pattern yyyy-MM-dd'T'HH:mm:ss'Z', you can parse it to OffsetDateTime, then truncate to LocalDate.

Sample code:

public static String convert(String originalTime) {
    LocalDate localDate;

    if (originalTime.contains("T")) {
        localDate = OffsetDateTime.parse(originalTime).toLocalDate();
    } else {
        localDate = LocalDate.parse(originalTime);
    }

    return localDate.format(DateTimeFormatter.ofPattern("dd-MM-yyyy"));
}

Test case:

public static void main(String args[]) throws Exception {
    System.out.println(convert("2000-11-10"));  // 10-11-2000
    System.out.println(convert("2000-11-10T00:00:01Z")); // 10-11-2000
}
xingbin
  • 27,410
  • 9
  • 53
  • 103
1
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
Date date = sdf.parse(originalTime);
String newDate = new SimpleDateFormat("dd-MM-yyyy").format(date);
Cătălin Florescu
  • 5,012
  • 1
  • 25
  • 36
  • Thanks that worked. – SIDDHARTH VARSHNEY Sep 03 '18 at 11:06
  • 1
    Please don’t teach the young ones to use the long outdated and notoriously troublesome `SimpleDateFormat` class as the first option and without any reservation. Today we have so much better in [`java.time`, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/) and its `DateTimeFormatter`. Yes, you can use it on Android. For older Android see [How to use ThreeTenABP in Android Project](https://stackoverflow.com/questions/38922754/how-to-use-threetenabp-in-android-project). – Ole V.V. Sep 03 '18 at 11:13
  • 2
    Also the `Z` in the string is an offset and must be parsed as such, or you will get incorrect results. So don’t parse it as a literal (in single quotes). – Ole V.V. Sep 03 '18 at 11:16
  • @OleV.V. you are right, what can i say – Cătălin Florescu Sep 03 '18 at 11:37
0

Have this method in utils class and then just call this method where ever u want as Utils.getDate("your date here").

public static String getDate(String ourDate) {
    SimpleDateFormat input = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
    SimpleDateFormat output = new SimpleDateFormat("dd/MM/yyyy");

    Date d = null;
    try {
        d = input.parse("2018-02-02T06:54:57.744Z");
    } catch (ParseException e) {
        e.printStackTrace();
    }
    String formatted = output.format(d);
    Log.i("DATE", "" + formatted);

    return formatted;
}
Faysal Ahmed
  • 7,501
  • 5
  • 28
  • 50
Abhilash
  • 500
  • 4
  • 13
  • 1
    Using these terrible old legacy classes is poor advice in 2018. They were supplanted years ago by the *java.time* classes. – Basil Bourque Sep 03 '18 at 20:14