DatePickerDialog incorrectly enforcing a minimum date of Jan 1, 1970

前端 未结 4 1719
星月不相逢
星月不相逢 2021-01-04 20:30

We have a member reporting that he is unable to set a date before Jan 1, 1970 on our DatePickerDialog. This issue does not repro for us.

I am already aware that the

相关标签:
4条回答
  • 2021-01-04 21:13

    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

    0 讨论(0)
  • 2021-01-04 21:25

    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.

    0 讨论(0)
  • 2021-01-04 21:26

    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!

    0 讨论(0)
  • 2021-01-04 21:31

    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);
    
    0 讨论(0)
提交回复
热议问题