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: Datum Tag vergleichen

21.09.2018 (👁159)


 

Wie kann man in Android Java zwei Datum Date Werte gegeneinander auf das gleiche Tagesdatum vergleichen.

Oder einen Vergleich auf den Tag oder Stunde oder Minute.

 

Lösung:

Man erstellt zwei Datums-Variablen vom Typ Calendar.

Dann setzt man die Stunde, Minute, Sekunde und Millisekunde der Variablen auf 0.

Danach hat man den gleichen Tageswert zur Stunde 0.

Diese kann man mit Datum.equals(Datum2) vergleichen.

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

 

Laden der zweiten Variable hier über eine Methode 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();
    }
}

 

 

Wenn Datum 1 und 2 gleich sind, oder der gleiche Tag beinhaltet, dann zeige die Zeile auf Rot

 

 

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() >--------
}