-1
try{
    SimpleDateFormat Format1 = new SimpleDateFormat("dd MMMMM yyyy hh:mm aaa");
    Format1.setLenient(false);
    s = "26 March 2017 10:30 am";
    Date d = Format1.parse(s);
}
Catch(Exceprion e){
        //ERROR!
}
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
PKC
  • 25
  • 1
  • 4

2 Answers2

2

Your string formatter is incorrect and your code will work depending on locale in the machine

you need to do something like:

new SimpleDateFormat("dd MMMMM yyyy hh:mm a", Locale.ENGLISH);

but it all depends of what MMMMM is (in your case March is ENGLISH )

it could be

Monday for Locale.ENGLISH

Montag for Locale.GERMAN

etc

Example:

    SimpleDateFormat Format1 = new SimpleDateFormat("dd MMMMM yyyy hh:mm a", Locale.ENGLISH);
     Format1.setLenient(false);
    String s = "26 March 2017 10:30 am";
    Date d;
    try {
        d = Format1.parse(s);
        System.out.println(d);
    } catch (ParseException e) {
        e.printStackTrace();
    }
Community
  • 1
  • 1
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
  • Adding the `Locale` argument is a good idea. You should still give small d in the format pattern (capital D is for day in year). The weekday (Monday/Montag), I can’t see how it could be relevant here. – Ole V.V. Apr 05 '17 at 11:31
2

Well of course it wont work, dd MMMMM yyyy hh:mm aaa should be dd MMMMM yyyy hh:mm a remember the a is the meridian AM or PM.

also remember your locale, Locale.ENGLISH as an example.

Remario
  • 3,813
  • 2
  • 18
  • 25
  • check this page for a detail table: https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html#text – Remario Apr 05 '17 at 11:01
  • are you still having parse issues? – Remario Apr 05 '17 at 11:04
  • When I add `Locale.ENGLISH` and correct `Catch(Exceprion e)` to `catch (Exception e)`, it works fine on my computer. `aaa` in the format pattern doesn’t seem to do any harm. – Ole V.V. Apr 05 '17 at 11:25
  • well according the documentation it does not exist. so do not use it. – Remario Apr 05 '17 at 11:29
  • glad to help though.might as well accept the answer for future comers then! – Remario Apr 05 '17 at 11:29
  • which version of java was the other system using? 7 or 8 – Remario Apr 05 '17 at 11:35
  • This worked however the aaa wasn't the problem. I copied it from the android developer page. The problem was the locale. I am not quiet sure why it is used but it worked!! Thanks:) – PKC Apr 05 '17 at 11:37
  • 1
    I don’t think there’s a Java 8 for Android just yet? Good question, though. I was just about to mention the `java.time` classes from JSR-310. Built into Java 8; if you want to use them in Java 7, you will be depending on a third party library (ThreeTen ABP). – Ole V.V. Apr 05 '17 at 11:37
  • oh i thought the other system was like Java EE or something. alright well there is kotlin which is pretty much java 8 – Remario Apr 05 '17 at 11:38
  • March is not called March in all languages, @PKC. Therefore having the correct locale is quite crucial. I’m not even sure am is called am in all languages. – Ole V.V. Apr 05 '17 at 11:39
  • you guys used Kotlin before? any thoughts on the date Api – Remario Apr 05 '17 at 11:40
  • According to the docs (and out of context, I admit): “For parsing, both forms are accepted, independent of the number of pattern letters.” It’s not the clearest spec, but I wouldn’t take it to mean that three letters `a` doesn’t exist. :-) I too would prefer to put just one or two. – Ole V.V. Apr 05 '17 at 11:44
  • 1
    Android is getting some features of Java 8 but not all of its libraries, as of 2017-04. Much of the java.time functionality is back-ported to Java 6 and Java 7 in the *ThreeTen-Backport* project, and further adapted for Android in the *ThreeTenABP* project. – Basil Bourque Apr 05 '17 at 16:40