am_dot_com

DDM 2021-11-24

Nov 24th, 2021 (edited)
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.75 KB | None | 0 0
  1. package com.joythis.android.distanceindays;
  2.  
  3. import android.app.Activity;
  4.  
  5. import java.util.Calendar;
  6.  
  7. public class AmCalendar {
  8. Activity mActivity; //to bridge to Activity tools
  9. /*
  10. Calendar objects represent
  11. month as 0-based data
  12. days of week as 1-based data
  13. */
  14. private Calendar mCalendar;
  15. private int mYear; //2021
  16. private int mMonth; //11
  17. private int mDay; //10
  18.  
  19. private int mAbsDayInYear;
  20. private int mDayOfWeek;
  21.  
  22. public Calendar getmCalendar() {
  23. return mCalendar;
  24. }
  25.  
  26. public int getmYear() {
  27. return mYear;
  28. }
  29.  
  30. public int getmMonth() {
  31. return mMonth;
  32. }
  33.  
  34. public int getmDay() {
  35. return mDay;
  36. }
  37.  
  38. public int getmAbsDayInYear() {
  39. return mAbsDayInYear;
  40. }
  41.  
  42. public int getmDayOfWeek() {
  43. return mDayOfWeek;
  44. }
  45.  
  46. /*
  47. constructs an AmCalendar object
  48. corresponding to the current date
  49. default constructor - the constructor without
  50. arguments
  51. */
  52. public AmCalendar(Activity pA){
  53. this.mActivity = pA;
  54. this.mCalendar = Calendar.getInstance();
  55. this.mYear = this.mCalendar.get(Calendar.YEAR);
  56. this.mMonth =
  57. 1+this.mCalendar.get(Calendar.MONTH);
  58. this.mDay = this.mCalendar.get(Calendar.DATE);
  59. this.mAbsDayInYear =
  60. this.mCalendar.get(Calendar.DAY_OF_YEAR);
  61. this.mDayOfWeek =
  62. this.mCalendar.get(Calendar.DAY_OF_WEEK);
  63. }//AmCalendar
  64.  
  65. public AmCalendar(
  66. Activity pA,
  67. int pYear,
  68. int pMonth, //natural
  69. int pDay
  70. ){
  71. this.mActivity = pA;
  72.  
  73. this.mCalendar = Calendar.getInstance();
  74. this.mCalendar.clear();
  75. this.mCalendar.set(
  76. Calendar.YEAR,
  77. pYear
  78. );
  79. //because Calendar's month are 0-based
  80. //after having received a "natural" month
  81. //we must discount it 1
  82. this.mCalendar.set(
  83. Calendar.MONTH,
  84. pMonth-1
  85. );
  86. this.mCalendar.set(
  87. Calendar.DATE,
  88. pDay
  89. );
  90. this.mYear = pYear;
  91. this.mMonth = pMonth;
  92. this.mDay = pDay;
  93. this.mAbsDayInYear = this.mCalendar.get(
  94. Calendar.DAY_OF_YEAR
  95. );
  96. //1 - Sunday, 2 - Monday ... 7 - Saturday
  97. this.mDayOfWeek = this.mCalendar.get(
  98. Calendar.DAY_OF_WEEK
  99. );
  100.  
  101. this.mDayOfWeek = this.mDayOfWeek;
  102. }//AmCalendar
  103.  
  104. public AmCalendar (Activity pA, String pStrYMD){
  105. this.mActivity = pA;
  106. String[] aParts = pStrYMD.split("-");
  107. boolean bCorrectNumberOfParts =
  108. aParts.length == 3;
  109. int y=0,m=0,d=0;
  110. this.mCalendar = Calendar.getInstance();
  111.  
  112. if (bCorrectNumberOfParts){
  113. y = Integer.parseInt(aParts[0]);
  114. m = Integer.parseInt(aParts[1]);
  115. d = Integer.parseInt(aParts[2]);
  116.  
  117. this.mCalendar.clear();
  118. this.mYear=y;
  119. this.mMonth=m;
  120. this.mDay=d;
  121.  
  122. this.mCalendar.set(Calendar.YEAR, y);
  123. this.mCalendar.set(Calendar.MONTH, m-1);
  124. this.mCalendar.set(Calendar.DATE, d);
  125. }//if
  126. else {
  127. //if not possible to extract y,m,d from String
  128. this.mYear = this.mCalendar.get(Calendar.YEAR);
  129. this.mMonth = this.mCalendar.get(Calendar.MONTH) + 1;
  130. this.mDay = this.mCalendar.get(Calendar.DATE);
  131. }
  132. //disaster or not
  133. this.mAbsDayInYear=
  134. this.mCalendar.get(Calendar.DAY_OF_YEAR);
  135. this.mDayOfWeek=
  136. this.mCalendar.get(Calendar.DAY_OF_WEEK);
  137. }//AmCalendar
  138.  
  139. /*
  140. computes the distance, in days,
  141. relatively to the device's current date
  142. */
  143. public long distanceInDays(){
  144. /*
  145. One needs to compute the difference
  146. between 2 moments
  147. moments represented as Calendar objects
  148. one of the 2 moments is the user's date
  149. the other moment is the system's date
  150. BOTH can be represented as Calendar objects
  151. All Calendar objects provide a method named
  152. getTimeInMillis
  153. which computes the time in milliseconds
  154. since the "Epoch" time (1970-1-1)
  155. So, the subtraction of these ints (long)
  156. gives the wanted number (in ms)
  157. */
  158. Calendar cNow = Calendar.getInstance();
  159. long lNow = cNow.getTimeInMillis();
  160.  
  161. long lUser = this.mCalendar.getTimeInMillis();
  162.  
  163. long lDiffMs = Math.abs(lNow-lUser);
  164. long lDiffDays = lDiffMs/1000/60/60/24;
  165. return lDiffDays;
  166. }//distanceInDays
  167.  
  168. /*
  169. computes the distance, in days, relatively
  170. to a given date (as a Calendar object)
  171. */
  172. public long distanceInDays(Calendar pC){
  173. long lDiffMS =
  174. Math.abs(pC.getTimeInMillis()-this.mCalendar.getTimeInMillis());
  175. long lDays = lDiffMS/1000/60/60/24;
  176. return lDays;
  177. }//distanceInDays
  178.  
  179.  
  180. public long distanceInDays(int y, int m, int d){
  181. Calendar c = Calendar.getInstance();
  182. c.clear();
  183. c.set(Calendar.YEAR, y);
  184. c.set(Calendar.MONTH, m-1);
  185. c.set(Calendar.DATE, d);
  186. long lDiffMS =
  187. Math.abs(this.mCalendar.getTimeInMillis() - c.getTimeInMillis());
  188. long lDays = lDiffMS/1000/60/60/24;
  189. return lDays;
  190. }//distanceInDays
  191.  
  192. public String toString(){
  193. String strRet = "";
  194.  
  195. String strDayOfWeek = "";
  196. /* 0=>"sunday", ... 6=>"saturday"*/
  197. String[] dow = this.mActivity.getResources().
  198. getStringArray(R.array.aDaysOfWeek);
  199. /* 1=>"sunday" .. 7=>"saturday"*/
  200. strDayOfWeek = dow[this.mDayOfWeek-1];
  201.  
  202. strRet = String.format(
  203. "%d-%d-%d #%d (%d = %s) => %d day(s) distant",
  204. this.mYear,
  205. this.mMonth,
  206. this.mDay,
  207. this.mAbsDayInYear,
  208. this.mDayOfWeek,
  209. strDayOfWeek,
  210. distanceInDays()
  211. );
  212.  
  213. return strRet;
  214. }//toString
  215.  
  216. public static boolean validYear(int pYear){
  217. return pYear>=1;
  218. }//validYear
  219.  
  220. public static boolean validMonth(int pMonth){
  221. return pMonth>=1 && pMonth<=12;
  222. }//validMonth
  223.  
  224. public static boolean validDay(
  225. int pY, int pM, int pD
  226. ){
  227. boolean bY = validYear(pY);
  228. boolean bM = validMonth(pM);
  229. if (bY && bM)
  230. return pD>=1 && pD<=daysInMonth(pY, pM);
  231. return false;
  232. }//validDay
  233.  
  234. public final static int IMPOSSIBLE_MONTH = -1;
  235. public static int daysInMonth(int pY, int pM){
  236. switch(pM){
  237. case 1:
  238. case 3:
  239. case 5:
  240. case 7:
  241. case 8:
  242. case 10:
  243. case 12:
  244. return 31;
  245. case 4:
  246. case 6:
  247. case 9:
  248. case 11:
  249. return 30;
  250. case 2:
  251. return leapYear(pY) ? 29:28;
  252. default:
  253. return IMPOSSIBLE_MONTH;
  254. }//switch
  255. }//daysInMonth
  256.  
  257. public static boolean leapYear(int pY){
  258. boolean bM4 = pY%4==0;
  259. boolean bM100 = pY%100==0;
  260. boolean bM400 = pY%400==0;
  261.  
  262. return bM400 || (bM4 && !bM100);
  263. }//leapYear
  264. }//AmCalendar
  265.  
  266.  
  267. *****************************************************
  268.  
  269. package com.joythis.android.distanceindays;
  270.  
  271. import androidx.annotation.Nullable;
  272. import androidx.appcompat.app.AppCompatActivity;
  273.  
  274. import android.content.Intent;
  275. import android.os.Bundle;
  276. import android.view.View;
  277. import android.widget.Button;
  278. import android.widget.EditText;
  279. import android.widget.TextView;
  280.  
  281. import java.util.Calendar;
  282.  
  283. public class MainActivity extends AppCompatActivity {
  284. AmUtil mUtil;
  285. TextView mTvResults;
  286. AmCalendar mCalendar;
  287. EditText mEtDate1, mEtDate2;
  288. Button mBtnComputeDistance, mBtnRequestDate1, mBtnRequestDate2;
  289.  
  290. View.OnClickListener mClickHandler = new View.OnClickListener() {
  291. @Override
  292. public void onClick(View v) {
  293. switch(v.getId()){
  294. case R.id.idBtnComputeDistance:
  295. actionComputeDistance();
  296. break;
  297. case R.id.idBtnRequestDate1:
  298. actionRequestDate1();
  299. break;
  300. case R.id.idBtnRequestDate2:
  301. actionRequestDate2();
  302. break;
  303. }//switch
  304. }//onClick
  305. };
  306.  
  307. public final static int CALL_ME_ON_THIS_REQUEST_CODE_WHEN_POSSIBLE = 123;
  308. void actionRequestDate1(){
  309. Intent goInputDate = new Intent(
  310. this,
  311. InputDateActivity.class
  312. );
  313. goInputDate.putExtra("DATE", 1);
  314. //startActivity(goInputDate);
  315.  
  316. startActivityForResult(
  317. goInputDate,
  318. CALL_ME_ON_THIS_REQUEST_CODE_WHEN_POSSIBLE
  319. );
  320. }//actionRequestDate1
  321.  
  322. @Override
  323. protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
  324. super.onActivityResult(requestCode, resultCode, data);
  325.  
  326. boolean bConfirm, bCancel;
  327. bConfirm = resultCode == RESULT_OK;
  328. bCancel = resultCode == RESULT_CANCELED;
  329. if (bConfirm) {
  330. if(requestCode==CALL_ME_ON_THIS_REQUEST_CODE_WHEN_POSSIBLE){
  331. //unpacking of the result responded by called Activity
  332. int y, m, d;
  333. y = data.getIntExtra(
  334. InputDateActivity.KEY_RESULT_YEAR,
  335. //Calendar.getInstance().get(Calendar.YEAR)
  336. -1
  337. );
  338. m = data.getIntExtra(
  339. "RESULT_MONTH",
  340. -1
  341. );
  342. d = data.getIntExtra(
  343. "RESULT_DAY",
  344. -1
  345. );
  346.  
  347. int iDate = data.getIntExtra(
  348. "RESULT_DATE",
  349. -1
  350. );
  351.  
  352. String strDate = data.getStringExtra(
  353. "STR_DATE"
  354. );
  355.  
  356. boolean bProblem = y==-1 || m==-1 || d==-1 || iDate==-1;
  357. if (bProblem){
  358. mUtil.fb
  359. ("Unacceptable date component received, nothing done");
  360. return;
  361. }//bProblem
  362.  
  363. String strDateRecv =
  364. String.format("%d-%d-%d", y, m, d);
  365. switch(iDate){
  366. case 1:
  367. mEtDate1.setText(strDate);
  368. //ALT
  369. mEtDate1.setText(strDateRecv);
  370. break;
  371. case 2:
  372. mEtDate2.setText(strDate);
  373. //ALT
  374. mEtDate2.setText(strDateRecv);
  375. break;
  376. }
  377.  
  378.  
  379. }
  380. }
  381.  
  382. if (bCancel){
  383. mUtil.fb("Date selection canceled");
  384. }
  385. }
  386.  
  387. void actionRequestDate2(){
  388. Intent goInputDate = new Intent(
  389. this,
  390. InputDateActivity.class
  391. );
  392. goInputDate.putExtra("DATE", 2);
  393. //startActivity(goInputDate);
  394.  
  395. startActivityForResult(
  396. goInputDate,
  397. CALL_ME_ON_THIS_REQUEST_CODE_WHEN_POSSIBLE
  398. );
  399. }
  400.  
  401. void actionComputeDistance(){
  402. int y,m,d;
  403. try {
  404. AmCalendar calendarCorrespondingToD1 =
  405. new AmCalendar(this, mEtDate1.getText().toString());
  406. AmCalendar calendarCorrespondingToD2 =
  407. new AmCalendar(this, mEtDate2.getText().toString());
  408.  
  409. long lDistanceInDays =
  410. calendarCorrespondingToD1.distanceInDays(
  411. calendarCorrespondingToD2.getmCalendar()
  412. );
  413.  
  414. String strD1 = calendarCorrespondingToD1.toString();
  415. String strD2 = calendarCorrespondingToD2.toString();
  416.  
  417. //TODO show the result
  418. String strResult = String.format(
  419. "The distance in days between:\n"+
  420. "%s\n"+
  421. "%s\n"+
  422. "is %d day(s).",
  423. strD1,
  424. strD2,
  425. lDistanceInDays
  426. );
  427.  
  428. //String strAll=strResult +"\n" + mTvResults.getText().toString();
  429. //mTvResults.setText(strAll);
  430. mTvResults.setText(strResult);
  431. }
  432. catch (Exception e){
  433. //TODO
  434. mUtil.fb("Empty field.");
  435. }
  436.  
  437. }//actionComputeDistance
  438.  
  439. @Override
  440. protected void onCreate(Bundle savedInstanceState) {
  441. super.onCreate(savedInstanceState);
  442. setContentView(R.layout.rl_distance_in_days_v2);
  443.  
  444. init();
  445. }//onCreate
  446.  
  447. void init(){
  448. mUtil = new AmUtil (this);
  449. mEtDate1 = findViewById(R.id.idEtDate1);
  450. mEtDate2 = findViewById(R.id.idEtDate2);
  451. mBtnRequestDate1 = findViewById(R.id.idBtnRequestDate1);
  452. mBtnRequestDate2 = findViewById(R.id.idBtnRequestDate2);
  453.  
  454. mBtnComputeDistance = findViewById(R.id.idBtnComputeDistance);
  455.  
  456. String[] aDaysOfWeek =
  457. getResources().getStringArray
  458. (R.array.aDaysOfWeek);
  459.  
  460. mTvResults = findViewById(R.id.idTvResults);
  461.  
  462. mBtnRequestDate1.setOnClickListener(mClickHandler);
  463. mBtnRequestDate2.setOnClickListener(mClickHandler);
  464.  
  465. mBtnComputeDistance.setOnClickListener(
  466. mClickHandler
  467. );
  468. displayCurrentYMD();
  469.  
  470. }//init
  471.  
  472. void displayCurrentYMD(){
  473. Calendar c = Calendar.getInstance();
  474. int y = c.get(Calendar.YEAR);
  475. int m = c.get(Calendar.MONTH)+1;
  476. int d = c.get(Calendar.DATE);
  477. /*
  478. mEtYear.setText(String.valueOf(y));
  479. mEtMonth.setText(String.valueOf(m));
  480. mEtDay.setText(String.valueOf(d));
  481.  
  482. */
  483. }//displayCurrentYMD
  484.  
  485. void auxTests(){
  486. //testing
  487. for (int day=1; day<=31; day+=1){
  488. AmCalendar calendarAtDay = new AmCalendar
  489. (MainActivity.this, 2021, 1, day);
  490. String strDay = calendarAtDay.toString();
  491. mTvResults.setText(
  492. strDay+"\n"+
  493. mTvResults.getText().toString()
  494. );
  495. }//for
  496. }//auxTests
  497.  
  498. /*
  499. EX1: on init make sure the EditTexts for
  500. year, month and day, are initialized to the
  501. device's current year, month and day
  502.  
  503. EX2: create and print an AmCalendar object,
  504. corresponding to the user date
  505. */
  506. }//MainActivity
Advertisement
Add Comment
Please, Sign In to add comment