本文介绍了比较Java的两个时间值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要得到当前系统时间,并与两个不同的时间比较例如开始时间为上午02时00分○○秒,结束为上午06时00分00秒。如果我的当前系统时间落在这之间我必须执行我的code的休息和显示的TextView输出。
 我有这个使用这种code获取当前的时间,我需要帮助的,如果条件使用的开始和结束时间。

 在此输入code TextView的tvTime =(的TextView)findViewById(R.id.textView1);
    SimpleDateFormat的TIMEFORMAT =新的SimpleDateFormat(HH:MM:SS);
    日历C = Calendar.getInstance();    IF(IF(c.getTime()==上午2点00分00秒和放大器;&安培;<早上6:00:00){   tvTime.setText(+ timeFormat.format(c.getTime()));
   }


解决方案

尝试()之前后() 日历

 公共布尔isTimeWithinInterval(字符串lwrLimit,字符串uprLimit,字符串时间){    //字符串时间1  - 下限
    TIME_1日期=新的SimpleDateFormat(HH:MM:SS)解析(lwrLimit)。
    日历calendar_1 = Calendar.getInstance();
    calendar_1.setTime(TIME_1);    //时间2串 - 上限
    日期time_2 =新的SimpleDateFormat(HH:MM:SS)解析(uprLimit)。
    日历calendar_2 = Calendar.getInstance();
    calendar_2.setTime(time_2);    //在字符串时间3 - 要检查
    日期D =新的SimpleDateFormat(HH:MM:SS)解析(时间);
    日历calendar_3 = Calendar.getInstance();
    calendar_3.setTime(四);    日期X = calendar_3.getTime();
    如果(x.after(calendar_1.getTime())及&放大器; x.before(calendar_2.getTime())){
        // checkes是否当前的时间是两个时间之间
        Log.d(TAG,真+);
        返回true;
    }    返回false;
}

更新:

 布尔timeChk = isTimeWithinInterval(时间1,时间2,时间3); 如果(timeChk){
   //成功code
   tvTime.setText(+时间3);
 }
 其他{
   //失败code }

I want to get the current system time and compare with the two different timing example start time as 2:00:00AM and end as 6:00:00AM. if my current system time falls between these i have to execute my rest of the code and show the output in textview. i have this use this code for getting the current time i need help for using start and end time in if condition.

enter code here



 TextView tvTime = (TextView)findViewById(R.id.textView1);
    SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss a");
    Calendar c = Calendar.getInstance();

    if (if (c.getTime() == 2:00:00 AM && < 6:00:00 AM) {

   tvTime.setText(" " + timeFormat.format(c.getTime()));
   }
解决方案

try before() and after() methods of Calender

public boolean isTimeWithinInterval(String lwrLimit, String uprLimit, String time){

    // Time 1 in string - Lower limit
    Date time_1 = new SimpleDateFormat("HH:mm:ss").parse(lwrLimit);
    Calendar calendar_1 = Calendar.getInstance();
    calendar_1.setTime(time_1);

    // Time 2 in string - Upper limit
    Date time_2 = new SimpleDateFormat("HH:mm:ss").parse(uprLimit);
    Calendar calendar_2 = Calendar.getInstance();
    calendar_2.setTime(time_2);

    // Time 3 in String - to be checked
    Date d = new SimpleDateFormat("HH:mm:ss").parse(time);
    Calendar calendar_3 = Calendar.getInstance();
    calendar_3.setTime(d);

    Date x = calendar_3.getTime();
    if (x.after(calendar_1.getTime()) && x.before(calendar_2.getTime())) {
        //checkes whether the current time is between two times
        Log.d(TAG,true+"");
        return true;
    }

    return false;
}

Update :

 boolean timeChk = isTimeWithinInterval(time1, time2, time3);

 if(timeChk){
   // Success code
   tvTime.setText(" " + time3);
 }
 else{
   // Failure code

 }

这篇关于比较Java的两个时间值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 12:41