DatePickerDialog incorrectly enforcing a minimum date of Jan 1, 1970

走远了吗. 提交于 2019-11-30 19:07:17

Starting date of Android devices starts from Jan 1, 1970. Maybe this can be your case. Android calculates time as a number of milliseconds passed since Jan 1, 1970.

I've found a kind of hack for your case. Here I create dynamically datePicker:

     DatePicker dp = new DatePicker(this);
     dp.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
     v.addView(dp);

In the manifest file I declare a custom theme for my application - I want the same theme for the application. By the way you can do the same for activity.

 <application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" 
    android:theme="@style/CustomTheme">
    <activity
        android:name=".HelloDatePickerActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

In a styles.xml I do this:

<style name="CustomTheme" parent="android:Theme.Light">
    <item name="android:startYear">1890</item>
</style>

The default start date in my case is 1900. Thus, for me this approach works.

Hope this will help you!

I realize this question is a little old, and that this answer will not apply to older devices, but I've recently been made aware of this issue on a newer device (the Huawei Mediapad 7 Youth), so I figured I'd update this with newer information:

Starting with API 11, the methods necessary to do this are exposed. Remember, while Calendar may use Unix Epoch as zero, Java (at least until ~JRE 8?) uses signed numbers, so negatives are completely acceptable. Thankfully you don't need to be fully aware of this, as this code runs fine:

// This is API 11!
try {
    // Hack to (try to) force minDate to 1888 instead of 1970.
    DatePicker dp = dialog.getDatePicker();
    Calendar minCal = Calendar.getInstance();
    minCal.set(1888, Calendar.JANUARY, 1, 0, 0, 0);
    dp.setMinDate(minCal.getTimeInMillis());

    if (maxDate > 0)
        dp.setMaxDate(maxDate);
} catch (NoSuchMethodError e) {
    Log.w(TAG, "Can't set min/max dates: device/Android version is too old.");
}

I used 1888 so that any reports stating this is the minimum will make it clear that the code was executed and not ignored/overridden. I should note I have not yet heard back whether or not this works on the reported device.

Hi I found the solution for it. you can set the -ve value in the setMinDate() method and it works for me.

dp.setMinDate(-1576800000000L);

I see you already tried something similar, but have you tried setting the following:

android:startYear="1900"

directly on the DatePicker XML?

Like so:

<DatePicker
          android:startYear="1900"
          android:endYear="2100"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
></DatePicker>

Reference: http://kevsaidwhat.blogspot.com/2011/10/fun-with-android-datepicker-and-1970.html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!