In simple date format, day is not coming properly

73 views Asked by At

If I select a date from calendar it is showing the day of yesterday. I'm doing a project on date picker .

SimpleDateFormat dateFormat = new SimpleDateFormat("dd MMM, E", 
Locale.ENGLISH);
Date dat =new Date(year,month,date);
str = dateFormat.format(dat);
2

There are 2 answers

1
Vikas singh On

try this..

int mYear, mMonth, mDay;
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
c.add(Calendar.DAY_OF_MONTH, -7);
DatePickerDialog datePickerDialog = new DatePickerDialog(Attendence.this,
        new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
            String datee = "";
            String months = "";
            if (dayOfMonth < 10) {
                datee = "0" + dayOfMonth;
            } 
            else {
                datee = String.valueOf(dayOfMonth);
            }

            if ((monthOfYear + 1) < 10) {
                months = "0" + String.valueOf(monthOfYear + 1);
            } 
            else {
                months = String.valueOf(monthOfYear + 1);
            }

            String actualDate = datee + "-" + months + "-" + year;
            edtDatepicker.setText(actualDate);
        }
}, mYear, mMonth, mDay);
datePickerDialog.show();
0
marmor On

If you're using Date.getDay(), note the meaning of the integer value returned, is may seem strange in some countries:

int getDay ()

Returns the day of the week represented by this date. The returned value (0 = Sunday, 1 = Monday, 2 = Tuesday, 3 = Wednesday, 4 = Thursday, 5 = Friday, 6 = Saturday) represents the day of the week that contains or begins with the instant in time represented by this Date object, as interpreted in the local time zone.