Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.26 KB | None | 0 0
  1. private Pattern pattern;
  2. private Matcher matcher;
  3.  
  4. private static final String DATE_PATTERN =
  5. "(0?[1-9]|1[012]) [/.-] (0?[1-9]|[12][0-9]|3[01]) [/.-] ((19|20)\d\d)";
  6.  
  7.  
  8. /**
  9. * Validate date format with regular expression
  10. * @param date date address for validation
  11. * @return true valid date format, false invalid date format
  12. */
  13. public boolean validate(final String date){
  14.  
  15. matcher = pattern.matcher(date);
  16.  
  17. if(matcher.matches()){
  18. matcher.reset();
  19.  
  20. if(matcher.find()){
  21. String day = matcher.group(1);
  22. String month = matcher.group(2);
  23. int year = Integer.parseInt(matcher.group(3));
  24.  
  25. if (day.equals("31") &&
  26. (month.equals("4") || month .equals("6") || month.equals("9") ||
  27. month.equals("11") || month.equals("04") || month .equals("06") ||
  28. month.equals("09"))) {
  29. return false; // only 1,3,5,7,8,10,12 has 31 days
  30. }
  31. }
  32.  
  33. else{
  34. return false;
  35. }
  36. }
  37. else{
  38. return false;
  39. }
  40. }
  41.  
  42. private Pattern pattern;
  43. private Matcher matcher;
  44.  
  45. private static final String DATE_PATTERN =
  46. "(0?[1-9]|1[012]) [/.-] (0?[1-9]|[12][0-9]|3[01]) [/.-] ((19|20)\d\d)";
  47.  
  48.  
  49. /**
  50. * Validate date format with regular expression
  51. * @param date date address for validation
  52. * @return true valid date format, false invalid date format
  53. */
  54. public boolean validate(final String date){
  55.  
  56. matcher = pattern.matcher(date);
  57.  
  58. if(matcher.matches()){
  59. matcher.reset();
  60.  
  61. if(matcher.find()){
  62. String day = matcher.group(1);
  63. String month = matcher.group(2);
  64. int year = Integer.parseInt(matcher.group(3));
  65.  
  66. if (day.equals("31") &&
  67. (month.equals("4") || month .equals("6") || month.equals("9") ||
  68. month.equals("11") || month.equals("04") || month .equals("06") ||
  69. month.equals("09"))) {
  70. return false; // only 1,3,5,7,8,10,12 has 31 days
  71. }
  72.  
  73. else if (month.equals("2") || month.equals("02")) {
  74. //leap year
  75. if(year % 4==0){
  76. if(day.equals("30") || day.equals("31")){
  77. return false;
  78. }
  79. else{
  80. return true;
  81. }
  82. }
  83. else{
  84. if(day.equals("29")||day.equals("30")||day.equals("31")){
  85. return false;
  86. }
  87. else{
  88. return true;
  89. }
  90. }
  91. }
  92.  
  93. else{
  94. return true;
  95. }
  96. }
  97.  
  98. else{
  99. return false;
  100. }
  101. }
  102. else{
  103. return false;
  104. }
  105. }
  106.  
  107. matcher = Pattern.compile(DATE_PATTERN).matcher(Birthday);
  108.  
  109. //Birthday validator
  110. else if (!matcher.matches()) {
  111. Toast.makeText(getApplicationContext(), "Invalid Birthday!", Toast.LENGTH_SHORt).show();
  112. }
  113.  
  114. public static final SimpleDateFormat BIRTHDAY_FORMAT_PARSER = new SimpleDateFormat("yyyy-MM-dd");
  115. public static final String DASH_STRING = "-";
  116. public static Calendar parseDateString(String date) {
  117. Calendar calendar = Calendar.getInstance();
  118. BIRTHDAY_FORMAT_PARSER.setLenient(false);
  119. try {
  120. calendar.setTime(BIRTHDAY_FORMAT_PARSER.parse(date));
  121. } catch (ParseException e) {}
  122. return calendar;
  123. }
  124. public static boolean isValidBirthday(String birthday) {
  125. Calendar calendar = parseDateString(birthday);
  126. int year = calendar.get(Calendar.YEAR);
  127. int thisYear = Calendar.getInstance().get(Calendar.YEAR);
  128. return year >= 1900 && year < thisYear;
  129. }
  130.  
  131. private void setupBirthdayEditText() {
  132. mBirthdayEditText.addTextChangedListener(new TextWatcher() {
  133. int beforeTextChangedLength;
  134. @Override
  135. public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
  136. beforeTextChangedLength = charSequence.length();
  137. }
  138.  
  139. @Override
  140. public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
  141. if (!TextUtils.isEmpty(mBirthdayTextInputLayout.getError())) {
  142. mBirthdayTextInputLayout.setError(null);
  143. mBirthdayTextInputLayout.setErrorEnabled(false);
  144. }
  145. }
  146.  
  147. @Override
  148. public void afterTextChanged(Editable editable) {
  149. int length = editable.length();
  150. // text is being removed
  151. if (beforeTextChangedLength > length) return;
  152.  
  153. String str = editable.toString();
  154. String[] strArr = str.split(Utils.DASH_STRING);
  155. // only add dash after input year with zero dash and input month with one dash
  156. if ((length == YEAR_LENGTH && strArr.length == 1) || (length == YEAR_MONTH_LENGTH && strArr.length == 2)) {
  157. mBirthdayEditText.setText(str + Utils.DASH_STRING);
  158. mBirthdayEditText.setSelection(mBirthdayEditText.length());
  159. }
  160. }
  161. });
  162. }
  163.  
  164. protected boolean isAllFieldsValid() {
  165. String birthday = mBirthdayEditText.getText().toString().trim();
  166. if (!Utils.isValidBirthday(birthday)) {
  167. mBirthdayTextInputLayout.setError(getString(R.string.error_birth_date));
  168. return false;
  169. }
  170.  
  171. return true;
  172. }
  173.  
  174. <android.support.design.widget.TextInputLayout
  175. android:id="@+id/register_birthday_textinput_layout"
  176. android:layout_width="match_parent"
  177. android:layout_height="wrap_content"
  178. app:errorTextAppearance="@style/ErrorTextInputLayout"
  179. >
  180.  
  181. <android.support.design.widget.TextInputEditText
  182. android:id="@+id/register_birthday_edittext"
  183. android:hint="@string/birth_date"
  184. android:backgroundTint="@color/white_12"
  185. android:inputType="date"
  186. style="@style/BaseEditText"
  187. android:digits="1234567890"
  188. android:maxLength="10"
  189. />
  190.  
  191. </android.support.design.widget.TextInputLayout>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement