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