2016-06-14 15 views
0

Wenn Benutzer wählen date zwischen von und date. wie zu calculate anzahl der nacht in 3rd Edittext in android? für datepicker meinen Code NachSo berechnen Sie die Anzahl der Tage zwischen zwei Datepicker in Android

3 edittext box 

1 Von Datum 2 bis Datum 3 Anzahl der Nacht =?

CheckInDate = (EditText) rootView.findViewById(R.id.CheckInDate); 

     CheckOutDate = (EditText) rootView.findViewById(R.id.CheckOutDate); 





     CheckInDate.setOnClickListener(new View.OnClickListener() 

     { 
      @Override 
      public void onClick(View v) { 


       if (v == CheckInDate) { 

        final Calendar c = Calendar.getInstance(); 
        int mYear = c.get(Calendar.YEAR); 
        int mMonth = c.get(Calendar.MONTH); 
        int mDay = c.get(Calendar.DAY_OF_MONTH); 
        c.set(Calendar.DAY_OF_MONTH, mDay); 


        DatePickerDialog dpd = new DatePickerDialog(getActivity(), 
          new DatePickerDialog.OnDateSetListener() { 

           @Override 
           public void onDateSet(DatePicker view, int year, 
                 int monthOfYear, int dayOfMonth) { 
            Calendar cal = Calendar.getInstance(); 
            cal.set(Calendar.MONTH, monthOfYear); 
            cal.set(Calendar.DAY_OF_MONTH, dayOfMonth); 
            cal.set(Calendar.YEAR, year); 
            if(cal.before(c)) { 
             Toast.makeText(getActivity(), "Select valid date please.", Toast.LENGTH_LONG).show(); 
             return; 
            } 
            StringBuilder date = new StringBuilder(); 
            date.delete(0, date.length()); 
            date.append((dayOfMonth<10?"0":"")).append(dayOfMonth) 
              .append("/").append((monthOfYear + 1) < 10 ? "0" : "") 
              .append((monthOfYear+1)).append("/").append(year); 
            CheckInDate.setText(date.toString()); 
           } 
          }, mYear, mMonth, mDay); 
        dpd.getDatePicker().setMinDate(c.getTimeInMillis()); 
        dpd.show(); 
       } 

      } 

     }); 

     CheckOutDate.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 


       if (v == CheckOutDate) { 

        final Calendar c = Calendar.getInstance(); 
        int mYear = c.get(Calendar.YEAR); 
        int mMonth = c.get(Calendar.MONTH); 
        int mDay = c.get(Calendar.DAY_OF_MONTH); 
        c.set(Calendar.DAY_OF_MONTH, mDay); 


        DatePickerDialog dpd = new DatePickerDialog(getActivity(), 
          new DatePickerDialog.OnDateSetListener() { 

           @Override 
           public void onDateSet(DatePicker view, int year, 
                 int monthOfYear, int dayOfMonth) { 
            Calendar cal = Calendar.getInstance(); 
            cal.set(Calendar.MONTH, monthOfYear); 
            cal.set(Calendar.DAY_OF_MONTH, dayOfMonth); 
            cal.set(Calendar.YEAR, year); 
            if(cal.before(c)) { 
             Toast.makeText(getActivity(), "Select valid date please.", Toast.LENGTH_LONG).show(); 
             return; 
            } 
            StringBuilder date = new StringBuilder(); 
            date.delete(0, date.length()); 
            date.append((dayOfMonth<10?"0":"")).append(dayOfMonth) 
              .append("/").append((monthOfYear + 1) < 10 ? "0" : "") 
              .append((monthOfYear+1)).append("/").append(year); 
            CheckOutDate.setText(date.toString()); 
           } 
          }, mYear, mMonth, mDay); 
        dpd.getDatePicker().setMinDate(c.getTimeInMillis()); 
        dpd.show();    } 

      } 

     }); 
+0

Sie Joda Zeit verwenden können: http://stackoverflow.com/questions/3802893/number-of-days-between-two-dates-in-joda- Zeit – antonio

Antwort

0

Try this -

