Advertisement
am_dot_com

DDM 2022-11-30

Nov 30th, 2022 (edited)
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.36 KB | None | 0 0
  1. package com.joythis.android.datesdistance;
  2.  
  3. import java.util.Calendar;
  4.  
  5. /*
  6. objetivo:
  7. - ajudar na matemática entre datas
  8. - fornecer frases correspondentes a datas
  9. */
  10. public class AmDate {
  11. public static int MES_IMPOSSIVEL = -1;
  12.  
  13. int mYear;
  14. int mMonth;
  15. int mDay;
  16.  
  17. /*
  18. default constructor
  19. vai representar a "data corrente"
  20. conforme o relógio do
  21. dispositivo móvel
  22. */
  23. public AmDate(){
  24. Calendar c =
  25. Calendar.getInstance();
  26.  
  27. int y = c.get(Calendar.YEAR);
  28. /*
  29. A Java Calendar is 0-based
  30. for the month
  31. 0 - January
  32. ...
  33. 10 - November
  34. 11 - December
  35. */
  36. int m = c.get(Calendar.MONTH);
  37. int d = c.get(Calendar.DAY_OF_MONTH);
  38. this.mYear = y;
  39. this.mMonth = m+1; // natural month
  40. this.mDay = d;
  41. }//AmDate
  42.  
  43. public String toString(){
  44. String s = String.format(
  45. "%4d-%2d-%2d",
  46. this.mYear,
  47. this.mMonth,
  48. this.mDay
  49. );
  50. return s;
  51. }//toString
  52.  
  53. public static boolean bValidYear(
  54. int pYear
  55. ){
  56. return pYear>=1;
  57. }//bValidYear
  58.  
  59. public static boolean bValidMonth(
  60. int pMonth
  61. ){
  62. return pMonth>=1 && pMonth<=12;
  63. }//bValidMonth
  64.  
  65. public static boolean bValidDate(
  66. int pY, int pM, int pD
  67. ){
  68. boolean bYearOK = bValidYear(pY);
  69. boolean bMonthOK = bValidMonth(pM);
  70. boolean bDayOK =
  71. pD>=1
  72. &&
  73. pD<=iHowManyDaysIn(pM, pY);
  74. return bYearOK && bMonthOK && bDayOK;
  75. }//bValidDate
  76.  
  77. public boolean leapYear(int pY){
  78. boolean bM400 = pY%400==0;
  79. boolean bM4NotSecular =
  80. pY%4==0 && pY%100!=0;
  81. return bM400 || bM4NotSecular;
  82. }//leapYear
  83.  
  84. public static int iHowManyDaysIn
  85. (int pM, int pY)
  86. {
  87. boolean bCase31 =
  88. pM==1 || pM==3 || pM==5 || pM==7
  89. || pM==8 || pM==10 || pM==12;
  90. if (bCase31) return 31;
  91. boolean bCase30 =
  92. pM==4 || pM==6 || pM==9 || pM==11;
  93. if(bCase30) return 30;
  94. if(pM==2)
  95. return leapYear(pY)?29:28;
  96. return MES_IMPOSSIVEL;
  97. }//iHowManyDaysIn
  98. }//AmDate
  99.  
  100.  
  101. ********************
  102. //MainActivity.java
  103. package com.joythis.android.datesdistance;
  104.  
  105. import androidx.appcompat.app.AppCompatActivity;
  106.  
  107. import android.app.Activity;
  108. import android.content.Intent;
  109. import android.os.Bundle;
  110. import android.view.View;
  111. import android.widget.Button;
  112. import android.widget.EditText;
  113.  
  114. public class MainActivity extends AppCompatActivity {
  115.  
  116. Activity mA;
  117. EditText mEtDate1, mEtDate2;
  118. Button mBtnPickDate1,
  119. mBtnPickDate2,
  120. mBtnComputeDistance;
  121.  
  122. View.OnClickListener mClickHandler = new View.OnClickListener() {
  123. @Override
  124. public void onClick(View v) {
  125. switch(v.getId()){
  126. case R.id.idBtnPickDate1:
  127. actionPickDate1();
  128. break;
  129. case R.id.idBtnPickDate2:
  130. actionPickDate2();
  131. break;
  132. }//switch
  133. }//onClick
  134. };//mClickHandler
  135.  
  136. void actionPickDate1(){
  137. //SAFR!!! TODO
  138. Intent goGoPickDate1 = new Intent(
  139. MainActivity.this,
  140. DateInput.class
  141. );
  142. startActivity(goGoPickDate1);
  143. }//actionPickDate1
  144.  
  145. void actionPickDate2(){
  146. //SAFR!!! TODO
  147. Intent goGoPickDate2 = new Intent(
  148. MainActivity.this,
  149. DateInput.class
  150. );
  151. startActivity(goGoPickDate2);
  152. }//actionPickDate2
  153.  
  154. @Override
  155. protected void onCreate(Bundle savedInstanceState) {
  156. super.onCreate(savedInstanceState);
  157. setContentView(R.layout.rl_distance_dates);
  158.  
  159. init();
  160. }//onCreate
  161.  
  162. void init(){
  163. //1 - assocs
  164. mEtDate1 = findViewById(R.id.idEtDate1);
  165. mEtDate2 = findViewById(R.id.idEtDate2);
  166. mBtnPickDate1 = findViewById(R.id.idBtnPickDate1);
  167. mBtnPickDate2 = findViewById(R.id.idBtnPickDate2);
  168. mBtnComputeDistance = findViewById(R.id.idBtnComputeDistance);
  169.  
  170. displayDefaultDates();
  171.  
  172. //2 - behaviors
  173. mBtnPickDate1.setOnClickListener(mClickHandler);
  174. mBtnPickDate2.setOnClickListener(mClickHandler);
  175.  
  176. }//init
  177.  
  178. void displayDefaultDates(){
  179. AmDate d = new AmDate();
  180. mEtDate1.setText(d.toString());
  181. mEtDate2.setText(d.toString());
  182. }//displayDefaultDates
  183. }
  184. ********************************
  185.  
  186. <?xml version="1.0" encoding="utf-8"?>
  187. <!-- rl_pick_date.xml -->
  188. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  189. android:layout_width="match_parent"
  190. android:layout_height="match_parent">
  191.  
  192. <LinearLayout
  193. android:id="@+id/idLlLabels"
  194. android:orientation="horizontal"
  195. android:layout_width="match_parent"
  196. android:layout_height="wrap_content">
  197.  
  198. <TextView
  199. android:gravity="center_horizontal"
  200. android:layout_weight="1"
  201. android:text="@string/strTvYear"
  202. android:layout_width="match_parent"
  203. android:layout_height="wrap_content"/>
  204. <TextView
  205. android:gravity="center_horizontal"
  206. android:layout_weight="1"
  207. android:text="@string/strTvMonth"
  208. android:layout_width="match_parent"
  209. android:layout_height="wrap_content"/>
  210. <TextView
  211. android:gravity="center_horizontal"
  212. android:layout_weight="1"
  213. android:text="@string/strTvDay"
  214. android:layout_width="match_parent"
  215. android:layout_height="wrap_content"/>
  216. </LinearLayout>
  217.  
  218. <LinearLayout
  219. android:id="@+id/idLlInputs"
  220. android:layout_below="@id/idLlLabels"
  221. android:orientation="horizontal"
  222. android:layout_width="match_parent"
  223. android:layout_height="wrap_content">
  224. <NumberPicker
  225. android:layout_weight="1"
  226. android:id="@+id/idNpYear"
  227. android:layout_width="match_parent"
  228. android:layout_height="wrap_content"/>
  229. <NumberPicker
  230. android:layout_weight="1"
  231. android:id="@+id/idNpMonth"
  232. android:layout_width="match_parent"
  233. android:layout_height="wrap_content"/>
  234. <NumberPicker
  235. android:layout_weight="1"
  236. android:id="@+id/idNpDay"
  237. android:layout_width="match_parent"
  238. android:layout_height="wrap_content"/>
  239. </LinearLayout>
  240.  
  241. <Button
  242. android:layout_below="@id/idLlInputs"
  243. android:id="@+id/idBtnConfirm"
  244. android:text="@string/strBtnConfirm"
  245. android:layout_width="match_parent"
  246. android:layout_height="wrap_content"/>
  247.  
  248. <Button
  249. android:layout_below="@id/idBtnConfirm"
  250. android:id="@+id/idBtnCancel"
  251. android:text="@string/strBtnCancel"
  252. android:layout_width="match_parent"
  253. android:layout_height="wrap_content"/>
  254.  
  255. </RelativeLayout>
  256.  
  257. ****************************************************************
  258. //DateInput.java
  259. package com.joythis.android.datesdistance;
  260.  
  261. import androidx.appcompat.app.AppCompatActivity;
  262.  
  263. import android.app.Activity;
  264. import android.os.Bundle;
  265. import android.widget.Button;
  266. import android.widget.NumberPicker;
  267.  
  268. import java.util.Calendar;
  269.  
  270. public class DateInput extends AppCompatActivity {
  271.  
  272. Activity mA;
  273. NumberPicker mNpYear, mNpMonth, mNpDay;
  274. Button mBtnConfirm, mBtnCancel;
  275.  
  276. NumberPicker.OnValueChangeListener mNpValueHandler =
  277. new NumberPicker.OnValueChangeListener() {
  278. @Override
  279. public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
  280. int y = mNpYear.getValue();
  281. int m = mNpMonth.getValue();
  282. int maxDay = AmDate.iHowManyDaysIn(y, m);
  283. mNpDay.setMaxValue(maxDay);
  284. }//onValueChange
  285. };//mNpValueHandler
  286.  
  287. @Override
  288. protected void onCreate(Bundle savedInstanceState) {
  289. super.onCreate(savedInstanceState);
  290. setContentView(R.layout.rl_pick_date);
  291.  
  292. init();
  293. }//onCreate
  294.  
  295. void initNpDate(){
  296. int iCurrentYear = Calendar.getInstance().get(Calendar.YEAR);
  297. mNpYear.setMinValue(iCurrentYear-400);
  298. mNpYear.setMaxValue(iCurrentYear+100);
  299. mNpYear.setValue(iCurrentYear);
  300.  
  301. int iCurrentMonth = Calendar.getInstance().get(Calendar.MONTH)+1;
  302. mNpMonth.setMinValue(1);
  303. mNpMonth.setMaxValue(12);
  304. mNpMonth.setValue(iCurrentMonth);
  305.  
  306. int iCurrentDay = Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
  307. mNpDay.setMinValue(1);
  308. mNpDay.setMaxValue(AmDate.iHowManyDaysIn(iCurrentYear, iCurrentMonth));
  309. mNpDay.setValue(iCurrentDay);
  310. }//initNpDate
  311.  
  312. void init(){
  313. //1 - assocs
  314. mNpYear = findViewById(R.id.idNpYear);
  315. mNpMonth = findViewById(R.id.idNpMonth);
  316. mNpDay = findViewById(R.id.idNpDay);
  317.  
  318. NumberPicker[] nps = {mNpYear, mNpMonth, mNpDay};
  319. for (NumberPicker np : nps){
  320. np.setOnValueChangedListener(mNpValueHandler);
  321. }//for
  322.  
  323. initNpDate();
  324. //2 - behaviors
  325. }//init
  326. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement