代码之家  ›  专栏  ›  技术社区  ›  SIDDHARTH VARSHNEY

android项目中的日期格式[重复]

  •  -1
  • SIDDHARTH VARSHNEY  · 技术社区  · 8 年前

    我正在做一个新闻Android应用程序。 我使用JSON解析从NewaApi获取的所有数据。 我想把格式转换成DD-MM-YYYY。

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

    我还添加了JSON格式的图像。

    3 回复  |  直到 8 年前
        1
  •  2
  •   xingbin    8 年前

    yyyy-MM-dd ,可以将其解析为 LocalDate

    如果有规律的话 yyyy-MM-dd'T'HH:mm:ss'Z' ,您可以将其解析为 OffsetDateTime ,然后截断为 .

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

    测试用例:

    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
    }
    
        2
  •  1
  •   Cătălin Florescu    8 年前
    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);
    
        3
  •  0
  •   Faysal Ahmed    8 年前

    把这个方法放在utils类中,然后在需要的地方调用这个方法实用程序.getDate(“你的约会在这里”)。

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