How to send a message telling the user the start date is greater than the end date?

1

I spent the whole afternoon doing this code but it did not work well. What do I do to send a message to the user saying that the start time can not be greater than the end time?

xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/content_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.alinesilvagonzaga.myapplication.MainActivity">



<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!"
    android:id="@+id/textView2" />

<EditText
    android:hint="data inicial"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="date"
    android:ems="10"
    android:layout_below="@+id/textView2"
    android:layout_alignParentEnd="true"
    android:layout_marginEnd="246dp"
    android:layout_marginTop="31dp"
    android:id="@+id/editText" />

<EditText
    android:hint="hora inicial"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="time"
    android:ems="10"
    android:layout_alignBottom="@+id/editText"
    android:layout_alignParentEnd="true"
    android:id="@+id/editText2" />

<EditText
    android:hint="data final"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="date"
    android:ems="10"
    android:layout_below="@+id/editText"
    android:layout_alignParentEnd="true"
    android:layout_marginEnd="173dp"
    android:layout_marginTop="60dp"
    android:id="@+id/editText3" />

<EditText
    android:hint="hora final"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="time"
    android:ems="10"
    android:layout_below="@+id/editText3"
    android:layout_alignParentEnd="true"
    android:layout_marginTop="14dp"
    android:id="@+id/editText5" />

    <TextView
        android:text="TextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editText5"
        android:layout_alignEnd="@+id/textView2"
        android:layout_marginTop="34dp"
        android:id="@+id/textView" />

    <EditText
        android:hint="período"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:ems="10"
        android:layout_below="@+id/textView"
        android:layout_alignParentStart="true"
        android:layout_marginTop="58dp"
        android:id="@+id/editText4" />
</RelativeLayout>

DatePicker xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <DatePicker
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/datePicker" />
</LinearLayout>

Timerpicker xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TimePicker
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/timePicker" />
</LinearLayout>

And the logic in Activity:

    private String segundosInicial = "59";
    private String horainicialRegra = "00:00";
    private String horafinalRegra = "23:59";
    private String segundosFinal = "59";
    private String datafinalregra = "18/12/3000"+" "+horafinalRegra+":"+segundosFinal;
    private String oi;
    private String horaatualok;
    private Date atualhoradata;
    private Date atualhoradatafinal;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        editTextDataInicio = (EditText) findViewById(R.id.editText);
        editTextHoraInicio = (EditText) findViewById(R.id.editText2);
        editTextDataTermino = (EditText) findViewById(R.id.editText3);
        editTextHoraTermino = (EditText) findViewById(R.id.editText5);
        TextView textView = (TextView) findViewById(R.id.textView);
        editTextHorario = (EditText) findViewById(R.id.editText4);




        Calendar calendar = Calendar.getInstance();
        Date dataAtual = calendar.getTime();
        datainicialRegra = dataAtual;
        SimpleDateFormat sdffinal = new SimpleDateFormat("dd/MM/yyyy");
        datainicialregra = sdffinal.format(datainicialRegra);
        editTextDataInicio.setText(datainicialregra);

        ano_ = String.valueOf(calendar.get(Calendar.YEAR));
        mes_ = String.valueOf(calendar.get(Calendar.MONTH));
        dia_ = String.valueOf(calendar.get(Calendar.DAY_OF_MONTH));

        dia_ = "0" + dia_;
        mes_ = "0" + (mes_ + 1);

        try{
            dia_ = dia_.substring(1,3);
        }
        catch (Exception e){

        }

        try{
            mes_ = mes_.substring(1,3);
        }
        catch (Exception e){

        }
        datainicialregra = dia_+"/"+mes_+"/"+ano_;

        try {
            datafinalRegra = sdffinal.parse(datafinalregra);
            datainicialRegra = sdffinal.parse(datainicialregra);

        } catch (ParseException e) {

        }




