Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.joythis.android.distanceindays;
- import android.app.Activity;
- import java.util.Calendar;
- public class AmCalendar {
- Activity mActivity; //to bridge to Activity tools
- /*
- Calendar objects represent
- month as 0-based data
- days of week as 1-based data
- */
- private Calendar mCalendar;
- private int mYear; //2021
- private int mMonth; //11
- private int mDay; //10
- private int mAbsDayInYear;
- private int mDayOfWeek;
- public Calendar getmCalendar() {
- return mCalendar;
- }
- public int getmYear() {
- return mYear;
- }
- public int getmMonth() {
- return mMonth;
- }
- public int getmDay() {
- return mDay;
- }
- public int getmAbsDayInYear() {
- return mAbsDayInYear;
- }
- public int getmDayOfWeek() {
- return mDayOfWeek;
- }
- /*
- constructs an AmCalendar object
- corresponding to the current date
- default constructor - the constructor without
- arguments
- */
- public AmCalendar(Activity pA){
- this.mActivity = pA;
- this.mCalendar = Calendar.getInstance();
- this.mYear = this.mCalendar.get(Calendar.YEAR);
- this.mMonth =
- 1+this.mCalendar.get(Calendar.MONTH);
- this.mDay = this.mCalendar.get(Calendar.DATE);
- this.mAbsDayInYear =
- this.mCalendar.get(Calendar.DAY_OF_YEAR);
- this.mDayOfWeek =
- this.mCalendar.get(Calendar.DAY_OF_WEEK);
- }//AmCalendar
- public AmCalendar(
- Activity pA,
- int pYear,
- int pMonth, //natural
- int pDay
- ){
- this.mActivity = pA;
- this.mCalendar = Calendar.getInstance();
- this.mCalendar.clear();
- this.mCalendar.set(
- Calendar.YEAR,
- pYear
- );
- //because Calendar's month are 0-based
- //after having received a "natural" month
- //we must discount it 1
- this.mCalendar.set(
- Calendar.MONTH,
- pMonth-1
- );
- this.mCalendar.set(
- Calendar.DATE,
- pDay
- );
- this.mYear = pYear;
- this.mMonth = pMonth;
- this.mDay = pDay;
- this.mAbsDayInYear = this.mCalendar.get(
- Calendar.DAY_OF_YEAR
- );
- //1 - Sunday, 2 - Monday ... 7 - Saturday
- this.mDayOfWeek = this.mCalendar.get(
- Calendar.DAY_OF_WEEK
- );
- this.mDayOfWeek = this.mDayOfWeek;
- }//AmCalendar
- public AmCalendar (Activity pA, String pStrYMD){
- this.mActivity = pA;
- String[] aParts = pStrYMD.split("-");
- boolean bCorrectNumberOfParts =
- aParts.length == 3;
- int y=0,m=0,d=0;
- this.mCalendar = Calendar.getInstance();
- if (bCorrectNumberOfParts){
- y = Integer.parseInt(aParts[0]);
- m = Integer.parseInt(aParts[1]);
- d = Integer.parseInt(aParts[2]);
- this.mCalendar.clear();
- this.mYear=y;
- this.mMonth=m;
- this.mDay=d;
- this.mCalendar.set(Calendar.YEAR, y);
- this.mCalendar.set(Calendar.MONTH, m-1);
- this.mCalendar.set(Calendar.DATE, d);
- }//if
- else {
- //if not possible to extract y,m,d from String
- this.mYear = this.mCalendar.get(Calendar.YEAR);
- this.mMonth = this.mCalendar.get(Calendar.MONTH) + 1;
- this.mDay = this.mCalendar.get(Calendar.DATE);
- }
- //disaster or not
- this.mAbsDayInYear=
- this.mCalendar.get(Calendar.DAY_OF_YEAR);
- this.mDayOfWeek=
- this.mCalendar.get(Calendar.DAY_OF_WEEK);
- }//AmCalendar
- /*
- computes the distance, in days,
- relatively to the device's current date
- */
- public long distanceInDays(){
- /*
- One needs to compute the difference
- between 2 moments
- moments represented as Calendar objects
- one of the 2 moments is the user's date
- the other moment is the system's date
- BOTH can be represented as Calendar objects
- All Calendar objects provide a method named
- getTimeInMillis
- which computes the time in milliseconds
- since the "Epoch" time (1970-1-1)
- So, the subtraction of these ints (long)
- gives the wanted number (in ms)
- */
- Calendar cNow = Calendar.getInstance();
- long lNow = cNow.getTimeInMillis();
- long lUser = this.mCalendar.getTimeInMillis();
- long lDiffMs = Math.abs(lNow-lUser);
- long lDiffDays = lDiffMs/1000/60/60/24;
- return lDiffDays;
- }//distanceInDays
- /*
- computes the distance, in days, relatively
- to a given date (as a Calendar object)
- */
- public long distanceInDays(Calendar pC){
- long lDiffMS =
- Math.abs(pC.getTimeInMillis()-this.mCalendar.getTimeInMillis());
- long lDays = lDiffMS/1000/60/60/24;
- return lDays;
- }//distanceInDays
- public long distanceInDays(int y, int m, int d){
- Calendar c = Calendar.getInstance();
- c.clear();
- c.set(Calendar.YEAR, y);
- c.set(Calendar.MONTH, m-1);
- c.set(Calendar.DATE, d);
- long lDiffMS =
- Math.abs(this.mCalendar.getTimeInMillis() - c.getTimeInMillis());
- long lDays = lDiffMS/1000/60/60/24;
- return lDays;
- }//distanceInDays
- public String toString(){
- String strRet = "";
- String strDayOfWeek = "";
- /* 0=>"sunday", ... 6=>"saturday"*/
- String[] dow = this.mActivity.getResources().
- getStringArray(R.array.aDaysOfWeek);
- /* 1=>"sunday" .. 7=>"saturday"*/
- strDayOfWeek = dow[this.mDayOfWeek-1];
- strRet = String.format(
- "%d-%d-%d #%d (%d = %s) => %d day(s) distant",
- this.mYear,
- this.mMonth,
- this.mDay,
- this.mAbsDayInYear,
- this.mDayOfWeek,
- strDayOfWeek,
- distanceInDays()
- );
- return strRet;
- }//toString
- public static boolean validYear(int pYear){
- return pYear>=1;
- }//validYear
- public static boolean validMonth(int pMonth){
- return pMonth>=1 && pMonth<=12;
- }//validMonth
- public static boolean validDay(
- int pY, int pM, int pD
- ){
- boolean bY = validYear(pY);
- boolean bM = validMonth(pM);
- if (bY && bM)
- return pD>=1 && pD<=daysInMonth(pY, pM);
- return false;
- }//validDay
- public final static int IMPOSSIBLE_MONTH = -1;
- public static int daysInMonth(int pY, int pM){
- switch(pM){
- case 1:
- case 3:
- case 5:
- case 7:
- case 8:
- case 10:
- case 12:
- return 31;
- case 4:
- case 6:
- case 9:
- case 11:
- return 30;
- case 2:
- return leapYear(pY) ? 29:28;
- default:
- return IMPOSSIBLE_MONTH;
- }//switch
- }//daysInMonth
- public static boolean leapYear(int pY){
- boolean bM4 = pY%4==0;
- boolean bM100 = pY%100==0;
- boolean bM400 = pY%400==0;
- return bM400 || (bM4 && !bM100);
- }//leapYear
- }//AmCalendar
- *****************************************************
- package com.joythis.android.distanceindays;
- import androidx.annotation.Nullable;
- import androidx.appcompat.app.AppCompatActivity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.TextView;
- import java.util.Calendar;
- public class MainActivity extends AppCompatActivity {
- AmUtil mUtil;
- TextView mTvResults;
- AmCalendar mCalendar;
- EditText mEtDate1, mEtDate2;
- Button mBtnComputeDistance, mBtnRequestDate1, mBtnRequestDate2;
- View.OnClickListener mClickHandler = new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- switch(v.getId()){
- case R.id.idBtnComputeDistance:
- actionComputeDistance();
- break;
- case R.id.idBtnRequestDate1:
- actionRequestDate1();
- break;
- case R.id.idBtnRequestDate2:
- actionRequestDate2();
- break;
- }//switch
- }//onClick
- };
- public final static int CALL_ME_ON_THIS_REQUEST_CODE_WHEN_POSSIBLE = 123;
- void actionRequestDate1(){
- Intent goInputDate = new Intent(
- this,
- InputDateActivity.class
- );
- goInputDate.putExtra("DATE", 1);
- //startActivity(goInputDate);
- startActivityForResult(
- goInputDate,
- CALL_ME_ON_THIS_REQUEST_CODE_WHEN_POSSIBLE
- );
- }//actionRequestDate1
- @Override
- protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
- super.onActivityResult(requestCode, resultCode, data);
- boolean bConfirm, bCancel;
- bConfirm = resultCode == RESULT_OK;
- bCancel = resultCode == RESULT_CANCELED;
- if (bConfirm) {
- if(requestCode==CALL_ME_ON_THIS_REQUEST_CODE_WHEN_POSSIBLE){
- //unpacking of the result responded by called Activity
- int y, m, d;
- y = data.getIntExtra(
- InputDateActivity.KEY_RESULT_YEAR,
- //Calendar.getInstance().get(Calendar.YEAR)
- -1
- );
- m = data.getIntExtra(
- "RESULT_MONTH",
- -1
- );
- d = data.getIntExtra(
- "RESULT_DAY",
- -1
- );
- int iDate = data.getIntExtra(
- "RESULT_DATE",
- -1
- );
- String strDate = data.getStringExtra(
- "STR_DATE"
- );
- boolean bProblem = y==-1 || m==-1 || d==-1 || iDate==-1;
- if (bProblem){
- mUtil.fb
- ("Unacceptable date component received, nothing done");
- return;
- }//bProblem
- String strDateRecv =
- String.format("%d-%d-%d", y, m, d);
- switch(iDate){
- case 1:
- mEtDate1.setText(strDate);
- //ALT
- mEtDate1.setText(strDateRecv);
- break;
- case 2:
- mEtDate2.setText(strDate);
- //ALT
- mEtDate2.setText(strDateRecv);
- break;
- }
- }
- }
- if (bCancel){
- mUtil.fb("Date selection canceled");
- }
- }
- void actionRequestDate2(){
- Intent goInputDate = new Intent(
- this,
- InputDateActivity.class
- );
- goInputDate.putExtra("DATE", 2);
- //startActivity(goInputDate);
- startActivityForResult(
- goInputDate,
- CALL_ME_ON_THIS_REQUEST_CODE_WHEN_POSSIBLE
- );
- }
- void actionComputeDistance(){
- int y,m,d;
- try {
- AmCalendar calendarCorrespondingToD1 =
- new AmCalendar(this, mEtDate1.getText().toString());
- AmCalendar calendarCorrespondingToD2 =
- new AmCalendar(this, mEtDate2.getText().toString());
- long lDistanceInDays =
- calendarCorrespondingToD1.distanceInDays(
- calendarCorrespondingToD2.getmCalendar()
- );
- String strD1 = calendarCorrespondingToD1.toString();
- String strD2 = calendarCorrespondingToD2.toString();
- //TODO show the result
- String strResult = String.format(
- "The distance in days between:\n"+
- "%s\n"+
- "%s\n"+
- "is %d day(s).",
- strD1,
- strD2,
- lDistanceInDays
- );
- //String strAll=strResult +"\n" + mTvResults.getText().toString();
- //mTvResults.setText(strAll);
- mTvResults.setText(strResult);
- }
- catch (Exception e){
- //TODO
- mUtil.fb("Empty field.");
- }
- }//actionComputeDistance
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.rl_distance_in_days_v2);
- init();
- }//onCreate
- void init(){
- mUtil = new AmUtil (this);
- mEtDate1 = findViewById(R.id.idEtDate1);
- mEtDate2 = findViewById(R.id.idEtDate2);
- mBtnRequestDate1 = findViewById(R.id.idBtnRequestDate1);
- mBtnRequestDate2 = findViewById(R.id.idBtnRequestDate2);
- mBtnComputeDistance = findViewById(R.id.idBtnComputeDistance);
- String[] aDaysOfWeek =
- getResources().getStringArray
- (R.array.aDaysOfWeek);
- mTvResults = findViewById(R.id.idTvResults);
- mBtnRequestDate1.setOnClickListener(mClickHandler);
- mBtnRequestDate2.setOnClickListener(mClickHandler);
- mBtnComputeDistance.setOnClickListener(
- mClickHandler
- );
- displayCurrentYMD();
- }//init
- void displayCurrentYMD(){
- Calendar c = Calendar.getInstance();
- int y = c.get(Calendar.YEAR);
- int m = c.get(Calendar.MONTH)+1;
- int d = c.get(Calendar.DATE);
- /*
- mEtYear.setText(String.valueOf(y));
- mEtMonth.setText(String.valueOf(m));
- mEtDay.setText(String.valueOf(d));
- */
- }//displayCurrentYMD
- void auxTests(){
- //testing
- for (int day=1; day<=31; day+=1){
- AmCalendar calendarAtDay = new AmCalendar
- (MainActivity.this, 2021, 1, day);
- String strDay = calendarAtDay.toString();
- mTvResults.setText(
- strDay+"\n"+
- mTvResults.getText().toString()
- );
- }//for
- }//auxTests
- /*
- EX1: on init make sure the EditTexts for
- year, month and day, are initialized to the
- device's current year, month and day
- EX2: create and print an AmCalendar object,
- corresponding to the user date
- */
- }//MainActivity
Advertisement
Add Comment
Please, Sign In to add comment