2

My date string is "Oct 17 2016 12:17PM" and My date parsing method is:

public static String formatDate(String dateString) {
    String stringDate = "";
    try {
        //Mar 11 2016 2:21PM
        if (dateString != null) {
            SimpleDateFormat dt = new SimpleDateFormat("MMM dd yyyy hh:mma");

            Date date = dt.parse(dateString);

            // *** same for the format String below
            SimpleDateFormat dt1 = new SimpleDateFormat("EEE , hh:mm a");
            stringDate = dt1.format(date);
        }
        return stringDate;
    } catch (ParseException parseException) {
        Log.e("date format", "" + parseException.getMessage());
    }
    return stringDate;
}

It is working fine with english language but when i change my app language to hindi or any other i am getting an exception.

java.text.ParseException: Unparseable date: "Oct 17 2016 12:17PM" (at offset 0)

What i do?

1
  • Date parsing depends on language. It's a Locale problem. There's many many duplicate posts here about that Commented Oct 17, 2016 at 7:16

2 Answers 2

1

You need set Locale English

SimpleDateFormat dt = new SimpleDateFormat("MMM dd yyyy hh:mma", Locale.ENGLISH);
Sign up to request clarification or add additional context in comments.

Comments

1
 public class Practice {
 public static void main(String...args) {
 String day,month, finalDay;
 try {
 DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
 String  currentDateTimeString = df.format(new Date());


     //String currentDateTimeString = DateFormat.getDateTimeInstance().format(new Date());
     SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy", Locale.US);
     Date date = format.parse(currentDateTimeString);


     SimpleDateFormat dayFormat = new SimpleDateFormat("dd");


     day = dayFormat.format(date);


     SimpleDateFormat monthFormat = new SimpleDateFormat("MM");

     month = monthFormat.format(date);
     System.out.print("My day"+ day);
     System.out.println("My month"+ month);




 } catch (Exception e) {
     System.out.println("Hello exception "+ e.getMessage());
     e.printStackTrace();
 }

} }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.