有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

ArrayList上的安卓 JAVA SimpleDateFormat更改日期

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

但是,当我使用SimpleDataFormat根据需要更改输出时,它会以某种方式将所有对象的日期设置为相同的日期(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

正如前文提到的,所有对象都有这个日期,即使日期&;时间不同

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


共 (2) 个答案

  1. # 1 楼答案

    用这样的方法让事情变得简单, 使用以下方法创建一个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;
            }
    
        }
    
  2. # 2 楼答案

    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.