【问题标题】:JAVA SimpleDateFormat on ArrayList changes DateArrayList 上的 JAVA SimpleDateFormat 更改日期
【发布时间】:2017-07-05 11:11:59
【问题描述】:

我正在使用适配器使用“ArrayList”填充“ListView”。 'ArrayList' 包含带有日期和时间的对象。 当我将数据按原样发送到 ListView 时,没有问题,列表根据需要显示相关对象:

public View getView(int position, View convertView, ViewGroup parent) {

  TimePeriod freeSlot = (TimePeriod)getItem(position);

  if (convertView == null) {
      convertView = LayoutInflater.from(getContext()).inflate(R.layout.item, parent, false);
    }

    TextView start = (TextView) convertView.findViewById(R.id.start);
    TextView end = (TextView) convertView.findViewById(R.id.end);

    startTime = freeSlot.getStart().toString()      
    endTime = freeSlot.getEnd().toString();
    start.setText(startTime);
    end.setText(endTime);

    return convertView;
}

2017-07-05T10:30:00.000Z 2017-07-05T11:30:00.000Z

但是当我使用 SimpleDateFormat 根据需要更改输出时,它会以某种方式将所有对象的日期设置为相同的日期(01/01):

startTime = freeSlot.getStart().toString();
endTime = freeSlot.getEnd().toString();

try {
            SimpleDateFormat formatter = new SimpleDateFormat("YYYY-MM-dd'T'hh:mm:ss.SSS'Z'");
            Date newDate = formatter.parse(startTime);
            Date newDate2 = formatter.parse(endTime);

            SimpleDateFormat format = new SimpleDateFormat("dd/MM hh:mm");
            startTime = format.format(newDate);
            SimpleDateFormat format1 = new SimpleDateFormat("hh:mm");
            endTime = format1.format(newDate2);

        } catch (ParseException pe) {
            pe.printStackTrace();
        }

        start.setText(startTime);
        end.setText(endTime);

开始 - 01/01 10:30,结束 - 11:30

正如前面提到的,所有对象都会得到这个日期,即使日期和时间不同。

有什么想法吗?我究竟做错了什么? 非常感谢!

【问题讨论】:

  • 您的日期包含 12 或 24 小时格式?

标签: java android arraylist adapter


【解决方案1】:

参见 java.text.SimpleDateFormat API,模式字母 y:用于解析,如果模式字母的个数超过 2 个,则按字面解释年份,而不考虑位数。

【讨论】:

  • 非常感谢!我所做的只是将'YYYY'更改为'yyyy'并且它有效!这么简单……
【解决方案2】:

使用这样的东西来让它变得简单, 使用以下方法创建一个 util 类:

 /**
     * Parses @date to String in provided @format
     *
     * @param date   Date
     * @param format Convert to Format
     * @return formatted date string
     */
    public static String DateToString(Date date, String format) {
        if (date != null) {
            try {
                DateFormat dateFormat = new SimpleDateFormat(format);
                return dateFormat.format(date);
            } catch (Exception e) {
                e.printStackTrace();
                return "";
            }
        } else {
            return "";
        }
    }

    /**
     * Parses date to String in provided format
     *
     * @param date   Date
     * @param format Convert to Format
     * @return formatted date string
     */
    public static String DateToString(Date date, Format format) {
        if (date != null) {
            try {
                DateFormat dateFormat = new SimpleDateFormat(format.format);
                return dateFormat.format(date);
            } catch (Exception e) {
                e.printStackTrace();
                return "";
            }
        } else {
            return "";
        }
    }

