【问题标题】:Formatting date correctly正确格式化日期
【发布时间】:2013-09-18 15:54:02
【问题描述】:

您好stackoverflowers,假设我有一个数据库,其中一行充满日期,好吗?

问题一:

喜欢这个:

2013-06-01

所以我想把这个日期翻译成字符串

输入:

2013 年 3 月 1 日星期二 00:00:00 EET

举个例子.. 那么我怎样才能像这样输出日期:

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

我正在使用第 3 方库: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);

    }

然而现在结果都混为一谈了!网格视图很大,我只看到:

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

在每一行。我能做些什么 ?谢谢

【问题讨论】:

    标签: java android string date


    【解决方案1】:

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

    这是它的代码:

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

    【讨论】:

    • 集合会为您执行此操作,并且您的列表将被排序。只需填写列表并调用最后一行。
    • 虽然代码不同,但我得到了不同的输出,但根本没有正确排序
    • 如果您使用正确的比较语句,比较器不会给您错误的结果。另一种方法是使用数组而不是集合,只是为了确保
    • 我在哪里添加该代码? Collections.sort(list,dateComparator);在循环内部还是外部?
    • 在循环之外,因为您正在传递参考,所以它当然会重新排列您的列表。
    【解决方案2】:

    所以在contuniously 进行了 1 天的修复工作后,我发现问题出在 GridViewStickyHeaders - 外部库。

    一旦我滚动就会出现问题!结果都搞砸了。

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

    【讨论】:

      【解决方案3】:

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

      &gt; Sep 2013

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

      很可能,问题出在您未显示的代码中,但我无法确定,因为您没有模拟数据库,因此我无法在 Eclipse 中运行您的代码。

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

      【讨论】:

        猜你喜欢
        • 2012-02-15
        • 2017-04-10
        • 2021-06-05
        • 1970-01-01
        • 2020-01-07
        • 1970-01-01
        • 2018-11-18
        • 2021-12-20
        相关资源
        最近更新 更多