Android 中使用 DateFormat 的 java.lang.IllegalArgumentException

标签 java android simpledateformat

这是我的代码 fragment

这里的日期格式为 10-Sep-2013 09:53:37

TextView tvDate = (TextView) convertView.findViewById(R.id.entered_date);
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
tvDate.setText(dateFormat.format(salesReportItems.getDate().toString()));
TextView tvCardType = (TextView) convertView.findViewById(R.id.card_type);
tvCardType.setText(salesReportItems.getCardType().toString());

请帮我解决这个问题。这是我的错误。 enter image description here

亲爱的Piyush,

这是我使用你的代码时的输出

enter image description here

最佳答案

创建如下所示的方法

private String formatDate(String dateString) {
        try {
            SimpleDateFormat sd = new SimpleDateFormat("dd-MMM-yyyy hh:mm:ss" /* 10-Sep-2013 09:53:37*/);
            Date d = sd.parse(dateString);
            sd = new SimpleDateFormat("yyyy-MM-dd");
            return sd.format(d);
        } catch (ParseException e) {
        }
        return "";
    }

然后将其称为

tvDate.setText(formatDate(salesReportItems.getDate().toString()));
<小时/>

了解更多关于How can I change the date format in Java?的信息

关于Android 中使用 DateFormat 的 java.lang.IllegalArgumentException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18733306/

相关文章:

java - SimpleDateFormat 导致时间不正确

java - 使用 ShareActionProvider 共享内容时检测成功/失败

java - 我的 GUI 表现得很奇怪

android - 定期工作请求未在工作管理器中执行 doWork

java - SimpleDateFormat 错误地解析日期字符串

java - 使用 SimpleDateFormat 解析自定义时间格式

java - 如何在 Maven 中配置 Checkstyle?

java - 在java中,我期望返回必须捕获的值的方法调用不会抛出编译错误。为什么?

java - 计数功能不完整

android - CertPathValidatorException 连接到 Android M 或更早版本上的 Let's Encrypt 主机