//
        editTextHoraInicio.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final AlertDialog.Builder builderTime = new AlertDialog.Builder(context);
                final View view = ((LayoutInflater) v.getRootView().getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.pegahora, null);
                builderTime.setView(view);
                timePicker = (TimePicker) view.findViewById(R.id.timePicker);

                timePicker.setIs24HourView(true);

                builderTime.setNegativeButton("Cancelar", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {

                        dialog.dismiss();
                    }
                });
                //coloca aqui
                builderTime.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {

                        datainicialregra = dia_+"/"+mes_+"/"+ano_;
                        Calendar cald = Calendar.getInstance();
                        atualhoradata = cald.getTime();

                        Calendar c = Calendar.getInstance();
                        int hora = timePicker.getHour();
                        int minuto = timePicker.getMinute();

                        String tHora = String.valueOf(hora);
                        String mHora = String.valueOf(minuto);
                        int tamHora = tHora.length();
                        int tamMin = mHora.length();
                        //Aqui é para a data inicial ser maior que a data atual.
                        if (tamHora == 1) {
                            horainicialRegra = "0" + hora + ":" + minuto+":"+segundosInicial;
                           // editTextHoraInicio.setText("0" + hora + ":" + minuto);
                        }
                        if (tamMin == 1) {
                            horainicialRegra = hora + ":" + "0" + minuto+":"+segundosInicial;
                            //editTextHoraInicio.setText(hora + ":" + "0" + minuto);
                        }
                        if (tamHora == 1 && tamMin == 1) {
                            horainicialRegra = "0" + hora + ":" + "0" + minuto+":"+segundosInicial;
                            //editTextHoraInicio.setText("0" + hora + ":" + "0" + minuto);
                        }
                        if (tamHora == 2 && tamMin == 2) {
                            horainicialRegra = hora + ":" + minuto+":"+segundosInicial;
                            //editTextHoraInicio.setText(hora + ":" + minuto);
                        }


                        Log.i(":", horainicialRegra);
                        SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
                        try {
                            datainicialRegra = sdf.parse(datainicialregra+" "+horainicialRegra);
                            //datafinalRegra = sdf.parse(datafinalregra+" "+horafinalRegra);

                        } catch (Exception e) {

                        }

                        Log.i("dataatual", sdf.format(atualhoradata));
                        Log.i("datainicial", sdf.format(datainicialRegra));

                            Log.i("jfhdj", String.valueOf(atualhoradata.compareTo(datainicialRegra)));
                        if(atualhoradata.compareTo(datainicialRegra) > 0){

                            AlertDialog.Builder alertaRegra1 = new  AlertDialog.Builder(activity);
                            alertaRegra1.setMessage("Hora já passou!");
                            alertaRegra1.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface arg0, int arg1) {
                                    arg0.dismiss();

                                }
                            });
                            alertaRegra1.create().show();
                        }else {

                            //if (hora >= c.get(Calendar.HOUR_OF_DAY) && minuto >= c.get(Calendar.MINUTE) || (hora == 0) || ( minuto == 0)) {

                            if (tamHora == 1) {
                               // horainicialRegra = "0" + hora + ":" + minuto;
                                editTextHoraInicio.setText("0" + hora + ":" + minuto);
                            }
                            if (tamMin == 1) {
                               // horainicialRegra = hora + ":" + "0" + minuto;
                                editTextHoraInicio.setText(hora + ":" + "0" + minuto);
                            }
                            if (tamHora == 1 && tamMin == 1) {
                                //horainicialRegra = "0" + hora + ":" + "0" + minuto;
                                editTextHoraInicio.setText("0" + hora + ":" + "0" + minuto);
                            }
                            if (tamHora == 2 && tamMin == 2) {
                               // horainicialRegra = hora + ":" + minuto;
                                editTextHoraInicio.setText(hora + ":" + minuto);
                            }

                        }
                        // }else{
                        //      Toast.makeText(context, "O horário não pode ser anterior ao de agora.", Toast.LENGTH_SHORT).show();
                        //  }

                    }
                });
                builderTime.create().show();
            }
        });

        editTextHoraTermino.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final AlertDialog.Builder builderTime2 = new AlertDialog.Builder(context);
                final View view = ((LayoutInflater) v.getRootView().getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.pegahora, null);
                builderTime2.setView(view);
                timePicker = (TimePicker) view.findViewById(R.id.timePicker);

                timePicker.setIs24HourView(true);

                builderTime2.setNegativeButton("Cancelar", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {

                        dialog.dismiss();
                    }
                });
                //coloca aqui
                builderTime2.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        //Protótipo:
                        //
                        if(editTextDataTermino.getText().toString().trim().equals("")) {
                            AlertDialog.Builder alertaRegra2 = new AlertDialog.Builder(activity);
                            alertaRegra2.setMessage("Insira data final do alerta.");
                            alertaRegra2.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface arg0, int arg1) {
                                    arg0.dismiss();

                                }
                            });
                            alertaRegra2.create().show();
                        }else {

                            Calendar calendarfinal = Calendar.getInstance();
                            atualhoradatafinal = calendarfinal.getTime();

                            Calendar c = Calendar.getInstance();
                            int hora = timePicker.getHour();
                            int minuto = timePicker.getMinute();

                            String tHora = String.valueOf(hora);
                            String mHora = String.valueOf(minuto);
                            int tamHora = tHora.length();
                            int tamMin = mHora.length();

                            if (tamHora == 1) {
                                horafinalRegra = "0" + hora + ":" + minuto + ":" + segundosFinal;
                                // editTextHoraInicio.setText("0" + hora + ":" + minuto);
                            }
                            if (tamMin == 1) {
                                horafinalRegra = hora + ":" + "0" + minuto + ":" + segundosFinal;
                                //editTextHoraInicio.setText(hora + ":" + "0" + minuto);
                            }
                            if (tamHora == 1 && tamMin == 1) {
                                horafinalRegra = "0" + hora + ":" + "0" + minuto + ":" + segundosFinal;
                                //editTextHoraInicio.setText("0" + hora + ":" + "0" + minuto);
                            }
                            if (tamHora == 2 && tamMin == 2) {
                                horafinalRegra = hora + ":" + minuto + ":" + segundosFinal;
                                //editTextHoraInicio.setText(hora + ":" + minuto);
                            }


                            Log.i(":", horainicialRegra);
                            SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
                            try {
                                datafinalRegra = sdf.parse(datafinalregra+" "+horafinalRegra);
                                //datafinalRegra = sdf.parse(datafinalregra+" "+horafinalRegra);

                            } catch (Exception e) {

                            }

                            Log.i("dataatual", sdf.format(atualhoradatafinal));
                            Log.i("datainicial", sdf.format(datafinalRegra));

                            Log.i("jfhdj", String.valueOf(atualhoradatafinal.compareTo(datafinalRegra)));
                            if (atualhoradatafinal.compareTo(datafinalRegra) > 0) {

                                AlertDialog.Builder alertaRegra1 = new AlertDialog.Builder(activity);
                                alertaRegra1.setMessage("Hora já passou!");
                                alertaRegra1.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface arg0, int arg1) {
                                        arg0.dismiss();

                                    }
                                });
                                alertaRegra1.create().show();
                            } else {

                                if (tamHora == 1) {
                                    // horainicialRegra = "0" + hora + ":" + minuto;
                                    editTextHoraTermino.setText("0" + hora + ":" + minuto);
                                }
                                if (tamMin == 1) {
                                    // horainicialRegra = hora + ":" + "0" + minuto;
                                    editTextHoraTermino.setText(hora + ":" + "0" + minuto);
                                }
                                if (tamHora == 1 && tamMin == 1) {
                                    //horainicialRegra = "0" + hora + ":" + "0" + minuto;
                                    editTextHoraTermino.setText("0" + hora + ":" + "0" + minuto);
                                }
                                if (tamHora == 2 && tamMin == 2) {
                                    // horainicialRegra = hora + ":" + minuto;
                                    editTextHoraTermino.setText(hora + ":" + minuto);
                                }

                            }
                        }
    
asked by anonymous 16.01.2017 / 01:10

1 answer

2

In your code at no time you are comparing the hours that the user put, but rather comparing each with the current time. For what you described in the question, this is not what you want to do (and your code is pretty confusing, it took me a long time to see what was going on right now, there are several variables that are instantiated but not used (Calendar c for example)

You need to save the values of the times selected by the user in a member variable (accessible for the whole class) and make this validation, or at the time the user press any confirmation button, or using any flag to control if the two hours were filled, and then carry out the comparison.

Another thing, you're comparing the already formatted strings in your compareTo, why not compare Date objects? It makes more sense.

I recommend you take a look at JodaTime , a VERY used library for handling dates / times in Java. / p>

Try to take a look at this gringo OS answer , it compares the Date objects.

    
16.01.2017 / 02:49