GridView for calendar view

Parts of my old application are out of date, I'm trying to rebuild it. This is mainly a calendar with monthly viewing. This is part of my gridview adapter:

public View getView(int position, View view, ViewGroup parent) { Date date = getItem(position); int day = date.getDate(); int month = date.getMonth(); int year = date.getYear(); } 

and int day = date.getDate(); int month = date.getMonth(); int year = date.getYear(); int day = date.getDate(); int month = date.getMonth(); int year = date.getYear(); outdated. I am trying to use the Calendar class instead of Date , but I cannot do the same. I know that to extract the day, month or year I can use this:

 Calendar calendar = Calendar.getInstance(); calendar.get(Calendar.DAY_OF_MONTH); 

but I do not know how to convert this string:

 Date date = getItem(position); 

for use with Calendar .

+7
java android android-gridview calendarview
source share
5 answers

You can use this line of code:

Just replace this line

 Date date = getItem(position); 

with this line:

 Calendar calendar = Calendar.getInstance(); Date date = calendar.getTime(); 

Here is a complete example:

 Calendar calendar = Calendar.getInstance(); Date date = calendar.getTime(); int day = calendar.get(Calendar.DAY_OF_MONTH); int month = calendar.get(Calendar.MONTH); int year = calendar.get(Calendar.YEAR); 
+2
source share

Here's how you convert a Date object to a Calendar object:

 Calendar cal = Calendar.getInstance(); cal.setTime(date); 

Then (as you said) you can do:

 int day = cal.get(Calendar.DAY_OF_MONTH); int month = cal.get(Calendar.MONTH) int year = cal.get(Calendar.YEAR); 
+1
source share

First you will want to refer to the Calendar. Once you do, you can say Date date = calendar.getTime

 public View getView(int position, View view, ViewGroup parent) { Calendar calendar = Calendar.getInstance(); Date date = calendar.getTime(); int day = calendar.get(Calendar.DAY_OF_MONTH); int month = calendar.get(Calendar.MONTH) int year = calendar.get(Calendar.YEAR); } 
+1
source share

Look for a response drawing from reliable and / or official sources.

OK

To the main sources:

Date not out of date. Only some methods.

So,

 public View getView(int position, View view, ViewGroup parent) { Date date = getItem(position); long ms = date.getTime();https://docs.oracle.com/javase/7/docs/api/java/util/Date.html#getTime() Calendar calendar = Calendar.getInstance();//allowed calendar.setTimeInMillis(ms);//allowed https://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html#setTimeInMillis(long) //calendar.setTime(date); is also allowed https://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html#setTime(java.util.Date) int day = calendar.get(Calendar.DAY_OF_MONTH);//allowed int month = calendar.get(Calendar.MONTH);//allowed int year = calendar.get(Calendar.YEAR);//allowed } 
+1
source share

Here is sample code for converting from Date to Calendar .

 public View getView(int position, View view, ViewGroup parent) { Date date = getItem(position); // convert a Date object to a Calendar object Calendar calendar = Calendar.getInstance(); calendar.setTime(date); int day = calendar.get(Calendar.DAY_OF_MONTH); int month = calendar.get(Calendar.MONTH); int year = calendar.get(Calendar.YEAR); } 
0
source share

All Articles