Java – correctly formatted date

Hello, stack overflows, let me say I have a database with a row of dates, okay?

Question 1:

Like this:

June 1, 2013

So I want to translate this date into string

Input:

Tue Mar 01 00:00:00 EET 2013

For example... How can I turn the date output like this

March 2013?? This is my code:

date = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH)
                            .parse(mCursor.getString(mCursor
                                    .getColumnIndex(KEY_AVAILABILITYDATE)));
                    list.add(date);

Question 2 (question 1 must be solved first) I guess:

Of course, I want to sort the list of dates I added

So I run this Code:

Collections.sort(list);

But frankly, it's not sorted correctly! It's mixed - dating all errors

Thanks for reading, I hope it's easy!

All codes:

public ArrayList<Date> GetValues() {
ArrayList<Date> list = new ArrayList<Date>();
        Date date;
if (mCursor.moveToFirst()) {
            while (mCursor.isAfterLast() == false) {
                try {
                    date = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH)
                            .parse(mCursor.getString(mCursor
                                    .getColumnIndex(KEY_AVAILABILITYDATE)));
                    list.add(date);

                } catch (ParseException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                mCursor.moveToNext();
            }

        }

        Collections.sort(list);

        return list;

    }

After losing some hope, I decided to show you all the code:

code:

    Database DbHelper = new Database(
                    this.getSherlockActivity()).open();
            ArrayList<Date> headers = DbHelper.GetValues();

            HashSet<Date> hs = new HashSet<Date>();
            hs.addAll(headers);
            headers.clear();
            headers.addAll(hs);
                    Collections.sort(header,dateComparator);
            Collections.reverse(headers);

I'm using a third-party library: stickygridheadersgridview, so this is my adapter:

@Override
public int getCountForHeader(int header) {
    // TODO Auto-generated method stub
    return 1;
}

    @Override
    public int getNumHeaders() {
        // TODO Auto-generated method stub
        return headers.size();
    }

    @Override
public View getHeaderView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    if (convertView == null) {
        convertView = inflater.inflate(R.layout.header, parent, false);
        TextView tvheader = (TextView) convertView
                .findViewById(R.id.tvheader);
        String headertitle = headers.get(position).toString();
        if (headertitle.contains("01 00:00:00 EET")) {
            headertitle = headertitle.replace("01 00:00:00 EET", "");
        }
        if (headertitle.contains("01 00:00:00 EEST")) {
            headertitle = headertitle.replace("01 00:00:00 EEST", "");
        }
        if (headertitle.contains("02 00:00:00 EET")) {
            headertitle = headertitle.replace("02 00:00:00 EET", "");
        }
        if (headertitle.contains("02 00:00:00 EEST")) {
            headertitle = headertitle.replace("02 00:00:00 EEST", "");
        }
        if (headertitle.contains("15 00:00:00 EET")) {
            headertitle = headertitle.replace("15 00:00:00 EET", "");
        }
        // days
        if (headertitle.contains("Mon")) {
            headertitle = headertitle.replace("Mon", "");
        }
        if (headertitle.contains("Tue")) {
            headertitle = headertitle.replace("Tue", "");
        }
        if (headertitle.contains("Wed")) {
            headertitle = headertitle.replace("Wed", "");
        }
        if (headertitle.contains("Thu")) {
            headertitle = headertitle.replace("Thu", "");
        }
        if (headertitle.contains("Fri")) {
            headertitle = headertitle.replace("Fri", "");
        }
        if (headertitle.contains("Sat")) {
            headertitle = headertitle.replace("Sat", "");
        }
        if (headertitle.contains("Sun")) {
            headertitle = headertitle.replace("Sun", "");
        }

        // months
        if (headertitle.contains("Jan")) {
            headertitle = headertitle.replace("Jan", "January");
        }
        if (headertitle.contains("Feb")) {
            headertitle = headertitle.replace("Feb", "February");
        }
        if (headertitle.contains("Mar")) {
            headertitle = headertitle.replace("Mar", "March");
        }
        if (headertitle.contains("Apr")) {
            headertitle = headertitle.replace("Apr", "April");
        }
        if (headertitle.contains("Jun")) {
            headertitle = headertitle.replace("Jun", "June");
        }
        if (headertitle.contains("Jul")) {
            headertitle = headertitle.replace("Jul", "July");
        }
        if (headertitle.contains("Aug")) {
            headertitle = headertitle.replace("Aug", "August");
        }
        if (headertitle.contains("Sep")) {
            headertitle = headertitle.replace("Sep", "September");
        }
        if (headertitle.contains("Oct")) {
            headertitle = headertitle.replace("Oct", "October");
        }
        if (headertitle.contains("Nov")) {
            headertitle = headertitle.replace("Nov", "November");
        }
        if (headertitle.contains("Dec")) {
            headertitle = headertitle.replace("Dec", "December");
        }
        tvheader.setText(headertitle);

    }

But now the results are confused! GridView is very big. I only see:

September 2013 June 2013 July 2013

In every line. What can I do? thank you

resolvent:

You need to create a comparator to compare two dates:

This is a code:

public static Comparator<Date> dateComparator = new Comparator<Date>()
{
    public int compare(Date date1, Date date2)
    {
        return date1.compareTo(date2);
    }
};

You can call it with this line of code:

Collections.sort(list,dateComparator);

This is how to use arrays instead of collections

Date[] arrayDates = new Date[list.size()];
arrayDates = list.toArray(arrayDates);
Arrays.sort(arrayDates, dateComparator);

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>