Android SimpleDateFormat Date changes on new day

165 views Asked by At

I have 10 new entry in my listview added dynamically from an edittext with a todays date 12-13-2013 i cant figure out why tomorrow all the dates will read 12-14-2013 i want each entry to keep the date it was entered on and not change any ideas?

class ViewHolder {
    TextView gump;
    ImageView pic;
    TextView day;
    ImageView more;

    ViewHolder(View v) {
        gump = (TextView) v.findViewById(R.id.inputLrgtxt);
        pic = (ImageView)v.findViewById(R.id.sexpic);
        day = (TextView)v.findViewById(R.id.holla);
    }
}

private class KoreyzAdapter extends ArrayAdapter<ArgueItem> {
    private ArrayList<ArgueItem> argueList;
    private Context context;

    public KoreyzAdapter(Context c, int textViewResourceId, ArrayList<ArgueItem> argueList) {
        super(c, textViewResourceId, argueList);
        this.argueList = argueList;
        this.context = c;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        ViewHolder holder = null;
        long msTime = System.currentTimeMillis();
        Date date = new Date(msTime);
        SimpleDateFormat sdf = new SimpleDateFormat("M/d/y");
        String biggie = sdf.format(date);

        if(row == null) {
            LayoutInflater inflator = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = inflator.inflate(R.layout.list_item, parent, false);
            holder = new ViewHolder(row);
            row.setTag(holder);
        }
        else {
            holder = (ViewHolder)row.getTag();
        }

        holder.gump.setText(argueList.get(position));
        holder.day.setText(biggie);

        return row;
    }
}
1

There are 1 answers

2
EJK On

Your code:

        long msTime = System.currentTimeMillis();
        Date date = new Date(msTime);
        SimpleDateFormat sdf = new SimpleDateFormat("M/d/y");
        String biggie = sdf.format(date);

is called when the list view is rendered. Thus you are creating a new date on the fly that reflects the current date (i.e. the date at the time of rendering).

If you want a single date that never changes, calculate it once and persist it (SharedPreferences would be a good option). Then in future calls, read the date string from SharedPreferences.