ArrayList 上的 JAVA SimpleDateFormat 更改日期

标签 java android arraylist adapter

我正在使用适配器用“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);

for start - 01/01 10:30, for end - 11:30

正如 iv'e 之前提到的,即使日期和时间不同,所有对象都会获取此日期。

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

最佳答案

See java.text.SimpleDateFormat API, pattern letter y: For parsing, if the number of pattern letters is more than 2, the year is interpreted literally, regardless of the number of digits.

关于ArrayList 上的 JAVA SimpleDateFormat 更改日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44924660/

相关文章:

java - 自定义对象的数组列表

java - 在重复字符之间拆分字符串

java - 如何迭代 HashMap 列表并将 HashMap 映射到模型

java - 使用功能分支进行 Gradle 依赖管理

java - RxJava/安卓 : Combine result of two dependent Observables

java - java中Hashmap和ArrayList添加值的方式不同

java - 将列表拆分为子列表并将它们一一传递到算法中

java - 在Java中将base64字符串转换为图像

android - 水平 ScrollView 的scrollTo()不起作用

android - 工具栏在 RecyclerView 滚动条上留下空白并隐藏