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;
- /*
- 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
- /*
- 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 int distanceInDays(Calendar pC){
- return 0;
- }//distanceInDays
- public int distanceInDays(int y, int m, int d){
- return 0;
- }
- 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.appcompat.app.AppCompatActivity;
- 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 mEtYear, mEtMonth, mEtDay;
- Button mBtnComputeDistance;
- View.OnClickListener mClickHandler =
- new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- switch(v.getId()){
- case R.id.idBtnComputeDistance:
- actionComputeDistance();
- break;
- }//switch
- }//onClick
- };
- void actionComputeDistance(){
- int y,m,d;
- try {
- y = AmUtil.readIntFromEditText(mEtYear);
- m = AmUtil.readIntFromEditText(mEtMonth);
- d = AmUtil.readIntFromEditText(mEtDay);
- boolean bValidDate =
- AmCalendar.validDay(y,m,d);
- if (bValidDate){
- //instantiate and write a corresponding
- //AmCalendar object
- AmCalendar cUserDate =
- new AmCalendar(this, y,m,d);
- mTvResults.setText(cUserDate.toString());
- long lDays = cUserDate.distanceInDays();
- mTvResults.setText(
- cUserDate.toString());
- }
- else{
- //TODO
- mUtil.fb("Invalid Date!");
- }
- }
- 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_v1);
- init();
- }//onCreate
- void init(){
- mUtil = new AmUtil (this);
- mEtYear = findViewById(R.id.idEtYear);
- mEtMonth = findViewById(R.id.idEtMonth);
- mEtDay = findViewById(R.id.idEtDay);
- mBtnComputeDistance = findViewById(R.id.idBtnComputeDistance);
- String[] aDaysOfWeek =
- getResources().getStringArray
- (R.array.aDaysOfWeek);
- mTvResults = findViewById(R.id.idTvResults);
- 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
Add Comment
Please, Sign In to add comment