Cengiz Dogan
Cengiz Dogan

Reputation: 149

Simple date format in java, Parse Exception

here is my date String : "07SEP2014 00:00"

and this is the code that convert string to date :

new SimpleDateFormat("ddMMMyyyy HH:mm").parse(dateString);

and i'm getting parse exception. What i'm doing wrong?

Upvotes: 0

Views: 2615

Answers (3)

Vish
Vish

Reputation: 842

You need to customize your date format based on pattern. More details here on customizing it.

Edit: Case is important here. SEP should be like Sep

    SimpleDateFormat formatter=new SimpleDateFormat("ddMMMyyyy HH:mm");
    Date today = new Date();
    today.setMonth(8);
    String result = formatter.format(today);
    System.out.println(result);

Output: 10Sep2015 15:08

Edit 2: Your example is also working.

    SimpleDateFormat formatter=new SimpleDateFormat("ddMMMyyyy HH:mm");
    Date result = formatter.parse("07Sep2014 00:00");
    System.out.println(result.toString());

Output: Sun Sep 07 00:00:00 IST 2014

Upvotes: 0

dube
dube

Reputation: 5019

Your Code works - as long as your System has a Locale where SEP = September. You could set the Locale to be sure about that:

Date result = new SimpleDateFormat("ddMMMyyyy HH:mm",Locale.ENGLISH).parse(dateString);

Upvotes: 1

OO7
OO7

Reputation: 2807

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateParse {

    public static void main(String[] args) {

        String date = "07SEP2014 00:00";
        try {
            Date dateParse = new SimpleDateFormat("ddMMMyyyy HH:mm").parse(date);
            System.out.println(dateParse);
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
}

Output :

Sun Sep 07 00:00:00 IST 2014

Upvotes: 0

Related Questions