diff --git a/android/src/main/java/com/henninghall/date_picker/PickerView.java b/android/src/main/java/com/henninghall/date_picker/PickerView.java index 9f46ad0..327b446 100644 --- a/android/src/main/java/com/henninghall/date_picker/PickerView.java +++ b/android/src/main/java/com/henninghall/date_picker/PickerView.java @@ -75,12 +75,11 @@ public class PickerView extends RelativeLayout { minutesWheel = new MinutesWheel( this, R.id.minutes); ampmWheel = new AmPmWheel(this, R.id.ampm); hourWheel = new HourWheel(this, R.id.hour); - dateFormat = new SimpleDateFormat(getDateFormatTemplate(), Locale.US); + setDateFormat(); changeAmPmWhenPassingMidnightOrNoon(); } - private final Runnable measureAndLayout = new Runnable() { @Override public void run() { @@ -95,7 +94,7 @@ public class PickerView extends RelativeLayout { hourWheel.picker.setOnValueChangeListenerInScrolling(new NumberPickerView.OnValueChangeListenerInScrolling() { @Override public void onValueChangeInScrolling(NumberPickerView picker, int oldVal, int newVal) { - if(Utils.usesAmPm(locale)){ + if(Settings.usesAmPm()){ String oldValue = hourWheel.getValueAtIndex(oldVal); String newValue = hourWheel.getValueAtIndex(newVal); boolean passingNoonOrMidnight = (oldValue.equals("12") && newValue.equals("11")) || oldValue.equals("11") && newValue.equals("12"); @@ -115,14 +114,15 @@ public class PickerView extends RelativeLayout { requireDisplayValueUpdate = true; } - public void setDate(String isoDate) { + public void f(String isoDate) { Calendar cal = Utils.isoToCalendar(isoDate, timeZone); applyOnAllWheels(new SetDate(cal)); + update2DigitYearStart(cal); } public void setLocale(Locale locale) { this.locale = locale; - dateFormat = new SimpleDateFormat(getDateFormatTemplate(), Locale.US); + setDateFormat(); wheelOrderUpdater.update(locale, mode); requireDisplayValueUpdate = true; } @@ -172,7 +172,7 @@ public class PickerView extends RelativeLayout { public void setMode(Mode mode) { this.mode = mode; - dateFormat = new SimpleDateFormat(getDateFormatTemplate(), Locale.US); + setDateFormat(); applyOnAllWheels(new UpdateVisibility()); wheelOrderUpdater.update(locale, mode); } @@ -226,4 +226,13 @@ public class PickerView extends RelativeLayout { if (maxDate == null) return null; return maxDate.get(); } + public void setDateFormat(){ + dateFormat = new SimpleDateFormat(getDateFormatTemplate(), Locale.US); + } + public void update2DigitYearStart(Calendar selectedDate){ + Calendar cal = (Calendar) selectedDate.clone(); + cal.add(Calendar.YEAR, -50); // subtract 50 years to hit the middle of the century + dateFormat.set2DigitYearStart(cal.getTime()); + } + } diff --git a/android/src/main/java/com/henninghall/date_picker/Settings.java b/android/src/main/java/com/henninghall/date_picker/Settings.java new file mode 100644 index 0000000..52c53ad --- /dev/null +++ b/android/src/main/java/com/henninghall/date_picker/Settings.java @@ -0,0 +1,12 @@ +package com.henninghall.date_picker; + + +import android.text.format.DateFormat; + +public class Settings { + + public static boolean usesAmPm (){ + return !DateFormat.is24HourFormat(DatePickerManager.context); + } + +} diff --git a/android/src/main/java/com/henninghall/date_picker/Utils.java b/android/src/main/java/com/henninghall/date_picker/Utils.java index efc090a..57c826b 100644 --- a/android/src/main/java/com/henninghall/date_picker/Utils.java +++ b/android/src/main/java/com/henninghall/date_picker/Utils.java @@ -18,11 +18,6 @@ import java.util.TimeZone; public class Utils { - public static boolean usesAmPm(Locale locale) { - DateFormat df = DateFormat.getTimeInstance(DateFormat.FULL, locale); - return df instanceof SimpleDateFormat && ((SimpleDateFormat) df).toPattern().contains("a"); - } - public static String printToday(Locale locale) { return PrettyTime.of(locale).printToday(); } diff --git a/android/src/main/java/com/henninghall/date_picker/wheels/AmPmWheel.java b/android/src/main/java/com/henninghall/date_picker/wheels/AmPmWheel.java index 8c6677b..7908e13 100644 --- a/android/src/main/java/com/henninghall/date_picker/wheels/AmPmWheel.java +++ b/android/src/main/java/com/henninghall/date_picker/wheels/AmPmWheel.java @@ -2,6 +2,7 @@ package com.henninghall.date_picker.wheels; import com.henninghall.date_picker.Mode; import com.henninghall.date_picker.PickerView; +import com.henninghall.date_picker.Settings; import com.henninghall.date_picker.Utils; import java.util.Calendar; import cn.carbswang.android.numberpickerview.library.NumberPickerView; @@ -34,12 +35,12 @@ public class AmPmWheel extends Wheel { @Override public boolean visible() { - return Utils.usesAmPm(pickerView.locale) && pickerView.mode != Mode.date; + return Settings.usesAmPm() && pickerView.mode != Mode.date; } @Override public String getFormatTemplate() { - return Utils.usesAmPm(pickerView.locale) ? " a " : ""; + return Settings.usesAmPm() ? " a " : ""; } } diff --git a/android/src/main/java/com/henninghall/date_picker/wheels/HourWheel.java b/android/src/main/java/com/henninghall/date_picker/wheels/HourWheel.java index 27504d3..5aa574e 100644 --- a/android/src/main/java/com/henninghall/date_picker/wheels/HourWheel.java +++ b/android/src/main/java/com/henninghall/date_picker/wheels/HourWheel.java @@ -2,9 +2,11 @@ package com.henninghall.date_picker.wheels; import com.henninghall.date_picker.Mode; import com.henninghall.date_picker.PickerView; +import com.henninghall.date_picker.Settings; import com.henninghall.date_picker.Utils; import java.util.Calendar; + import cn.carbswang.android.numberpickerview.library.NumberPickerView; @@ -16,7 +18,7 @@ public class HourWheel extends Wheel { @Override void init() { - int numberOfHours = Utils.usesAmPm(pickerView.locale) ? 12 : 24; + int numberOfHours = Settings.usesAmPm() ? 12 : 24; Calendar cal = pickerView.getInitialDate(); for(int i=0; i