skip to Main Content

Does anyone know how to change the date format in DatePickerDialog in Android studio in Java, I need like "Mon, May 6" I need like this format instead of "M05 6, Mon"Image link .

agetxt.setOnClickListener(v -> {
            Utils.HideKeyboard(FarmInputs.this);
            DatePickerDialog datePicker = new DatePickerDialog(FarmInputs.this, 0, new DatePickerDialog.OnDateSetListener() {
                @Override
                public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                    Date = dayOfMonth + "/" + (month + 1) + "/" + year;
                    DateDb = year + "-" + (month + 1) + "-" + dayOfMonth;
                    agetxt.setText(Date);
                    try {
                        Age = GetDays(dayOfMonth + "/" + (month + 1) + "/" + year);
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                }
            }, cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH));

            datePicker.setCancelable(false);
            datePicker.getDatePicker().setMaxDate(System.currentTimeMillis() + Constants.days60_millisecond);
            datePicker.show();

3

Answers


  1. To change the date format in DatePickerDialog in Android Studio to something like "Mon, May 6", you can format the date using SimpleDateFormat after selecting the date. Here’s how you can do it:

    Import the necessary classes:

    import java.text.SimpleDateFormat;
    import java.util.Calendar;
    import java.util.Date;
    

    Update your onDateSet method to format the date:

    agetxt.setOnClickListener(v -> {
        Utils.HideKeyboard(FarmInputs.this);
        DatePickerDialog datePicker = new DatePickerDialog(FarmInputs.this, 0, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                Calendar calendar = Calendar.getInstance();
                calendar.set(year, month, dayOfMonth);
    
                SimpleDateFormat sdf = new SimpleDateFormat("EEE, MMM d");
                String formattedDate = sdf.format(calendar.getTime());
    
                agetxt.setText(formattedDate);
    
                DateDb = year + "-" + (month + 1) + "-" + dayOfMonth;
    
                try {
                    Age = GetDays(dayOfMonth + "/" + (month + 1) + "/" + year);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }
        }, cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH));
    
        datePicker.setCancelable(false);
        datePicker.getDatePicker().setMaxDate(System.currentTimeMillis() + Constants.days60_millisecond);
        datePicker.show();
    });
    

    In this example:

    We use Calendar to set the selected date.
    SimpleDateFormat is used to format the date as "Mon, May 6".
    The formatted date is set to the agetxt TextView.
    This approach ensures the date is displayed in the desired format after the user selects a date.

    Login or Signup to reply.
  2. Use below code to achieve your output, And set it with your requirements

    Output

    enter image description here

    enter image description here

    enter image description here

    Code

    public class MainActivity2 extends AppCompatActivity {
    
        // Number of milliseconds in one day
        public static final long ONE_DAY_MILLISECONDS = 24 * 60 * 60 * 1000;
    
        // Number of milliseconds in 60 days
        public static final long DAYS_60_MILLISECONDS = 60 * ONE_DAY_MILLISECONDS;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main2);
    
            TextView finalDate = findViewById(R.id.final_date);
    
            Calendar cal = Calendar.getInstance();
    
            findViewById(R.id.agetxt).setOnClickListener(v -> {
                DatePickerDialog datePicker = new DatePickerDialog(this, 0, new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                        // Create a Calendar object with the selected date
                        Calendar selectedDate = Calendar.getInstance();
                        selectedDate.set(year, month, dayOfMonth);
    
    //                    // Format the date as "Mon, May 6"
    //                    SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, MMM d", Locale.getDefault());
    //                    String formattedDate = dateFormat.format(selectedDate.getTime());
    
                        // Format the date as "dd/MM/yyyy"
                        SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, dd MMM", Locale.getDefault());
                        String formattedDate = dateFormat.format(selectedDate.getTime());
    
                        // Set the formatted date to the TextView
    //                    agetxt.setText(formattedDate);
                        Log.e("tag", "formattedDate = " + formattedDate);
                        finalDate.setText("Date is: " + formattedDate);
                    }
                }, cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH));
    
                datePicker.setCancelable(false);
                datePicker.getDatePicker().setMaxDate(System.currentTimeMillis() + DAYS_60_MILLISECONDS);
                datePicker.show();
            });
    
        }
    }
    
    Login or Signup to reply.
  3. agetxt.setOnClickListener(v -> {
        Utils.HideKeyboard(FarmInputs.this);
        DatePickerDialog datePicker = new DatePickerDialog(FarmInputs.this, 0, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                // Format the date
                Calendar calendar = Calendar.getInstance();
                calendar.set(year, month, dayOfMonth);
                SimpleDateFormat dateFormat = new SimpleDateFormat("E, MMM d", Locale.ENGLISH);
                Date = dateFormat.format(calendar.getTime());
    
                // Set the formatted date
                agetxt.setText(Date);
                DateDb = year + "-" + (month + 1) + "-" + dayOfMonth;
    
                // Calculate age
                try {
                    Age = GetDays(dayOfMonth + "/" + (month + 1) + "/" + year);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }
        }, cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH));
    
        datePicker.setCancelable(false);
        datePicker.getDatePicker().setMaxDate(System.currentTimeMillis() + Constants.days60_millisecond);
        datePicker.show();
    });
    

    Try This Way.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search