Reputation:
Hello stackoverflowers
, Let's say i have a database with a row filled with dates, Okay?
Like this one :
2013-06-01
So i want to translate this date to String
Input:
Tue Mar 01 00:00:00 EET 2013
As an example.. So how can i turn the output of the date like this:
Mar 2013 ?? Here's my codes:
date = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH)
.parse(mCursor.getString(mCursor
.getColumnIndex(KEY_AVAILABILITYDATE)));
list.add(date);
Of course i want to sort the list i added the date to.
So i run this code:
Collections.sort(list);
But frankly it doesn't sort correctly! it's mixed - dates all wrongs.
Thanks for reading, i hope it's easy!
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 loosing some hope i decided to show you all codes:
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 3rd party library : StickyGridHeadersGridView so here's 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);
}
However now the results are all mixed up and lost! the gridview is big and i only see :
in each row. What can i do ? Thanks
Upvotes: 4
Views: 164
Reputation:
So after 1 day of contuniously
working on a fix, i have found out that the problem occured from GridViewStickyHeaders
- external library.
The problem occurs once i scroll! the results all get messed.
Thanks for your contribution! your help really helped me! thanks =)
Upvotes: 1
Reputation: 1506
1) Use "MMM yyyy"
as your regex, like so:
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) Using a comparator with compareTo()
shouldn't be necessary since Collections.sort(list)
should sort a list of Dates chronologically by default.
Most likely, the problem lies somewhere in the code you're not showing but I can't be sure since you're not mocking the database so I can't run your code in Eclipse.
Source: http://docs.oracle.com/javase/tutorial/collections/interfaces/order.html
Upvotes: 0
Reputation: 2230
you need to build a comparator to compare two dates:
here is a code for it:
public static Comparator<Date> dateComparator = new Comparator<Date>()
{
public int compare(Date date1, Date date2)
{
return date1.compareTo(date2);
}
};
and you can call it using this line of code:
Collections.sort(list,dateComparator);
and this is how to use arrays instead of collections
Date[] arrayDates = new Date[list.size()];
arrayDates = list.toArray(arrayDates);
Arrays.sort(arrayDates, dateComparator);
Upvotes: 2