为可重用性创建静态枚举/字符串格式。如下:

 public static enum Format {

        /**
         * "EEE, MMM dd, yyyy, hh:mm a"
         */
        FORMAT1("EEE, MMM dd, yyyy, hh:mm a"),
        /**
         * "dd/MM/yyyy"
         */
        FORMAT2("dd/MM/yyyy"),
        /**
         * "dd/MM/yyyy hh:mm a"
         */
        FORMAT3("dd/MM/yyyy hh:mm a"),
        /**
         * "dd/MM/yyyy hh:mm:ss a"
         */
        FORMAT4("dd/MM/yyyy hh:mm:ss a"),
        /**
         * "dd/MM/yyyy HH:mm"
         */
        FORMAT5("dd/MM/yyyy HH:mm"),
        /**
         * "dd/MM/yyyy HH:mm:ss"
         */
        FORMAT6("dd/MM/yyyy HH:mm:ss"),
        /**
         * "dd-MM-yyyy"
         */
        FORMAT7("dd-MM-yyyy"),
        /**
         * "dd-MM-yyyy hh:mm a"
         */
        FORMAT8("dd-MM-yyyy hh:mm a"),
        /**
         * "dd-MM-yyyy hh:mm:ss a"
         */
        FORMAT9("dd-MM-yyyy hh:mm:ss a"),
        /**
         * "dd-MM-yyyy HH:mm"
         */
        FORMAT10("dd-MM-yyyy HH:mm"),
        /**
         * "dd-MM-yyyy HH:mm:ss"
         */
        FORMAT11("dd-MM-yyyy HH:mm:ss"),
        /**
         * "MM/dd/yyyy"
         */
        FORMAT12("MM/dd/yyyy"),
        /**
         * "MM/dd/yyyy hh:mm a"
         */
        FORMAT13("MM/dd/yyyy hh:mm a"),
        /**
         * "MM/dd/yyyy hh:mm:ss a"
         */
        FORMAT14("MM/dd/yyyy hh:mm:ss a"),
        /**
         * "MM/dd/yyyy HH:mm"
         */
        FORMAT15("MM/dd/yyyy HH:mm"),
        /**
         * "MM/dd/yyyy HH:mm:ss"
         */
        FORMAT16("MM/dd/yyyy HH:mm:ss"),
        /**
         * "MM-dd-yyyy"
         */
        FORMAT17("MM-dd-yyyy"),
        /**
         * "MM-dd-yyyy hh:mm a"
         */
        FORMAT18("MM-dd-yyyy hh:mm a"),
        /**
         * "MM-dd-yyyy hh:mm:ss a"
         */
        FORMAT19("MM-dd-yyyy hh:mm:ss a"),
        /**
         * "MM-dd-yyyy HH:mm"
         */
        FORMAT20("MM-dd-yyyy HH:mm"),
        /**
         * "MM-dd-yyyy HH:mm:ss"
         */
        FORMAT21("MM-dd-yyyy HH:mm:ss"),
        /**
         * "yyyy/MM/dd"
         */
        FORMAT22("yyyy/MM/dd"),
        /**
         * "yyyy/MM/dd hh:mm a"
         */
        FORMAT23("yyyy/MM/dd hh:mm a"),
        /**
         * "yyyy/MM/dd hh:mm:ss a"
         */
        FORMAT24("yyyy/MM/dd hh:mm:ss a"),
        /**
         * "yyyy/MM/dd HH:mm"
         */
        FORMAT25("yyyy/MM/dd HH:mm"),
        /**
         * "yyyy/MM/dd HH:mm:ss"
         */
        FORMAT26("yyyy/MM/dd HH:mm:ss"),
        /**
         * "yyyy-MM-dd"
         */
        FORMAT27("yyyy-MM-dd"),
        /**
         * "yyyy-MM-dd hh:mm a"
         */
        FORMAT28("yyyy-MM-dd hh:mm a"),
        /**
         * "yyyy-MM-dd hh:mm:ss a"
         */
        FORMAT29("yyyy-MM-dd hh:mm:ss a"),
        /**
         * "yyyy-MM-dd HH:mm"
         */
        FORMAT30("yyyy-MM-dd HH:mm"),
        /**
         * "yyyy-MM-dd HH:mm:ss"
         */
        FORMAT31("yyyy-MM-dd HH:mm:ss");

        private String format;

        private Format(String format) {
            this.format = format;
        }

        @Override
        public String toString() {
            return format;
        }

    }

【讨论】:

  • 您需要使用 HH:mm 或 hh:mm 这是 24 小时制和 12 小时制的区别。
  • 您需要使用 HH:mm 或 hh:mm 这是 24 小时制和 12 小时制的区别。
【解决方案3】:
SimpleDateFormat formatter = new SimpleDateFormat("YYYY-MM-dd'T'hh:mm:ss.SSS'Z'");

改成:

SimpleDateFormat formatter = new SimpleDateFormat("YYYY-MM-dd'T'HH:mm:ss.SSS'Z'");

你需要

您需要使用 HH:mm 或 hh:mm 这是 24 小时制和 12 小时制格式的区别。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-06
    • 1970-01-01
    • 2013-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多