0

I am able to change the my date Format 2014-12-09 to MMM dd,yyyy(Dec 09,2014 Like this.) But It Display Jan 09,2014 instead of Dec 09,2014. How to display Dec 09,2014? Please Guide me.

My Code is,

String strStartDate = "2014-12-09";

            readFormat = new SimpleDateFormat("yyyy-mm-dd");
            writeFormat = new SimpleDateFormat("MMM dd,yyyy");

            try {
                startDate = readFormat.parse(strStartDate);
            } catch (ParseException e) {
                e.printStackTrace();
            }

            viewHolder.txtStartDate
                    .setText(writeFormat.format(startDate));

Thanks in advance.

Reena
  • 563
  • 4
  • 11
  • 22

3 Answers3

6

Try with below code:

String date = "2014-11-25";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
Date testDate = null;
try {
    testDate = sdf.parse(date);
}catch(Exception ex){
    ex.printStackTrace();
}
SimpleDateFormat formatter = new SimpleDateFormat("MMM dd,yyyy");
String newFormat = formatter.format(testDate);
System.out.println(".....Date..."+newFormat);
MysticMagicϡ
  • 28,593
  • 16
  • 73
  • 124
Pratik Dasa
  • 7,439
  • 4
  • 30
  • 44
4

Formatted Code to get all Type of date. Only 3 things you need to pass it in argument is dateStr, dateStr Format is strReadFormat, Your desired fortmat as in Output is strWriteFormat.

public String getFormattedDate(String dateStr, String strReadFormat, String strWriteFormat) {

    String formattedDate = dateStr;

    DateFormat readFormat = new SimpleDateFormat(strReadFormat, Locale.getDefault());
    DateFormat writeFormat = new SimpleDateFormat(strWriteFormat, Locale.getDefault());

    Date date = null;

    try {
        date = readFormat.parse(dateStr);
    } catch (ParseException e) {
    }

    if (date != null) {
        formattedDate = writeFormat.format(date);
    }

    return formattedDate;
}

Log.v("Changed date",getFormattedDate("2014-11-26", "yyyy-mm-dd", "MMM dd,yyyy"));
Yugesh
  • 4,030
  • 9
  • 57
  • 97
  • I am Using your code but It display Dec 11,2014 Instead of Nov 11,2014. How to display Nov 11, 2014 Instead of Dec 11,2014. Because My current date is 2014-11-26 not a 2014-12-26. Its increment one month.How to Display Nov 11,2014. Please Reply me. – Reena Dec 09 '14 at 07:33
  • @Pratt I am Using your code but It display Dec 11,2014 Instead of Nov 11,2014. How to display Nov 11, 2014 Instead of Dec 11,2014. Because My current date is 2014-11-26 not a 2014-12-26. Its increment one month. – Reena Dec 09 '14 at 07:33
  • change new SimpleDateFormat("yyyy-mm-dd"); to new SimpleDateFormat("yyyy-MM-dd"); – Don Chakkappan Mar 31 '17 at 08:04
1
String t = "2014-11-26";

// To parse input string
SimpleDateFormat from = new SimpleDateFormat("yyyy-mm-dd", Locale.US);

// To format output string
SimpleDateFormat to = new SimpleDateFormat("MMMM dd, yyyy", Locale.US);

String res = to.format(from.parse(t));
  • I am Using your code but It display Dec 11,2014 Instead of Nov 11,2014. How to display Nov 11, 2014 Instead of Dec 11,2014. Because My current date is 2014-11-26 not a 2014-12-26. Its increment one month. – Reena Dec 09 '14 at 07:04