18

How to convert 2013-06-24 to 24 Jun 2013? I am using the below code.

date1="2013-06-24";
SimpleDateFormat d= new SimpleDateFormat("dd MMM yyyy");

try{
date2 =  d.parse(date1);
}catch (ParseException e1) {
// TODO Auto-generated catch block
  e1.printStackTrace();
}  

But I am getting this error "java.text.ParseException: Unparseable date: "2013-06-24" (at offset 4)"

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
user1740281
  • 383
  • 2
  • 5
  • 16
  • Update: The classes seen in the Question are now legacy, supplanted years ago by the *java.time* classes defined in JSR 310. See the modern solution in [Answer by Arvind Kumar Avinash](https://stackoverflow.com/a/75850647/642706). – Basil Bourque Mar 27 '23 at 06:46

9 Answers9

58

You need two DateFormat instances: One to parse the original String, and another to output the one you want.

DateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd");
DateFormat outputFormat = new SimpleDateFormat("dd MMM yyyy");
String inputDateStr="2013-06-24";
Date date = inputFormat.parse(inputDateStr);
String outputDateStr = outputFormat.format(date);
ZR Low
  • 51
  • 7
duffymo
  • 305,152
  • 44
  • 369
  • 561
4

See the first problem is that you are using different delimiters for String and Date. So either you do "2013-06-24" to "2013 06 24" in String or do new SimpleDateFormat("dd MMM yyyy") to new SimpleDateFormat("dd-MMM-yyyy").

And second problem is that you cannot directly change format like this, in String you are having year-month-date format, so first make a Date object with same format than change it to your desired format as below :

date1="2013-06-24";

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

Date dt = format.parse(date1);

SimpleDateFormat your_format = new SimpleDateFormat("dd-MMM-yyyy");

date2 = your_format.format(dt);
Priyank Joshi
  • 300
  • 1
  • 4
  • 17
2

Change

SimpleDateFormat d= new SimpleDateFormat("dd MMM yyyy");

with

SimpleDateFormat d= new SimpleDateFormat("yyyy-MM-dd");

you have to follow the date1 pattern. Then you can format your parsed date with

new SimpleDateFormat("dd MMM yyyy");
Blackbelt
  • 156,034
  • 29
  • 297
  • 305
2
public String getStringFormatted(String datestring) {
    String format = "dd MM yyyy";
    SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.getDefault());
    return sdf.format(new Date(datestring.replaceAll("-", "/")));
}
Linh
  • 57,942
  • 23
  • 262
  • 279
Marcela
  • 21
  • 3
2

java.time

The java.util date-time API and their corresponding parsing/formatting type, SimpleDateFormat are outdated and error-prone. In March 2014, the modern Date-Time API was released as part of the Java 8 standard library which supplanted the legacy date-time API and since then it is strongly recommended to switch to java.time, the modern date-time API.

Solution using java.time*

The java.time API is based on ISO 8601 standards and does not require specifying a DateTimeFormatter to parse a date/time string which is already in ISO 8601 format. Your given date string, 2013-06-24 is already in ISO 8601 format. You need a DateTimeFormatter just for formatting the LocalDate which will be obtained by parsing the given date string.

Note: The date-time parsing/formatting APIs are Locale sensitive and therefore never use date-time parsing/formatting APIs without a Locale.

Demo:

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

class Main {
    public static void main(String[] args) {
        LocalDate date = LocalDate.parse("2013-06-24");
        System.out.println(date);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd MMM uuuu", Locale.ENGLISH);
        String formatted = date.format(formatter);
        System.out.println(formatted);
    }
}

Output:

2013-06-24
24 Jun 2013

ONLINE DEMO

Learn more about the modern Date-Time API from Trail: Date Time.


* If you are working on an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
1
public String inputFormatget (String datestring1) {
        String format = "dd MM yyyy";
        SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.getDefault());
        return sdf.format(new Date(datestring.replaceAll("-", "/")));
    }

    public String outFormatset (String datestring2) {
        String format = "dd MM yyyy";
        SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.getDefault());
        return sdf.format(new Date(datestring.replaceAll("-", "/")));
    }
Raj Shah
  • 668
  • 1
  • 9
  • 23
1
public static String formateDateFromstring(String inputFormat, String outputFormat, String inputDate){

    Date parsed = null;
    String outputDate = "";

    SimpleDateFormat df_input = new SimpleDateFormat(inputFormat, java.util.Locale.getDefault());
    SimpleDateFormat df_output = new SimpleDateFormat(outputFormat, java.util.Locale.getDefault());

    try {
        parsed = df_input.parse(inputDate);
        outputDate = df_output.format(parsed);

    } catch (ParseException e) {
    }

    return outputDate;

}

String convertedDate = formateDateFromstring(
    "yyyy-MM-dd",
    "dd/MM/yyyy",
    datestring);

updateInsuranceEventFragmentBinding.edtdate.setText(convertedDate);
Moog
  • 10,193
  • 2
  • 40
  • 66
Raj Shah
  • 668
  • 1
  • 9
  • 23
1

Here is a generic method you can use to get required format.

Usage - getRequiredDateFormat("2019-09-03", "yyyy-MM-dd", "dd MM yyyy")

private String getRequiredDateFormat(String inputDate, String inputDateFormat, String outputDateFormat) {

    DateFormat inputFormat = new SimpleDateFormat(inputDateFormat);
    DateFormat outputFormat = new SimpleDateFormat(outputDateFormat);
    Date date = null;

    try {
        date = inputFormat.parse(inputDate);

    } catch (ParseException e) {
        LOGGER.error("Could not parse date {}", e.getMessage());
        return null;
    }

    return outputFormat.format(date);
}
SharadxDutta
  • 1,058
  • 8
  • 21
0

String idl = "2021-06-30"; String dl=null; try{ SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd"); SimpleDateFormat outputFormat = new SimpleDateFormat("dd-MM-yyyy"); Date date = inputFormat.parse(idl); dl = outputFormat.format(date); System.out.println(dl); } catch (ParseException e1) { e1.printStackTrace(); }