【问题标题】:How to match a string to the device time如何将字符串与设备时间匹配
【发布时间】:2013-09-04 10:11:59
【问题描述】:

当我的设备时间介于或等于开始时间和结束时间时,我希望它显示ChogadiaName。这是我获取时间的方法:

/*{"chogadia":[{"ChogadiaName":1,"StartTime":"5:31:19 AM","EndTime":"7:14:15 AM","Effect":"Inauspicious Chogadia"},{"ChogadiaName":3,"StartTime":"8:57:10 AM","EndTime":"10:40:5 AM","Effect":"Auspicious Chogadia"},{"ChogadiaName":4,"StartTime":"10:40:5 AM","EndTime":"12:22:59 AM","Effect":"Auspicious Chogadia"}]}*/

public class ChogadiaParser {

public static  ArrayList<Chogadia> mList=new ArrayList<Chogadia>();
public static Chogadia mChogadia;
public static String response,chogadia;
public static String Lucky="Auspicious Chogadia";
public static String UnLucky="Inauspicious Chogadia";


public static void GroupResult(String url){

    try{
      JSONArray jArray;
      JSONObject jObject;

     response=GetJsonObject.sendRequest(url);

     if(response == null){
            return;
        }

     jObject=new JSONObject(response);
     jArray=jObject.getJSONArray("chogadia");
     mList.clear();
     for(int i=0;i<jArray.length();i++){

         mChogadia=new Chogadia();
         jObject=jArray.getJSONObject(i);
         mChogadia.SetChogadiaName(jObject.getString("ChogadiaName"));
         mChogadia.SetStartTime(jObject.getString("StartTime"));
         mChogadia.SetEndTime(jObject.getString("EndTime"));
         mChogadia.SetEffect(jObject.getString("Effect"));
         mList.add(mChogadia);



         if(mathcTime(jObject.getString("StartTime"),jObject.getString("EndTime"))){


             System.out.println("Matched Name Is: " + jObject.getString("ChogadiaName"));

             break; // break loop
         }



     } 


    }catch(Exception e){
        e.printStackTrace();
    }
}



private static boolean mathcTime(String stime,String eTime) {
    SimpleDateFormat ft = new SimpleDateFormat("hh:mm:ss");

    try {
        Date ct = new Date();
        Date st = ft.parse(stime);
        Date et=ft.parse(eTime);;

        long currentTime = ((ct.getHours()*60)*60) + (ct.getMinutes()*60) + (ct.getSeconds());
        long startTime = ((st.getHours()*60)*60) + (st.getMinutes()*60) + (st.getSeconds());
        long endTime = ((et.getHours()*60)*60) + (et.getMinutes()*60) + (et.getSeconds());

        if(currentTime>=startTime || currentTime<=endTime){
            return true;
        }else{
            return false;
        }
    } catch (Exception e) {
    }
 return false;
}
}

【问题讨论】:

  • @Scott W-你能帮我写这段代码吗..

标签: android json


【解决方案1】:

您应该在Service 中使用TimerTask

The TimerTask class represents a task to run at a specified time. The task may be run once or repeatedly.

或者您可以使用BroadcastReceiver 来监视Intent.ACTION_TIME_TICK。 Android 将每分钟发送一次 Intent.ACTION_TIME_TICK。然后你可以在接收器中捕获它并做你想做的事情。

Broadcast Action: The current time has changed. Sent every minute. You can not receive this through components declared in manifests, only by exlicitly registering for it with Context.registerReceiver().

另外,你的代码里有什么

currentTime>=startTime || currentTime<=endTime

应该是

currentTime>=startTime && currentTime<=endTime

【讨论】:

  • -我的prblm是如何在应用程序打开时匹配时间并显示Chogadia名称......
  • -我有两个条件但没有给出确切的输出
  • 不,currentTime&gt;=startTime || currentTime&lt;=endTime 总是返回 true 为startTime &lt; endTime,所以你需要使用currentTime&gt;=startTime &amp;&amp; currentTime&lt;=endTime
  • 数学很有用。只需要画一个数轴,把 startTime 和 endTime 和你的不等式区域放在里面,你就可以看清楚了。
  • -代码正在运行,但现在是新的prblm。我已经面对它只匹配下午时间之后或从下午 12 点开始。它与上午 9 点到 11 点的时间不匹配
【解决方案2】:

如果 Tablet 时间大于 ServerTime(5 分钟),这个 sn-p 将返回 true/false

    if ((serverDate.getTime() >= tabletDate.getTime() - 300000) &&
    (serverDate.getTime() <= tabletDate.getTime() + 300000)) 
        {
         isTrue = true;
            } else { 
                    isTrue = false;
                }

【讨论】:

  • @Kalai.G-如何在这个 AM 或 PM 中检查 AM|PM。
  • 您需要查看平板时间还是服务器时间?
猜你喜欢
  • 1970-01-01
  • 2014-10-12
  • 2021-04-28
  • 1970-01-01
  • 1970-01-01
  • 2014-07-05
  • 1970-01-01
  • 2020-10-21
  • 1970-01-01
相关资源
最近更新 更多