日期格式转换错误

时间:2014-05-07 08:15:16

标签: android date

我想将日期转换为" 2014-04-09 00:00:00"到" 09-apr"但是我正在接近一支部队。 我在这部分收到错误[tvStartDate.setText(sdf.format(event.startDate +" \ n"));]

班级

package com.megasys.roushfunway.adapter;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import com.megasys.roushfunway.R;
import com.megasys.roushfunway.database.Event;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class EventListAdapter extends BaseAdapter {
    Context context;
    List<Event> eventList;
    String format = "dd-MMM";
     SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.US);

    public EventListAdapter(Context context, List<Event> eventList) {
        this.context = context;
        this.eventList = eventList;

    }

    @Override
    public int getCount() {
        return eventList.size();
    }

    @Override
    public Object getItem(int position) {
        return eventList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {

        Event event = eventList.get(position);

        if (view == null)
            view = LayoutInflater.from(context).inflate(
                    R.layout.view_event_list, parent, false);

        TextView tvStartDate = (TextView) view.findViewById(R.id.tv_start_date);
        TextView tvEventId = (TextView) view.findViewById(R.id.tv_event_id);
        TextView tvLocation = (TextView) view.findViewById(R.id.tv_location);

        tvStartDate.setText(sdf.format(event.startDate + "\n"));
        tvEventId.setText(event.eventId);
        tvLocation.setText(event.venue);

        return view;
    }

}

我想将日期转换为&#34; 2014-04-09 00:00:00&#34;到&#34; 09-apr&#34;但是我正在接近一支部队。 我在这部分收到错误[tvStartDate.setText(sdf.format(event.startDate +&#34; \ n&#34;));]

1 个答案:

答案 0 :(得分:1)

tvStartDate.setText(sdf.format(event.startDate + "\n"));

您无法格式化String。它将使用不受支持的format(Object)重载,而不是您需要的format(Date)

如果event.startDateDate,请移动paren:

tvStartDate.setText(sdf.format(event.startDate) + "\n");

如果是日期时间字符串,请先使用其他SimpleDateFormat将其解析为Date,然后格式化已解析的Date