0

I want to compare system time and user input time , i am comparing but i dont know its right or wrong statement because if(check) is not running here is my code

 c = Calendar.getInstance();
  SimpleDateFormat df1 = new SimpleDateFormat(" hh:mm a");
    String strdate = df1.format(c.getTime());
    Toast.makeText(getApplicationContext(), strdate , Toast.LENGTH_LONG).show();

Above Toast line showing the current time of system i.e. 04:50 AM

Toast.makeText(getApplicationContext(),  prayerTimes.get(0) , Toast.LENGTH_LONG).show();

this toast line showing the timing of user input i.e. 06:30 AM

if(strdate ==prayerTimes.get(0))
{
Intent in = new Intent(MainActivity.this, Dialog.class);
startActivity(in);
finish();
Toast.makeText(getApplicationContext(), "yahoo", Toast.LENGTH_LONG).show();
}

When both times will be same IF Check not work...Please help me how i can compare both times???

user3409263
  • 117
  • 1
  • 4
  • 11

3 Answers3

0

use equals() instead of == because you are comparing strings.

   if(strdate.equals(prayerTimes.get(0)))
{
Intent in = new Intent(MainActivity.this, Dialog.class);
startActivity(in);
finish();
Toast.makeText(getApplicationContext(), "yahoo", Toast.LENGTH_LONG).show();
}
M.Jaafar
  • 19
  • 1
0

I will suggest you not to compare two strings instead convert your both time into long and then compare it. To get current time use

long currentTime = System.currentTimeMillis(); 

pass your user time in below function to get user time in millisec and then compare both

private long getUserTime(String time){
    SimpleDateFormat df1 = new SimpleDateFormat(" hh:mm a");
    long timeInms = 0;
    Date date = null;
    try {
        date = df1.parse(time);
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
     timeInms = date.getTime();
    return timeInms;

}
maddy d
  • 1,530
  • 2
  • 12
  • 23
  • will you please tell me how to convert (strdate) into millisecond? – user3409263 Mar 14 '14 at 06:49
  • check [example](http://stackoverflow.com/questions/19419374/android-convert-date-and-time-to-milliseconds) to convert your time string into millisecond – maddy d Mar 14 '14 at 07:11
  • long l = Long.parseLong((String) prayerTimes.get(0)); i am doing this but its not working although long currentTime = System.currentTimeMillis(); getting the system time correctly but do not parse user set time – user3409263 Mar 14 '14 at 11:36
  • how do you set time for user, I mean your prayerTimes.get(0) what is it? – maddy d Mar 14 '14 at 11:58
  • prayerTimes.get(0) getting time like 4:55 – user3409263 Mar 14 '14 at 12:02
  • i am using your updated code it showing following error 03-15 at qibla.prayer.timing.MainActivity.getUserTime(MainActivity.java:259) – user3409263 Mar 15 '14 at 09:52
0

public class NotificationService extends Service {


    private Intent myIntent;
    MaterialDialog dialog;

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        this.myIntent = intent;

        final Handler handler = new Handler();
        Runnable runnable = new Runnable() {
            @RequiresApi(api = Build.VERSION_CODES.N)
            @Override
            public void run() {
                handler.postDelayed(this, 1000);

                Calendar calendar1 = Calendar.getInstance();
                SimpleDateFormat formatter1 = new SimpleDateFormat("dd/M/yyyy h:mm");
                String currentDate = formatter1.format(calendar1.getTime());

                String dateString = "11/08/2017";
                String timeString = "07:00";
                String datadb = dateString + " " + timeString;
             
                //comparing with the exac date/time/year what ever your wish

                if (currentDate.compareTo(datadb) >= 0) {
                    showDialog("msg"); // you can send extra information to service via intent

                }


            }
        };

        handler.postDelayed(runnable, 1000);


        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Toast.makeText(getApplicationContext(), "in service created", Toast.LENGTH_LONG).show();

    }

    private void showDialog(String message) {

//  NotificationCompat.Builder mBuilder =
        new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.holo_meducate)
                .setLargeIcon(BitmapFactory.decodeResource(getApplicationContext().getResources(), R.drawable.pdf_banner))
                .setContentTitle(msg)
                .setStyle(new NotificationCompat.BigTextStyle()
                        .bigText(Html.fromHtml(description)))
                .setContentText(Html.fromHtml(description))

                .setSound(getNotificationSoundUri());

        mBuilder.setContentIntent(contentIntent);
        mBuilder.setAutoCancel(true);
        mNotificationManager.notify(num, mBuilder.build());
        setLatestNotificationFlag();
    }


    @Override
    public void onDestroy() {
        if (dialog != null) {
            dialog.dismiss();
        }
        super.onDestroy();
    }

Start a service and inside a service start a runnable thrade in loop which reoccurs at every 30 sec and the compare the system time with required time and that will automatically fire notification ...

Sai Jayant
  • 366
  • 2
  • 14