jeudi 10 mars 2016

How do I deal with the time and date in android

I have a Appointment Scheduler application and any scheduler must deal with date and time of the appointment. So i have 2 functions in my project it isn't work fine. 1st checking if the appointment is conflicted with other appointment. 2nd i need to create a service to push a notification for the next appointment.

about the 1st function I've created a webservice that check that there is not appointments after or before the new appointment by 30 minutes.

$selectQuery = "SELECT * FROM Appointment WHERE (AppointmentDate = '$date' AND AppointmentTime = '$time') OR (AppointmentDate = '$date' AND AppointmentTime BETWEEN '$time'-'00:30' AND '$time'+'00:30') AND UserId = '$userId'";
$selectResult = mysqli_query($cn,$selectQuery);
{
    $response['state'] = false;
    $response['message'] = "Conflicted appointment!";
}

and this the addAppointment function from DBHelper

public boolean addPersonalAppointment(int userId, int serverId, String title, String content, String place, String address, String date, String time, String type, String material) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues cv = new ContentValues();
    cv.put(APPOINTMENT_USER_ID, userId);
    cv.put(APPOINTMENT_SERVER_ID, serverId);
    cv.put(APPOINTMENT_TITLE, title);
    cv.put(APPOINTMENT_CONTENT, content);
    cv.put(APPOINTMENT_PLACE, place);
    cv.put(APPOINTMENT_ADDRESS, address);
    cv.put(APPOINTMENT_DATE, date);
    cv.put(APPOINTMENT_TIME, time);
    cv.put(APPOINTMENT_TYPE, type);
    cv.put(APPOINTMENT_MATERIAL, material);
    long insertState = 0;
    //SELECT DATE(AppointmentDate) FROM PersonalAppointment WHERE DATE(AppointmentDate) = DATE('2016-02-16')
    ArrayList<PersonalAppointment> data = getAllPersonalAppointments(userId);
    for (int i = 0; i < data.size(); i++) {
        Log.d("Date", data.get(i).getAppointmentDate());
        if (data.get(i).getAppointmentDate().equals(date)) {
            String appointmentTime = data.get(i).getAppointmentTime();
            SimpleDateFormat format = new SimpleDateFormat("HH:mm", Locale.getDefault());

            try {
                Time currentTime = new Time(format.parse(time).getTime());
                Time existingTime = new Time(format.parse(appointmentTime).getTime());
                Log.d("CurrentTime", currentTime.toString());
                Log.d("ExistingTime", existingTime.toString());
                if (currentTime.getTime() == existingTime.getTime()) {
                    Toast.makeText(context, R.string.appointment_conflicted, Toast.LENGTH_LONG).show();
                    return false;

                } else if ((currentTime.getTime() - existingTime.getTime()) < ((60 * 1000) / 2) || (existingTime.getTime() - currentTime.getTime() > ((60 * 1000) / 2))) {
                    Log.d("Current Minutes", currentTime.getTime() + "");
                    Log.d("Existing Minutes", existingTime.getTime() + "");
                    Toast.makeText(context, R.string.appointment_is_near_to_another_appointment, Toast.LENGTH_LONG).show();
                    return false;
                }
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
    }

    try {
        insertState = db.insert(APPOINTMENT_TABLE, null, cv);
    } catch (Exception ex) {
        Log.d("addPersonalAppointment", ex.getMessage());
    } finally {
        db.close();
    }
    if (insertState > 0) {
        return true;
    } else {
        return false;
    }
}

so the problem it's when you add a new appointment it doesn't work fine some times give me that there is a conflicted appointment or maybe add a conflicted appointment.

Aucun commentaire:

Enregistrer un commentaire