【问题标题】:how to make a time cpmparison?如何进行时间比较?
【发布时间】:2018-09-14 18:08:28
【问题描述】:

我有两个数组

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

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

我也有当前时间

晚上 10:04

现在我的请求:

我想做一个比较下一次最接近当前时间的时间

结果将是:

下一次:苹果(凌晨 4 点 21 分)在 (6) 小时和 (17) 分钟之后。

我需要计时器每 1 秒检查一次执行此操作

任何人都可以看到这个主题

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

【问题讨论】:

  • 你能发布你到目前为止尝试过的东西吗?
  • 好办法是把字符串转成日期再处理
  • @ArsalaBangash 你能看到我帖子中的链接吗?看看 Andreas 的回答

标签: java android


【解决方案1】:

我认为这是更适合您的解决方案。

/**
 * @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;
    }
}

请注意 tomorrowTimeDiff() 方法体有一些变化。 我认为现在它适用于您的程序。

您还提到要每秒执行此功能,This 可能会帮助您。

【讨论】:

  • 我会尝试你的代码并返回......兄弟,你能帮我解决这个问题吗?我需要你下载数据库并回答我的请求......请参阅我在link中的帖子
  • 我也是android的初学者,我认为我可以解决这个问题,这就是我尝试的原因。我希望这会对你有所帮助。
  • 好的,兄弟,请尝试解决我关于 DB 和 listview 的其他问题,试试也许你会解决它
  • 它工作正常谢谢兄弟,现在你可以看到其他关于数据库的帖子
【解决方案2】:

我专门设计了一个函数来解决你的问题,你需要的函数使用。

它肯定对你有用。

/**
 *
 * @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;

    }     

}

我希望它能帮助您解决问题。 :-)

【讨论】:

  • 发生了什么!
  • 兄弟,当时间 03:38 PM 您的代码显示为:NEXT TIME: 04:21 AM apple..etc 代码应显示结果 >> NEXT TIME: 06:30 PMbanana ,, 兄弟当时间是 09:02 PM 时代码显示错误的结果它显示 NEXT TIME: 12:01 PM 樱桃,以及如何将此代码每隔一秒放入计时器更新器中
  • 兄弟,请查看此代码,它是 VB.NET 代码,并且可以 100% 工作,您可以将其转换为 JAVA link
  • 兄弟,当时间 03:38 PM 您的代码显示为:NEXT TIME: 04:21 AM apple..etc 代码应显示结果 >> NEXT TIME: 06:30 PMbanana ,, 也是兄弟当时间是** 09:02 PM** 代码显示错误的结果它显示 NEXT TIME: 12:01 PM cherry,代码应显示结果:NEXT TIME: 04:21 AM apple 以及如何将此代码每隔一秒放入计时器更新器中
  • 兄弟你能编辑(NEXT TIME: X Tomorrow. )并让它显示结果看起来像第一行 System.out.println("NEXT TIME: "+titles[time[0]]+ " ("+ times[time[0]] +") 在 ("+time[1]+") 小时和 ("+time[2]+") 分钟之后。");
猜你喜欢
  • 2014-01-22
  • 2011-02-02
  • 1970-01-01
  • 2011-11-14
  • 2019-10-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多