kaveer
1/24/2017 - 5:46 PM

Date handling

Date handling

// xml layout
<TextView
                        android:text="Date"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:id="@+id/textView33" />
                        

//code
EditText repairDate;

private void InitializeEDitText(View view) {
        repairDate = (EditText)view.findViewById(R.id.repairDate);
    }
    
repairDate.setText(GetDateNow());

private String GetDateNow() {
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); //import java.text.SimpleDateFormat instead of android.icu.text.simpleDateFormat
        String currentDate = dateFormat.format(calendar.getTime());
        return currentDate;
    }
 public String AddDateToCurrentDate(String dateNow){
        String result = "2017-01-03";

        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Calendar c = Calendar.getInstance();
            c.setTime(sdf.parse(dateNow)); //current date - date now
            c.add(Calendar.DATE, 3);

            sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date resultDate = new Date(c.getTimeInMillis());
            result = sdf.format(resultDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return result;
    }
 private boolean CompareDate(String expiry, String actual) {
        boolean result = true;

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date expiryDate = sdf.parse(expiry);
            Date actualDate = sdf.parse(actual); //date now

            if (expiryDate.before(actualDate)){
                return false;
            }

        } catch (ParseException e) {
            e.printStackTrace();
        }

        return result;
    }