-1

How can i parse String "2013-03-31", so i will get - "31 March 2013 Sun". I tried to parse as bellow:

final SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy DD");
try {
    Date data = formatter.parse(eventDate);
    System.out.println(data);
} catch (Exception e) {
    System.out.println(e.getMessage());
}

And i get Unparseable date: "2013-03-31" (at offset 10) exception text.

Is that String represented date is correct, and if correct how i must parse it.

bhathiya-perera
  • 1,303
  • 14
  • 32
Procurares
  • 2,169
  • 4
  • 22
  • 34

4 Answers4

1

Input format: "yyyy-MM-dd"
Output format: "dd MMMM yyyy EEE"

final SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd");
final SimpleDateFormat outputFormat = new SimpleDateFormat("dd MMMM yyyy EEE");
try {
    Date data = inputFormat.parse(eventDate);
    System.out.println(outputFormat.format(data));
} catch (Exception e) {
    System.out.println(e.getMessage());
}
Martijn Courteaux
  • 67,591
  • 47
  • 198
  • 287
0

see these links

string to date

How to parse a date?

How to parse date string to Date?

date to string

Convert java.util.Date to String

How to convert current date into string in java?

Community
  • 1
  • 1
bhathiya-perera
  • 1,303
  • 14
  • 32
0

You don't specify a date input format:

String eventDate = "2013-03-31";

final SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd");
final SimpleDateFormat outputFormat = new SimpleDateFormat(
        "dd MMMM yyyy EEE");
try {

    Date date = inputFormat.parse(eventDate);
    System.out.println(outputFormat.format(date));

} catch (Exception e) {

    e.printStackTrace();

}
amatellanes
  • 3,645
  • 2
  • 17
  • 19
0
public static String getGmtTmFromLcl(String inputDtm, String inputFormat, String outputFormat) throws Exception {
        SimpleDateFormat format = new SimpleDateFormat(inputFormat);
        Date dt = format.parse(inputDtm);
        format = new SimpleDateFormat(outputFormat);
        return format.format(dt);
    }