-2

I want to convert the date format to yyyy-MM-dd i am getting from the DatePickerDialog and storing the date in editext, and retrieving the date into string, the date i am getting in String is in the format of mm-dd-yyyy

    public class MainActivity extends AppCompatActivity
            implements NavigationView.OnNavigationItemSelectedListener, EnquiryAdapter.OnItemClickListener {

            String StartDate = mStartDate.getText().toString();
            String endDate = mEndDate.getText().toString();
     private EditText mStartDate, mEndDate;

     private void showFilterDialog(){
            LayoutInflater layoutInflater = LayoutInflater.from(MainActivity.this);
            View filterDialog = layoutInflater.inflate(R.layout.filters,null);


            AlertDialog.Builder alertBuilder = new AlertDialog.Builder(MainActivity.this);
            alertBuilder.setTitle("Filters");
            alertBuilder.setView(filterDialog);

            mStartDate = filterDialog.findViewById(R.id.editText);
            mEndDate = filterDialog.findViewById(R.id.editText2);


            String StartDate = mStartDate.getText().toString();
            String endDate = mEndDate.getText().toString();

 // i have tried this method but getting error
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            String sd = format.format(java.sql.Date.parse(StartDate));
            String ed = format.format(Date.parse(endDate));
            Log.d("Start Date", sd);
            Log.d("End Date", ed);
    }

// getting error java.lang.IllegalArgumentException

Shubham
  • 75
  • 2
  • 11
  • This problem already has a solution.[Solution](https://stackoverflow.com/a/882465/9263083) – sanoJ Apr 02 '19 at 11:19
  • Please indent your code properly so we can read it, post the stacktrace from your exception and point out which line of your code the stacktrace refers to. – Ole V.V. Apr 02 '19 at 11:25
  • Consider adding [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP) to your Android project so that you may use [java.time, the modern Java date and time API,](https://docs.oracle.com/javase/tutorial/datetime/) and throw away `Date` and `SimpleDateFormat`. `Date` and `SimpleDateFormat` are poorly designed and long outdated, and the modern API is so much nicer to work with. – Ole V.V. Apr 02 '19 at 11:29
  • I see no date picker dialog in your code, and I see nothing that might produce mm-dd-yyyy format. Also the code posted is not complete, so we can’t run it and try to reproduce your exception. Please [create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) so we may help you with your problems. – Ole V.V. Apr 02 '19 at 11:32

2 Answers2

0

Try this @Shubham

SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
Calendar calDate; = calDate = Calendar.getInstance();
calDate.add(Calendar.DAY_OF_YEAR, 0);
String date = simpleDateFormat.format(calDate.getTime());
Log.e("result_date",date);
Brahma Datta
  • 1,102
  • 1
  • 12
  • 20
0

Try This it may help you

public static String parseDate(String inputDateString, SimpleDateFormat inputDateFormat, SimpleDateFormat outputDateFormat) {
    Date date = null;
    String outputDateString = null;
    try {
        date = inputDateFormat.parse(inputDateString);
        outputDateString = outputDateFormat.format(date);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return outputDateString;
}

How to call

public static final SimpleDateFormat inputFormat = new SimpleDateFormat("mm-dd-yyyy", Locale.getDefault());

public static final SimpleDateFormat outPutForamt = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());

private String outputDateStr = "";

outputDateStr = parseDate("04-02-2019", inputFormat , outPutForamt );

Log.i("output_string", outputDateStr);
VIISHRUT MAVANII
  • 11,410
  • 7
  • 34
  • 49