9

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 DatePickerDialog does not expose the setMinDate/setMaxDate functions of the underlying DatePicker, so it would seem that some kind of handset maker-specific modification is affecting the minDate/maxDate.

This user reports he is running a Droid x2 on Verizon running 2.2 Froyo. While we believe he is correct in his description of his device model, many users are confused about the OS version, so he may be running 2.3.

I attempted to solve this problem by adding this theme to my Activity:

<style name="profile_editor_theme">     
    <item name="android:endYear">2025</item>
    <item name="android:startYear">1910</item>
</style>

While this theme on my activity had the intended effect of constraining the DatePickerDialog on my test devices (a Galaxy tab and an original Motorola Droid), it apparently had no effect for the user.

This issue repros for our user 100% of the time, but works correctly for us on our own devices.

Can anyone explain what might be causing this and how we could fix it?

I have filed this bug against Google on this matter.

Thanks!

esilver
  • 27,713
  • 23
  • 122
  • 168

4 Answers4

2

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.

edit: This is an old post. I think I did get confirmation but I don't remember at this point. Also, instead of try-catch this should likely check the API version instead. I've learned much since the days of this post. :)

Keilaron
  • 437
  • 4
  • 10
  • In my case made it simple val minDate = Calendar.getInstance().apply { this.set(Calendar.YEAR, 1900) }.timeInMillis datePicker.minDate = minDate – bene25 Mar 27 '23 at 11:23
2

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);
Manmohan Soni
  • 6,472
  • 2
  • 23
  • 29
2

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!

Yury
  • 20,618
  • 7
  • 58
  • 86
  • 3
    Just for reference, this is what's called "Unix Epoch" or "Unix Time", and this is just one of the reasons why I try to avoid using it as much as possible. More on the subject here: http://en.wikipedia.org/wiki/Unix_time – Telmo Marques Dec 23 '11 at 21:50
  • While yes, unixtime is frequently the method of time representation on devices, especially for "current" dates (like file modification times), that is not the case for a DatePicker, which has its own internal representation, as per this source here: http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/2.2_r1.1/android/widget/DatePicker.java#DatePicker – esilver Dec 23 '11 at 22:19
  • We've gone with this solution for now, will see how the marketplace responds :) – greg7gkb Feb 07 '12 at 21:43
0

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

Telmo Marques
  • 5,066
  • 1
  • 24
  • 34
  • We don't have any DatePicker XML. The DatePickerDialog is dynamically constructed. The DatePicker is itself allocated by the DatePickerDialog as per this source here: http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/2.2_r1.1/android/app/DatePickerDialog.java?av=f – esilver Dec 23 '11 at 22:14
  • I can't seem to find in the docs a way to programmatically set the `startDate` parameter, sorry... – Telmo Marques Dec 23 '11 at 22:26
  • Per these lines in the DatePicker source: private static final int DEFAULT_START_YEAR = 1900; I should not have to programmatically set it, and the fact that the Droid x2 was able to do so indicates there is a bug someplace.... – esilver Dec 23 '11 at 23:03