0

I picked 27th jan 2023

This app calculates which week of the semester the selected date is. It works fine. But I need to add more functionality.

I picked 27th jan 2023 and I need to calculate amount of days passed to it from any chosen date. How to I execute this? Any tips and examples would be really helpful.

I know I need a loop to execute this, but I have no idea.

I have only my main page code:

package kz.talipovsn.calendar;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import android.util.TypedValue;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.CalendarView;
import android.widget.EditText;
import android.widget.Toast;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class MainActivity extends AppCompatActivity {

    final int maxSemesterWeek = 15; 

    final SimpleDateFormat sdf = new SimpleDateFormat("dd MMMM yyyy, EEEE"); 
    final Calendar calendar = Calendar.getInstance();

    SharedPreferences sp; 
    CalendarView cal;  
    EditText editText; 

    boolean offsetNumerator; 
    int displacementWeekSemester; 

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        sp = PreferenceManager.getDefaultSharedPreferences(this);

        editText = findViewById(R.id.editText);

        cal = findViewById(R.id.calendarView);
        cal.setFirstDayOfWeek(2); 

        calendar.setFirstDayOfWeek(Calendar.MONDAY); 

        readSettings(); 

        cal.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {

            @Override
            public void onSelectedDayChange(CalendarView view, int year, int month,
                                            int dayOfMonth) {
                calendar.set(year, month, dayOfMonth);
                editText.setText(doCalc());
            }
        });

    }

    private void readSettings() {
        offsetNumerator = sp.getBoolean("offsetNumerator", false);
        displacementWeekSemester = Integer.valueOf(sp.getString("displacementWeekSemester", "1")) - 1;

        int fontSize = Integer.valueOf(sp.getString("fontSize", String.valueOf((getString(R.string.fontSize)))));
        editText.setTextSize(TypedValue.COMPLEX_UNIT_SP, fontSize);
    }

    @Override
    protected void onResume() {
        super.onResume();
        readSettings();
        Date date = new Date();
        cal.setDate(date.getTime());
        calendar.setTime(date);
        editText.setText(doCalc());
    }

    // Even or odd definition
    public static boolean isEven(int x) {
        return (x & 1) == 0;
    }

    // Calculation of week number and is it numerator or denomerator
    public String doCalc() {
        String date = sdf.format(calendar.getTime());
        int semesterWeek = calendar.get(Calendar.WEEK_OF_YEAR) - displacementWeekSemester;
        boolean numerator = isEven(semesterWeek);
        if (offsetNumerator) {
            numerator = !numerator;
        }
        if ((semesterWeek <= 0) || (semesterWeek > maxSemesterWeek)) {
            return getString(R.string.Selected) + date + getString(R.string.This_date_is_not_in_semester);
        } else {
            return getString(R.string.Выбрано) + date + getString(R.string.Week_Semester) + semesterWeek + getString(R.string.Now)
                    + (numerator ? getString(R.string.Numerator) : getString(R.string.Denomerator));
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        if (id == R.id.action_settings) {
            Intent intent = new Intent(this, SettingsActivity.class);
            startActivity(intent);
            return true;
        }

        if (id == R.id.about) {
            Toast toast = Toast.makeText(getApplicationContext(), R.string.email, Toast.LENGTH_LONG);
            toast.show();
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

}
  • First of all, consider not using `SimpleDateFormat`, `Calendar` and `Date`. All those classes are poorly designed and long outdated. See if you can use [desugaring](https://developer.android.com/studio/write/java8-support) to use [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/index.html). The classes you want from there are `LocalDate` and `ChronoUnit` for counting the days. Which then will be a matters of `ChronoUnit.DAYS.between(localDate1, localDate2)`. – Ole V.V. Jan 27 '23 at 21:40
  • LocalDate startDate = LocalDate.of(2022, 12, 1); LocalDate endDate = LocalDate.of(2023, 1, 27); Period period = Period.between(startDate, endDate); int days = period.getDays(); – Ömer Seyfettin Yavuzyiğit Feb 16 '23 at 19:55

0 Answers0