Click here to Skip to main content
15,884,237 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Once upon a time, i was post using loop..
example :
today is 10/28/2020
then i input 3 days after...

so the output is
10/28/2020
10/29/2020
10/30/2020

here is my code for post with loop date :
private void khusus() throws ParseException {
        pDialog = new ProgressDialog(this);
        showDialog();
        pDialog.setContentView(R.layout.progress_dialog);
        pDialog.getWindow().setBackgroundDrawableResource(
                android.R.color.transparent
        );

        final int num3 = (int) Double.parseDouble(hari.getText().toString());
        int i;
        String date = edittext.getText().toString();
        final SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
        final Calendar c = Calendar.getInstance();
            c.setTime(sdf.parse(date));
            for (i = 1; i <= num3; i++) {
                final String tanggal = sdf.format(c.getTime());
                c.add(Calendar.DATE, 1);
                System.out.println("Tanggal = " + tanggal);

                StringRequest stringRequest2 = new StringRequest(Request.Method.POST, "http://10.12.0.220/db_eis/cuti_khusus_insert.php",
                        new Response.Listener<String>() {
                            @Override
                            public void onResponse(String response) {
                                    hideDialog();
                                    Toast.makeText(getApplicationContext(), "data sudah dimasukkan", Toast.LENGTH_LONG).show();
                                    cutikhusus.this.finish();
                                }
                        },
                        new Response.ErrorListener() {
                            @Override
                            public void onErrorResponse(VolleyError error) {
                                hideDialog();
                                Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_LONG).show();

                            }

                        }) {


                    @Override
                    protected Map<String, String> getParams() throws AuthFailureError {

                        Map<String, String> params = new HashMap<String, String>();
                        sharedPreferences = getSharedPreferences("user_details", MODE_PRIVATE);

                        String pengajuan = nopengajuan.getText().toString();
                        String nik_baru = sharedPreferences.getString(KEY_NIK, null);
                        String jabatan = txt_nomor_jabatan.getText().toString();
                        String jenis = jeniscuti.getText().toString();

                        String kondisi = spinner.getSelectedItem().toString();
                        String keterangan_khusus = keterangan.getText().toString();
                        String gambar = imagetoString(bitmap);

                        params.put("no_pengajuan_khusus", pengajuan);
                        params.put("nik_cuti_khusus", nik_baru);
                        params.put("jabatan_cuti_khusus", jabatan);
                        params.put("jenis_cuti_khusus", jenis);
                        params.put("kondisi", kondisi);
                        params.put("start_cuti_khusus", tanggal);

                        params.put("ket_tambahan_khusus", keterangan_khusus);
                        params.put("status_cuti_khusus", "0");
                        params.put("status_cuti_khusus_2", "0");
                        params.put("dokumen_cuti_khusus", gambar);

                        return params;
                    }
                };
                stringRequest2.setRetryPolicy(new DefaultRetryPolicy(20 * 1000, 0,
                        DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
                RequestQueue requestQueue2 = Volley.newRequestQueue(this);
                requestQueue2.add(stringRequest2);
            }
        }


What I have tried:

in this case when i input 5 its alright, because it sorted well in mysql
10/28/2020
10/29/2020
10/30/2020
10/31/2020
11/01/2020

but, when i input more than 20 it's not sorted well in mysql

10/23/2020
10/29/2020
10/21/2020
11/01/2020
11/19/2020
|
|
|
etc...

so, this is the code i used for post with loop...
int i;
        String date = edittext.getText().toString();
        final SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
        final Calendar c = Calendar.getInstance();
            c.setTime(sdf.parse(date));
            for (i = 1; i <= num3; i++) {
                final String tanggal = sdf.format(c.getTime());
                c.add(Calendar.DATE, 1);
                System.out.println("Tanggal = " + tanggal);


and this is i used it for post...
@Override
                    protected Map<String, String> getParams() throws AuthFailureError {

                        Map<String, String> params = new HashMap<String, String>();
                        sharedPreferences = getSharedPreferences("user_details", MODE_PRIVATE);

                        String pengajuan = nopengajuan.getText().toString();
                        String nik_baru = sharedPreferences.getString(KEY_NIK, null);
                        String jabatan = txt_nomor_jabatan.getText().toString();
                        String jenis = jeniscuti.getText().toString();

                        String kondisi = spinner.getSelectedItem().toString();
                        String keterangan_khusus = keterangan.getText().toString();
                        String gambar = imagetoString(bitmap);

                        params.put("no_pengajuan_khusus", pengajuan);
                        params.put("nik_cuti_khusus", nik_baru);
                        params.put("jabatan_cuti_khusus", jabatan);
                        params.put("jenis_cuti_khusus", jenis);
                        params.put("kondisi", kondisi);
                        params.put("start_cuti_khusus", tanggal);

                        params.put("ket_tambahan_khusus", keterangan_khusus);
                        params.put("status_cuti_khusus", "0");
                        params.put("status_cuti_khusus_2", "0");
                        params.put("dokumen_cuti_khusus", gambar);

                        return params;
                    }
                };
Posted
Updated 27-Oct-20 18:05pm
v2
Comments
David Crow 28-Oct-20 9:53am    
"...it's not sorted well in mysql"

What does your SELECT statement look like? What type of field is used for the date?
Komang Putra 28-Oct-20 9:56am    
i use date then i convert to string
David Crow 28-Oct-20 10:14am    
Shouldn't it be an actual DATE type? If you use CHAR or TEXT instead, then you'll need to store your date values as YYYYMMDD instead.
Komang Putra 29-Oct-20 22:04pm    
yes, i already have this date value, but it keeps not sorted
David Crow 30-Oct-20 8:27am    
"yes, i already have this date value..."

The date value and its visual format are two different things. To which do you refer?

"...but it keeps not sorted"

You've not shown any sorting code.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900