-1

i have two arrays

    String[] titles = {
    "apple",
    "cherry",
    "coconut",
    "banana",
    "lemon"}

and

    String[] times = {
    "04:21 AM",
    "12:01 PM",
    "03:32 PM",
    "06:30 PM",
    "08:04 PM"}

and also i have current time is

10:04 PM

Now my request:

I want to do a comparison What is the next time closest to the current time

the result will be:

NEXT TIME: apple (4:21 AM) after (6) Hour And (17) Minutes.

and i need timer to do this action every 1 second check

can anyone see this topic

how to get near time for the present time [Android Studio]

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Black.JAC
  • 25
  • 1
  • 6

2 Answers2

-1

i specially design a function to solve your problem , use function as you needed.

it will works for you surely.

/**
 *
 * @author Niravdas
 */
public class TimeDiff {
    String[] titles = { "apple", "cherry", "coconut", "banana", "lemon"};   
    String[] times = { "04:21 AM", "12:01 PM", "03:32 PM", "06:30 PM", "08:04 PM"};
    String findingafter="10:04 AM";

    public TimeDiff()
    {
        int time[]=nextTimeArrayIndex(findingafter,times);
        if(time[0]>=0)
            System.out.println("NEXT TIME: "+titles[time[0]]+ "("+ times[time[0]] +") after ("+time[1]+") Hour And ("+time[2]+") Minutes.");
        else
        {
            System.out.println("NEXT TIME: "+titles[time[3]]+ "("+ times[time[3]] +") Tommorrow");
        }
    }

    public static void main(String argv[])
    {
        new TimeDiff();
    }
    int[]  nextTimeArrayIndex(String Finding,String[] fromArray)
    {
        int shortest=-1,shortestsec=-1;
        long minsecdif=(24*60*60+1),minsec=(24*60*60+1);
        int hr=Integer.parseInt(Finding.substring(0, 2));
        int min=Integer.parseInt(Finding.substring(3, 5));
        long seconds = convertToSec(hr, min, 0, Finding.substring(Finding.length()-2));
        System.out.println("seconds :" + seconds);
          for(int i=0;i<fromArray.length;i++)
          {
              int temphr=Integer.parseInt(fromArray[i].substring(0, 2));
              int tempmin = Integer.parseInt(fromArray[i].substring(3,5));
              long tempsec = convertToSec(temphr, tempmin, 0, fromArray[i].substring(Finding.length()-2));
              System.out.println("Compared to :" + tempsec);
              if((tempsec - seconds) > 0 && minsecdif > (tempsec - seconds))
              {
                  minsecdif = (tempsec - seconds);
                  shortest = i;
              }
              if(minsec > tempsec)
              {
                  minsec = tempsec;
                  shortestsec = i;
              }
          }

          int[] data = {0,0,0,0};

            data[0] = shortest;
            data[1] = (int) (minsecdif/60)/60;
            data[2] = (int) (minsecdif/60)%60;

            if(shortest==-1)
            {
              data[3] = (int)shortestsec;
            }

          return data;
    }
    long convertToSec(int hr,int min,int sec,String AMorPM)
    {
        if(hr==12)
        {
               hr=0;
        }
        long secs = (hr*60*60) + (min*60) + (sec);
        if(AMorPM.equalsIgnoreCase("PM"))
        {
            secs += (12*60*60);
        }
        return secs;

    }     

}

i hope it will help to solve your problem. :-)

Niravdas
  • 371
  • 2
  • 19
  • what Happened ! – Niravdas Sep 15 '18 at 17:43
  • bro when the time **03:38 PM** your code read: **NEXT TIME: 04:21 AM apple..etc** the code should show the result >> **NEXT TIME: 06:30 PM banana** ,, also bro when the time was **09:02 PM** the code show wrong result it show **NEXT TIME: 12:01 PM cherry** ,,and how to put this code in timer updater every one second – Black.JAC Sep 15 '18 at 21:31
  • bro please see this code it's **VB.NET** code and work 100% can you convert it to JAVA [link](https://pastebin.com/7PSNh4DT) – Black.JAC Sep 15 '18 at 21:39
  • bro when the time **03:38 PM** your code read: **NEXT TIME: 04:21 AM apple..etc** the code should show the result >> **NEXT TIME: 06:30 PM banana** ,, also bro when the time was** 09:02 PM** the code show wrong result it show **NEXT TIME: 12:01 PM cherry** ,, the code should show result: **NEXT TIME: 04:21 AM apple** and how to put this code in timer updater every one second – Black.JAC Sep 15 '18 at 21:47
  • i think you modify code, when i run my code gives output propery see this ** NEXT TIME FOR (03:38 PM)is: banana (06:30 PM) after (2) Hour And (52) Minutes.** and **NEXT TIME FOR (09:02 PM) is: apple(04:21 AM) Tommorrow** , if you want to call it per second you have to call this function in Thread. – Niravdas Sep 16 '18 at 08:50
  • it still give great output when i run it. just try again copy this code. and put this in a simple java file named TimeDiff.java – Niravdas Sep 16 '18 at 09:21
  • well bro, try put **03:45 PM** and tell me what output ? and try put time **09:24 PM** and also tell me what output ?? – Black.JAC Sep 16 '18 at 10:58
  • 03:45 PM : 06:30 PM 2hr 45min Difference , 09:24 PM : 04:21 AM Tomorrow. – Niravdas Sep 16 '18 at 15:55
  • veryyyyy well,, bro can you edit ( **NEXT TIME: X Tomorrow.** ) and make it show result look like first line `System.out.println("NEXT TIME: "+titles[time[0]]+ "("+ times[time[0]] +") after ("+time[1]+") Hour And ("+time[2]+") Minutes.");` – Black.JAC Sep 16 '18 at 16:07
  • and bro, i tryed add code to get current time and put it inside this virable **String findingafter="10:04 AM";** but i have crash can you get current time now – Black.JAC Sep 16 '18 at 16:09
  • convert Current time in HH:MM AM/PM perfect Format. otherwise it gives cast error string cannot converted to Integer. – Niravdas Sep 17 '18 at 14:52
  • 1
    bro can you edit ( NEXT TIME: X Tomorrow. ) and make it show result look like first line System.out.println("NEXT TIME: "+titles[time[0]]+ "("+ times[time[0]] +") after ("+time[1]+") Hour And ("+time[2]+") Minutes."); – Black.JAC Sep 17 '18 at 16:11
