简体   繁体   中英

How to get int Month, Day, And Year in String format “EEE, MMM d, yyyy”

I'm hoping you guys can help me on this. When i use DatePicker, i display my date in "EEE, MMM d, yyyy" format.

            Calendar cal = Calendar.getInstance();
            cal.set(Calendar.YEAR, year);
            cal.set(Calendar.MONTH, month);
            cal.set(Calendar.DAY_OF_MONTH, dayOfMonth);
            String strDate = new SimpleDateFormat("EEE, MMM d, yyyy").format(cal.getTime());
            editTextStartDate.setText(strDate);

But after setting the date i will process the data, And I need the data to be formatted into "dd/mm/yyyy" so that i can use it by splitting it:

    String startDate = editTextStartDate.getText().toString().trim();
    if (!startDate.isEmpty()) {
        String getTo[] = startDate.split("/");
        getToDateYear = Integer.parseInt(getTo[2]);
        getToDateMonth = Integer.parseInt(getTo[1]);
        getToDateDay = Integer.parseInt(getTo[0]);

        toTime.set(Calendar.YEAR, getToDateYear);
        toTime.set(Calendar.MONTH, getToDateMonth-1);
        toTime.set(Calendar.DAY_OF_MONTH, getToDateDay);
    }

What is the best way for me to do this? Hopefully you guys can help teach and guide me. Thank you guys!

There will not be any / in String strDate = new SimpleDateFormat("EEE, MMM d, yyyy").format(cal.getTime());

So, Your String getTo[] will not have any Array Output.

So Solution is below to convert your Date format to another format as below:

SimpleDateFormat sourceDateFormat = new SimpleDateFormat("EEE, MMM d, yyyy");
Date sourceDate = null;
try {
    sourceDate = sourceDateFormat.parse(startDate);
} catch (ParseException e) {
    e.printStackTrace();
}

SimpleDateFormat targetFormat = new SimpleDateFormat("dd/MM/yyyy");
String targetdatevalue= targetFormat.format(sourceDate);

So targetdatevalue will be your desired output.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM