gpt4 book ai didi

java - 正确格式化日期

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:02:30 25 4
gpt4 key购买 nike

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

问题一:

喜欢这个:

2013-06-01

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

输入:

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

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

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

}

然而现在结果都乱七八糟了! gridview 很大,我只看到:

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

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

最佳答案

您需要构建一个比较器来比较两个日期:

这是它的代码:

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

关于java - 正确格式化日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18795144/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com