-1

I think this is more suitable solution for you.

/**
 * @author Niravdas
 */
public class TimeDiff {
    String[] titles = {"apple", "cherry", "coconut", "banana", "lemon"};
    String[] times = {"04:21 AM", "12:01 PM", "03:32 PM", "06:30 PM", "08:04 PM"};
    String findingAfter = "09:21 PM";
    public TimeDiff() {
        int time[] = nextTimeArrayIndex(findingAfter, times);
        if (time[0] >= 0) {
            System.out.println("NEXT TIME FOR (" + findingAfter + ") is: " + titles[time[0]] + "(" + times[time[0]] + ") after (" + time[1] + ") Hour And (" + time[2] + ") Minutes.");
        }
    }
    public static void main(String argv[]) {
        new TimeDiff();
    }
    int[] nextTimeArrayIndex(String Finding, String[] fromArray) {
        int shortest = -1, shortestsec = -1;
        long minsecdif = (24 * 60 * 60 + 1), minsec = (24 * 60 * 60 + 1);
        int hr = Integer.parseInt(Finding.substring(0, 2));
        int min = Integer.parseInt(Finding.substring(3, 5));
        long seconds = convertToSec(hr, min, 0, Finding.substring(Finding.length() - 2));
        for (int i = 0; i < fromArray.length; i++) {
            int temphr = Integer.parseInt(fromArray[i].substring(0, 2));
            int tempmin = Integer.parseInt(fromArray[i].substring(3, 5));
            long tempsec = convertToSec(temphr, tempmin, 0, fromArray[i].substring(Finding.length() - 2));
            if ((tempsec - seconds) > 0 && minsecdif > (tempsec - seconds)) {
                minsecdif = (tempsec - seconds);
                shortest = i;
            }
            if (minsec > tempsec) {
                minsec = tempsec;
                shortestsec = i;
            }
        }
        int[] data = {0, 0, 0};
        if (shortest != -1) {
            data[0] = shortest;
            data[1] = (int) (minsecdif / 60) / 60;
            data[2] = (int) (minsecdif / 60) % 60;
        } else {
            data[0] = (int) shortestsec;
            long tomorrowTimeDiff = (convertToSec(11, 59, 59, "PM") + 1 - seconds) + minsec;
            data[1] = (int) (tomorrowTimeDiff / 60) / 60;
            data[2] = (int) (tomorrowTimeDiff / 60) % 60;
        }
        return data;
    }
    long convertToSec(int hr, int min, int sec, String AMorPM) {
        if (hr == 12) {
            hr = 0;
        }
        long secs = (hr * 60 * 60) + (min * 60) + sec;
        if (AMorPM.equalsIgnoreCase("PM")) {
            secs += (12 * 60 * 60);
        }
        return secs;
    }
}

here notice that tomorrowTimeDiff() mathod body has some change. i think now it works proper for your program.

you also mentioned that you want to execute this function per second , This may help you.

Niravdas
  • 371
  • 2
  • 19
  • i will try your code and return... bro please can you help me in this topic i need you download DB and answear my request .. see my post in this [link](https://stackoverflow.com/questions/52367064/read-from-db-assest-and-display-data-in-listview) – Black.JAC Sep 17 '18 at 20:03
  • i am also beginner in android, i thought that i can solve this problem that's why i tried. i hope this will help you. – Niravdas Sep 17 '18 at 20:13
  • ok bro please try solve my other question about DB and listview just try maybe you will solve it – Black.JAC Sep 17 '18 at 20:30
  • it's work fine thank you bro,, now can you see mt other post about database – Black.JAC Sep 18 '18 at 05:16