Correct format date

Hello stackoverflowers . Say I have a database with a string filled with dates. Good?

Problem 1:

Like this:

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 get the date output as follows:

Mar 2013. Here are my codes:

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

Task 2 (the first problem should be solved):

Of course, I want to sort the list into which I added the date.

So, I run this code:

 Collections.sort(list); 

But honestly, this is not right! he mixes up - indicates all errors.

Thanks for reading, I hope this is 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 hope, I decided to show you all the codes:

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 use a third-party library: StickyGridHeadersGridView , so here 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); } 

However, now the results are all mixed up and lost! gridview is big and I only see:

September 2013 June 2013 July 2013

in each row. What can I do? Thanks

+4
source share
3 answers

you need to build a comparator to compare two dates:

here is the 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 how to use arrays instead of collections

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

So, after 1 day of contuniously working on the fix, I found that the problem came from GridViewStickyHeaders - an external library.

The problem occurs after scrolling! the results are all confused.

Thanks for your input! your help really helped me! thanks =)

+1
source

1) Use "MMM yyyy" as your regular expression, for example:

 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() not necessary since Collections.sort(list) defaults to sorting the default date list.

Most likely, the problem lies somewhere in the code that you are not showing, but I cannot be sure, since you are not mocking the database, so I cannot run your code in Eclipse.

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

0
source

Source: https://habr.com/ru/post/1502118/


All Articles