Date checkInTime, checkOutTime; 
private void checkDiff() { 
    long duration = checkOutTime.getTime() - checkInTime.getTime(); 

    long diffInSeconds = TimeUnit.MILLISECONDS.toSeconds(duration); 
    long diffInMinutes = TimeUnit.MILLISECONDS.toMinutes(duration); 
    long diffInHours = TimeUnit.MILLISECONDS.toHours(duration); 
    //Now Code as per requirement 
} 


     CheckInDate = (EditText) rootView.findViewById(R.id.CheckInDate); 
     CheckOutDate = (EditText) rootView.findViewById(R.id.CheckOutDate); 

     CheckInDate.setOnClickListener(new View.OnClickListener() 

     { 
      @Override 
      public void onClick(View v) { 


       if (v == CheckInDate) { 

        final Calendar c = Calendar.getInstance(); 
        int mYear = c.get(Calendar.YEAR); 
        int mMonth = c.get(Calendar.MONTH); 
        int mDay = c.get(Calendar.DAY_OF_MONTH); 
        c.set(Calendar.DAY_OF_MONTH, mDay); 

        DatePickerDialog dpd = new DatePickerDialog(getActivity(), 
          new DatePickerDialog.OnDateSetListener() { 

           @Override 
           public void onDateSet(DatePicker view, int year, 
                 int monthOfYear, int dayOfMonth) { 
            Calendar cal = Calendar.getInstance(); 
            cal.set(Calendar.MONTH, monthOfYear); 
            cal.set(Calendar.DAY_OF_MONTH, dayOfMonth); 
            cal.set(Calendar.YEAR, year); 
            if (cal.before(c)) { 
             Toast.makeText(getActivity(), "Select valid date please.", Toast.LENGTH_LONG).show(); 
             return; 
            } 
            checkInTime = cal.getTime(); 
            StringBuilder date = new StringBuilder(); 
            date.delete(0, date.length()); 
            date.append((dayOfMonth < 10 ? "0" : "")).append(dayOfMonth) 
              .append("/").append((monthOfYear + 1) < 10 ? "0" : "") 
              .append((monthOfYear + 1)).append("/").append(year); 
            CheckInDate.setText(date.toString()); 
           } 
          }, mYear, mMonth, mDay); 
        dpd.getDatePicker().setMinDate(c.getTimeInMillis()); 
        dpd.show(); 
       } 

      } 

     }); 

     CheckOutDate.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 


       if (v == CheckOutDate) { 

        final Calendar c = Calendar.getInstance(); 
        int mYear = c.get(Calendar.YEAR); 
        int mMonth = c.get(Calendar.MONTH); 
        int mDay = c.get(Calendar.DAY_OF_MONTH); 
        c.set(Calendar.DAY_OF_MONTH, mDay); 
        checkOutTime = c.getTime(); 

        DatePickerDialog dpd = new DatePickerDialog(getActivity(), 
          new DatePickerDialog.OnDateSetListener() { 

           @Override 
           public void onDateSet(DatePicker view, int year, 
                 int monthOfYear, int dayOfMonth) { 
            Calendar cal = Calendar.getInstance(); 
            cal.set(Calendar.MONTH, monthOfYear); 
            cal.set(Calendar.DAY_OF_MONTH, dayOfMonth); 
            cal.set(Calendar.YEAR, year); 
            if (cal.before(c)) { 
             Toast.makeText(getActivity(), "Select valid date please.", Toast.LENGTH_LONG).show(); 
             return; 
            } 
            StringBuilder date = new StringBuilder(); 
            date.delete(0, date.length()); 
            date.append((dayOfMonth < 10 ? "0" : "")).append(dayOfMonth) 
              .append("/").append((monthOfYear + 1) < 10 ? "0" : "") 
              .append((monthOfYear + 1)).append("/").append(year); 
            CheckOutDate.setText(date.toString()); 
            checkOutTime = cal.getTime(); 
            checkDiff(); 
           } 
          }, mYear, mMonth, mDay); 
        dpd.getDatePicker().setMinDate(c.getTimeInMillis()); 
        dpd.show(); 
       } 

      } 

     }); 
    } 
