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!
}
            Asked
            
        
        
            Active
            
        
            Viewed 147 times
        
    -1
            
            
         
    
    
        ΦXocę 웃 Пepeúpa ツ
        
- 47,427
- 17
- 69
- 97
 
    
    
        PKC
        
- 25
- 1
- 4
- 
                    Adding the stack trace could help – Fustigador Apr 05 '17 at 10:53
- 
                    3Please include a [mcve] rather than pseudo-code. Also include details of your system's locale and the error. – Jon Skeet Apr 05 '17 at 10:54
- 
                    Please edit your title to actually describe the nature of your Question. – Basil Bourque Apr 16 '17 at 07:13
2 Answers
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
- 
                    
- 
                    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
- 
                    
- 
                    glad to help though.might as well accept the answer for future comers then! – Remario Apr 05 '17 at 11:29
- 
                    
- 
                    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
- 
                    1I 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
- 
                    
- 
                    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
- 
                    1Android 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