0

I'm developing application that in one of its activity has the mechanism that calculates workdays between two dates:

package com.example.salary;

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

import android.app.Activity;
import android.app.DatePickerDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Toast;

public class CountingWorkDays extends Activity {

    static Calendar start_calendar = Calendar.getInstance();
    static Calendar end_calendar = Calendar.getInstance();
    EditText et_start;
    EditText et_end;
    Button count;
    Button save_days;
    static int workDays = 0;
    static int totalDays = 0;
    int dni_robocze = 0;

    Calendar start_calendar_save = Calendar.getInstance();
    Calendar end_calendar_save = Calendar.getInstance();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.przeliczanie_dni_roboczych);

        gather_start_date();
        gather_end_date();
        after_clicking_count();
        after_clicking_save();
    }

    private void update_et(EditText edittext, Calendar myCalendar) {  //inserting to EditText object

        String myFormat = "dd/MM/yy"; 
        SimpleDateFormat date = new SimpleDateFormat(myFormat, Locale.UK);
        edittext.setText(date.format(myCalendar.getTime()));

    }

    public void gather_start_date() {   //getting start date

        et_start = (EditText) findViewById(R.id.et_od);
        final DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener() {

            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear,
                    int dayOfMonth) {
                // TODO Auto-generated method stub
                start_calendar.set(Calendar.YEAR, year);
                start_calendar.set(Calendar.MONTH, monthOfYear);
                start_calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
                update_et(et_start, start_calendar);
            }

        };

        et_start.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                new DatePickerDialog(PrzeliczanieDniRoboczych.this, date,
                        start_calendar.get(Calendar.YEAR), start_calendar
                                .get(Calendar.MONTH), start_calendar
                                .get(Calendar.DAY_OF_MONTH)).show();
            }
        });

    }

    public void gather_end_date() {  //getting end date
        et_end = (EditText) findViewById(R.id.et_do);
        final DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener() {

            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear,
                    int dayOfMonth) {
                // TODO Auto-generated method stub
                end_calendar.set(Calendar.YEAR, year);
                end_calendar.set(Calendar.MONTH, monthOfYear);
                end_calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
                update_et(et_end, end_calendar);
            }

        };

        et_end.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                new DatePickerDialog(PrzeliczanieDniRoboczych.this, date,
                        end_calendar.get(Calendar.YEAR), end_calendar
                                .get(Calendar.MONTH), end_calendar
                                .get(Calendar.DAY_OF_MONTH)).show();
            }
        });

    }

    public void after_clicking_count() {  //after clicking "Calculate"
        work_days = 0;
        count = (Button) findViewById(R.id.b_count_workdays);
        count.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                work_days = getWorkingDaysBetweenTwoDates();

            }
        });

    }

    public int getWorkingDaysBetweenTwoDates() {  //getting working days between two dates

        workDays = 0;
        totalDays = 0;
        // Return 0 if start and end are the same
        if (et_start.getText().toString().trim().length() == 0
                || et_end.getText().toString().trim().length() == 0
                || et_start.getText().toString().matches("dd/mm/yy")
                || et_end.getText().toString().matches("dd/mm/yy")) {
            Toast.makeText(getApplicationContext(), "Choose dates!",
                    Toast.LENGTH_SHORT).show();
            return 0;
        } else {

            if (start_calendar.getTimeInMillis() > end_calendar
                    .getTimeInMillis()) {
                Toast.makeText(PrzeliczanieDniRoboczych.this,
                        "Start date has to be before end date",
                        Toast.LENGTH_SHORT).show();
            } else {

                for (; start_calendar.getTimeInMillis() <= end_calendar
                        .getTimeInMillis(); start_calendar.add(
                        Calendar.DAY_OF_MONTH, 1)) {
                    totalDays++;

                    if (start_calendar.get(Calendar.DAY_OF_WEEK) != Calendar.SATURDAY
                            && start_calendar.get(Calendar.DAY_OF_WEEK) != Calendar.SUNDAY) {
                        workDays++;
                    }

                }

                start_calendar.add(Calendar.DAY_OF_MONTH, -1 * totalDays);

                Toast.makeText(PrzeliczanieDniRoboczych.this,
                        "Workday is this period are: " + workDays,
                        Toast.LENGTH_SHORT).show();
            }
            return workDays;
        }
    }

I would like to add the mechanism that will allow user to insert dates of holidays (this aplication will calculate the salary and holidays means no salary, so it's important).

As I saw here, user SimplyPanda suggested adding an ArrayList<Integer> to do so. However, I wondering how the inserting by user should look like. Should user know the integer representation of particular dates in Java? - I guess no.

Does anyone have any idea how to implement such functionality?

Community
  • 1
  • 1
Jack Lynx
  • 216
  • 2
  • 12
  • Actually if you name your variables and methods in English, it would be easier for people to help you. Okay, logically **if you want to count the salary** then all you need to do is to **know how many holidays available in a month**. You can say the basic formula is `(totalWorkDays - totalHolidays) * baseSalary`. Whether you should use `ArrayList` or not, it depends on how you get the **user input**. – Harry Jan 21 '15 at 07:07
  • And this is my question, regardless the names of functions and variables. **How to gather information about holidays?** – Jack Lynx Jan 21 '15 at 18:22
  • If that is your real question, then I suggest you to use [Google Calendar API V3](https://developers.google.com/google-apps/calendar/v3/reference/) in case you are too lazy to do the input one by one. Take a look [here](http://stackoverflow.com/questions/18996577/how-to-get-national-holidays-of-selected-country) for the detail. – Harry Jan 22 '15 at 02:23

0 Answers0