+0

wo sollte ich diesen Code setzen – ATKT

+0

In Ihrem Fall machen Sie zwei öffentliche Refrecesh von Date (sagen wir checkInDate und checkOutDate) in onDateSet Holen Sie sich das Datum aus dem Kalender und weisen Sie es der jeweiligen Referenz zu. In onDateSet der zweiten (d. H. Kasse) können Sie die Differenz zwischen beiden Daten berechnen. – Neo

+0

Ich bin Anfänger bro :( – ATKT

0

diesen Code Versuchen TOTALNUMBER von Tagen zu erhalten:

Calendar cal1 = new GregorianCalendar(); 
    Calendar cal2 = new GregorianCalendar(); 

    SimpleDateFormat sdf = new SimpleDateFormat("ddMMyyyy"); 

    Date date = sdf.parse("your first date"); 
    cal1.setTime(date) 
    date = sdf.parse("your second date"); 
    cal2.setTime(date); 

System.out.println("Days= "+daysBetween(cal1.getTime(),cal2.getTime())); 

Überprüfen Sie die Anzahl der Tage Code hier:

 CheckOutDate.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 


       if (v == CheckOutDate) { 

        final Calendar c = Calendar.getInstance(); 
        int mYear = c.get(Calendar.YEAR); 
        int mMonth = c.get(Calendar.MONTH); 
        int mDay = c.get(Calendar.DAY_OF_MONTH); 
        c.set(Calendar.DAY_OF_MONTH, mDay); 


        DatePickerDialog dpd = new DatePickerDialog(getActivity(), 
          new DatePickerDialog.OnDateSetListener() { 

           @Override 
           public void onDateSet(DatePicker view, int year, 
                 int monthOfYear, int dayOfMonth) { 
            Calendar cal = Calendar.getInstance(); 
            cal.set(Calendar.MONTH, monthOfYear); 
            cal.set(Calendar.DAY_OF_MONTH, dayOfMonth); 
            cal.set(Calendar.YEAR, year); 
            if(cal.before(c)) { 
             Toast.makeText(getActivity(), "Select valid date please.", Toast.LENGTH_LONG).show(); 
             return; 
            } 
            StringBuilder date = new StringBuilder(); 
            date.delete(0, date.length()); 
            date.append((dayOfMonth<10?"0":"")).append(dayOfMonth) 
              .append("/").append((monthOfYear + 1) < 10 ? "0" : "") 
              .append((monthOfYear+1)).append("/").append(year); 
            CheckOutDate.setText(date.toString()); 




            /************* Check you days HERE ***************/ 
           try { 

            Calendar calCheckIn = new GregorianCalendar(); 
\         Calendar calCheckOut = new GregorianCalendar(); 

            SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy"); 


            Date dateCheckIn = sdf.parse(CheckInDate.getText().toString()); 
            calCheckIn.setTime(dateCheckIn); 
            Date dateCheckOut = sdf.parse(CheckOutDate.getText().toString()); 
            calCheckOut.setTime(dateCheckOut); 

            System.out.println("Days= "+daysBetween(dateCheckIn.getTime(),dateCheckOut.getTime())); 

            EditText Night = (EditText) rootView.findViewById(R.id.Noofnights); 
             Night.setText(daysBetween(dateCheckIn.getTime(),dateCheckOut.getTime()) + ""); 
            } catch (Exception e) { 
            e.printStackTrace(); 
            } 
/***************************************/ 


           } 
          }, mYear, mMonth, mDay); 
        dpd.getDatePicker().setMinDate(c.getTimeInMillis()); 
        dpd.show();    } 

      } 

     }); 

diese Methode überall in der Klasse Put:

public int daysBetween(Date d1, Date d2){ 
      return (int)((d2.getTime() - d1.getTime())/(1000 * 60 * 60 * 24)); 
    } 
+0

wo sollte ich diesen Code setzen? – ATKT

+0

In welcher Edittext Sie die Nummer der Nacht setzen? Legen Sie den Code hier – NehaK

+0

Night = (EditText) rootView.findViewById (R.id.Noofnights); – ATKT