1

I am using SimpleDateFormat on Android. I tested it with API 24 and it worked. When i try using it on API 16 it crashes. I have searched the Internet and i found people saying that it might be because of a wrong import statement (import android.icu.text.SimpleDateFormat) but i am already using import java.text.SimpleDateFormat.

My code (it crashes on onclickButton):

import java.text.SimpleDateFormat;

public class MainActivity extends AppCompatActivity {

SimpleDateFormat sdf;
String date;

    public void onclickButton (View view) {

        Calendar c = Calendar.getInstance();
        sdf = new SimpleDateFormat("dd MM YYYY");
        sdf.setTimeZone(TimeZone.getTimeZone("Europe/Berlin"));
        date = sdf.format(c.getTime());
    }
...
}

Stacktrace:

W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0xb111d228)
E/AndroidRuntime: FATAL EXCEPTION: main
              java.lang.IllegalStateException: Could not execute method for android:onClick
                  at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
                  at android.view.View.performClick(View.java:4084)
                  at android.view.View$PerformClick.run(View.java:16966)
                  at android.os.Handler.handleCallback(Handler.java:615)
                  at android.os.Handler.dispatchMessage(Handler.java:92)
                  at android.os.Looper.loop(Looper.java:137)
                  at android.app.ActivityThread.main(ActivityThread.java:4745)
                  at java.lang.reflect.Method.invokeNative(Native Method)
                  at java.lang.reflect.Method.invoke(Method.java:511)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
                  at dalvik.system.NativeStart.main(Native Method)
               Caused by: java.lang.reflect.InvocationTargetException
                  at java.lang.reflect.Method.invokeNative(Native Method)
                  at java.lang.reflect.Method.invoke(Method.java:511)
                  at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
                  at android.view.View.performClick(View.java:4084) 
                  at android.view.View$PerformClick.run(View.java:16966) 
                  at android.os.Handler.handleCallback(Handler.java:615) 
                  at android.os.Handler.dispatchMessage(Handler.java:92) 
                  at android.os.Looper.loop(Looper.java:137) 
                  at android.app.ActivityThread.main(ActivityThread.java:4745) 
                  at java.lang.reflect.Method.invokeNative(Native Method) 
                  at java.lang.reflect.Method.invoke(Method.java:511) 
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
                  at dalvik.system.NativeStart.main(Native Method) 
               Caused by: java.lang.IllegalArgumentException: Unknown pattern character 'Y'
                  at java.text.SimpleDateFormat.validateFormat(SimpleDateFormat.java:268)
                  at java.text.SimpleDateFormat.validatePattern(SimpleDateFormat.java:323)
                  at java.text.SimpleDateFormat.<init>(SimpleDateFormat.java:369)
                  at java.text.SimpleDateFormat.<init>(SimpleDateFormat.java:253)
                  at com.jonathan_tilly.adventskalender.MainActivity.onclickButton(MainActivity.java:34)
                  at java.lang.reflect.Method.invokeNative(Native Method) 
                  at java.lang.reflect.Method.invoke(Method.java:511) 
                  at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 
                  at android.view.View.performClick(View.java:4084) 
                  at android.view.View$PerformClick.run(View.java:16966) 
                  at android.os.Handler.handleCallback(Handler.java:615) 
                  at android.os.Handler.dispatchMessage(Handler.java:92) 
                  at android.os.Looper.loop(Looper.java:137) 
                  at android.app.ActivityThread.main(ActivityThread.java:4745) 
                  at java.lang.reflect.Method.invokeNative(Native Method) 
                  at java.lang.reflect.Method.invoke(Method.java:511) 
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
                  at dalvik.system.NativeStart.main(Native Method) 

Any help is appreciated!

nox
  • 11
  • 4
  • 1
    Show the stacktrace. Also, I don't think you can declare variables outside of a class. – nbokmans Nov 25 '16 at 16:47
  • You are right, i edited it. I only made the mistake when i copied the code. I also added the stacktrace – nox Nov 25 '16 at 16:59
  • Try with `dd MM yyyy` – Nicolas Filotto Nov 25 '16 at 17:04
  • @trooper The OP's current problem is that `Y` is not a valid character before API 24. If the OP replies to state that they specifically meant to be using week year, then I might undo my vote, as they'd need a completely different solution for API < 24. However, I don't see how either of your suggestions in the previous comment will help. Furthermore, I just ran a quick test on an old API, and there was no issue with `y` with either the specified `TimeZone`, or your `Locale` suggestion. – Mike M. Nov 25 '16 at 18:09

2 Answers2

0

Probably that's because you are declaring variables out the class. Try:

import java.text.SimpleDateFormat;

public class MainActivity extends AppCompatActivity {

    SimpleDateFormat sdf;
    String date;

    public void onclickButton (View view) {

        Calendar c = Calendar.getInstance();
        sdf = new SimpleDateFormat("dd MM yyyy");
        sdf.setTimeZone(TimeZone.getTimeZone("Europe/Berlin"));
        date = sdf.format(c.getTime());
    }
...

}

fsnasser
  • 203
  • 4
  • 11
  • Thank you for your answer. you are right, that was a mistake that i made when i copied the code to my post. i edited it above :) i still have the problem with this correction. – nox Nov 25 '16 at 17:08
  • 1
    See the line in your Stacktrace: `Caused by: java.lang.IllegalArgumentException: Unknown pattern character 'Y'`. Try to change YYYY to yyyy (lower case). – fsnasser Nov 25 '16 at 17:46
-1

I think you should use

new SimpleDateFormat("dd MM yyyy");

Instead of

new SimpleDateFormat("dd MM YYYY");

It worked for me

And java doc says YYYY is for week year Here

Vihaari Varma
  • 155
  • 11