正确格式化日期

时间:2013-09-13 20:59:32

标签: java android string date

您好 stackoverflowers ,假设我的数据库中有一行填充了日期,好吗?

问题1:

喜欢这个:

2013年6月1日

所以我想把这个日期翻译成String

输入:

Tue Mar 01 00:00:00 EET 2013

作为一个例子..  那么我怎么能像这样转动日期的输出:

2013年3月? 这是我的代码:

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

问题2(问题1必须先解决,我猜):

当然我想对我添加日期的列表进行排序。

所以我运行这段代码:

Collections.sort(list);

但坦率地说它没有正确排序!它是混合的 - 约会所有错误。

感谢阅读,我希望这很简单!

所有代码:

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;

    }

在失去一些希望后,我决定向你展示所有代码:

代码:

    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);

我正在使用第三方库:StickyGridHeadersGridView所以这是我的适配器:

@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);

    }

然而现在结果都混淆了! gridview很大,我只看到:

2013年9月2013年6月2013年7月2013

每行

。我能做什么 ?感谢

3 个答案:

答案 0 :(得分:2)

你需要建立一个比较器来比较两个日期:

这是一个代码:

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

你可以用这行代码来调用它:

Collections.sort(list,dateComparator);

这就是如何使用数组而不是集合

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

答案 1 :(得分:1)

因此,在contuniously处理修复后的1天后,我发现问题发生在GridViewStickyHeaders - 外部库。

滚动后问题就出现了!结果都搞砸了。

感谢您的贡献!你的帮助真的对我有帮助!谢谢=)

答案 2 :(得分:0)

1)使用"MMM yyyy"作为正则表达式,如下所示:

public class Main {
  public static void main(String args[]) {
    Date now = new Date();
    SimpleDateFormat format = new SimpleDateFormat("MMM yyyy", Locale.ENGLISH);
    System.out.println(format.format(now));
  }
}

> Sep 2013

2)不需要使用带compareTo()的比较器,因为Collections.sort(list)默认情况下应按时间顺序对日期列表进行排序。

最有可能的是,问题出现在你没有展示的代码中,但我不能确定,因为你没有嘲笑数据库所以我无法在Eclipse中运行你的代码。

来源:http://docs.oracle.com/javase/tutorial/collections/interfaces/order.html

相关问题