Android:如何实现带假日日期的ArrayList

时间:2015-01-20 19:11:19

标签: java android arraylist calendar

我正在开发应用程序,在其中一个活动中有一个计算两个日期之间工作日的机制:

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;
        }
    }

我想添加允许用户插入假日日期的机制(这个应用程序将计算工资,假期意味着没有工资,所以这很重要。)

在我看到here时,用户 SimplyPanda 建议添加ArrayList<Integer>来执行此操作。但是,我想知道用户插入应该如何。 用户应该知道Java中特定日期的整数表示吗? - 我猜不是。

有没有人知道如何实现这样的功能?

0 个答案:

没有答案