Search code examples
javasimpledateformat

SimpleDateFormat print "." for MMM format


I am trying to convert date string from one format to another using SimpleDateFormat. Conversion works but there is a dot "." after month.

    String dateStr = "04/02/1987";
    DateFormat df1 = new SimpleDateFormat("dd/MM/yyyy");
    Date d = df1.parse(dateStr);
    DateFormat df2 = new SimpleDateFormat("dd MMM yyyy");
    System.out.println(df2.format(d));

Output is 04 Feb. 1987 instead of 04 Feb 1987.


Solution

  • Please provide Locale.ENGLISH in SimpleDateFormat constructor while creating object as shown below:

    String dateStr = "04/02/1987";
    DateFormat df1 = new SimpleDateFormat("dd/MM/yyyy");
    Date d = df1.parse(dateStr);
    DateFormat df2 = new SimpleDateFormat("dd MMM yyyy", Locale.ENGLISH);
    System.out.println(df2.format(d));