Readdy Write  
0,00 €
Your View Money
Views: Count
Self 20% 0
Your Content 60% 0

Users by Links 0
u1*(Content+Views) 10% 0
Follow-Follower 0
s2*(Income) 5% 0

Count
Followers 0
Login Register as User

Android Java: compare date day

21.09.2018 (👁152)


 

How to compare two date date values against each other on the same date in Android Java.

Or a comparison on the day or hour or minute.

 

Solution:

You create two date variables of type Calendar.

Then set the hour, minute, second and millisecond of the variable to 0.

Then you have the same daily value to the hour 0.

These can be compared with Datum.equals (Datum2).

public void create_Table(){
   
//-------< create_Table() >--------
    //< current Date >
   
Calendar today=Calendar.getInstance();
   
//current_CalendarDay.setTime(today);
   
today.set(Calendar.HOUR_OF_DAY, 0);
    today.set(Calendar.
MINUTE, 0);
    today.set(Calendar.
SECOND, 0);
    today.set(Calendar.
MILLISECOND, 0);
    Date dtToday=today.getTime();
   
//</ current Date >

    //----< Day-Row >----
    //-< Date >-
   
Date dtRow= clsHelper.getDate(2018,9,iDay);

   
if (dtRow.equals(dtToday) )
    {
       row.setBackgroundColor(Color.rgb(
200, 0, 0));
    }   

 

Loading the second variable here via a method getDate (..)

public class clsHelper {


   
public static Date getDate(int year, int month, int day) {
        Calendar cal = Calendar.getInstance();
        month=month-
1 ;//*Months are indexed on 0
       
cal.set(year,month,day);
       
cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.
MINUTE, 0);
        cal.set(Calendar.
SECOND, 0);
        cal.set(Calendar.
MILLISECOND, 0);
       
return cal.getTime();
    }
}

 

 

If dates 1 and 2 are the same, or if they contain the same day, then show the line in red

 

 

public void create_Table(){
   
//-------< create_Table() >--------
    //< current Date >
    //Date today=new Date();
    //today=new Date();
   
Calendar today=Calendar.getInstance();
   
//current_CalendarDay.setTime(today);
   
today.set(Calendar.HOUR_OF_DAY, 0);
    today.set(Calendar.
MINUTE, 0);
    today.set(Calendar.
SECOND, 0);
    today.set(Calendar.
MILLISECOND, 0);
    Date dtToday=today.getTime();
   
//</ current Date >


   
TableLayout tbl=findViewById(R.id.table_main);

   
//----< @Loop: Days >----
   
for (int iDay=1;iDay<=31;iDay++) {
       
//----< Day-Row >----
        //-< Date >-
       
Date dtRow= clsHelper.getDate(2018,9,iDay);

       
//< weekday >
        //*as string
       
SimpleDateFormat fmtWeekday = new SimpleDateFormat("EE");
        String weekday = fmtWeekday.format(dtRow);
       
//</ weekday >

        //*as weekday_number
       
Calendar calendarDay=Calendar.getInstance();
        calendarDay.setTime(dtRow);
       
int NrWeekday=calendarDay.get(Calendar.DAY_OF_WEEK);//  fmtWeekdayNr.format(dtRow);
        //-</ Date >-

        //--< Row >--
       
TableRow row = new TableRow(this);
       
if(NrWeekday== Calendar.SATURDAY || NrWeekday==Calendar.SUNDAY) {
            row.setBackgroundColor(Color.rgb(
200, 200, 200));
        }

       
if (dtRow.equals(dtToday) )
        {
            row.setBackgroundColor(Color.rgb(
200, 0, 0));
        }

       
//< dayNr >
       
TextView lblDayNr = new TextView(this);
        lblDayNr.setText(String.valueOf(iDay) );   
//*cast integer to string
       
row.addView(lblDayNr);
       
//</ dayNr >

        //--< weekday >--
       
TextView lblWeekday = new TextView(this);
        lblWeekday.setText(weekday);
        row.addView(lblWeekday);
       
//--</ weekday >--

        //--</ Row >--

        //< add to table >
       
tbl.addView(row);
       
//</ add to table >
        //----</ Day-Row >----
   
}
   
//-------</ create_Table() >--------
}