【问题标题】:Date Formatting in android project [duplicate]android项目中的日期格式[重复]
【发布时间】:2018-09-03 10:46:25
【问题描述】:

我正在制作一个 NEWS Android 应用程序。 我使用 JSON 解析从 NewaApi 获取的所有数据。 我还以“YYYY-MM-DD”格式从 API 收集日期信息。 我想将格式转换为 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 格式的图像。

【问题讨论】:

  • 是什么阻止您解析然后格式化?如何做到这一点已经有很多问题......

标签: java android date datetime java-time


【解决方案1】:

如果是yyyy-MM-dd,则可以解析为LocalDate

如果它在模式yyyy-MM-dd'T'HH:mm:ss'Z' 中,您可以将其解析为OffsetDateTime,然后截断为LocalDate

示例代码:

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】:
    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);
    

    【讨论】:

    • 感谢工作。
    • 请不要毫无保留地教导年轻人将早已过时且臭名昭著的SimpleDateFormat课程作为第一选择。今天我们在java.time, the modern Java date and time API 和它的DateTimeFormatter 中有很多更好的东西。是的,您可以在 Android 上使用它。对于较旧的 Android,请参阅 How to use ThreeTenABP in Android Project
    • 另外,字符串中的Z 是一个偏移量,必须这样解析,否则会得到不正确的结果。所以不要将其解析为文字(单引号)。
    • @OleV.V.你是对的,我能说什么
    【解决方案3】:

    在 utils 类中有这个方法,然后在任何你想要的地方调用这个方法作为 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;
    }
    

    【讨论】:

    • 在 2018 年使用这些糟糕的旧遗留类是一个糟糕的建议。它们在几年前被 java.time 类所取代。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多