0

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()));
   }
Padma Kumar
  • 19,893
  • 17
  • 73
  • 130
user3584316
  • 61
  • 10

3 Answers3

3

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

 }
Ritesh Gune
  • 16,629
  • 6
  • 44
  • 72
  • thanks ritesh please tell me how to print the output in textview, im new to java. – user3584316 Apr 29 '14 at 08:20
  • thankss but where to add these updated lines im getting error on log.d and tag. – user3584316 Apr 29 '14 at 08:34
  • you can remove that line, that was put just to explain. – Ritesh Gune Apr 29 '14 at 08:36
  • time1 is for 2am, and time2 is for 6am say, time is the one you want to check. I am giving general solution. – Ritesh Gune Apr 29 '14 at 09:01
  • If it's working for you, then plz mark it as answer so that future users would benefit from it. – Ritesh Gune Apr 30 '14 at 05:10
  • but it is not parsing in correct format. its giving me output in this way Thu Jun 04 04:36:00 GMT+05:00 1970. – user3584316 Apr 30 '14 at 08:17
  • you can use any valid format you want, what i gave was just an example. – Ritesh Gune Apr 30 '14 at 08:34
  • i am having problem in parsing the strings, using any other format does not support the after and before methods of calendar. can you suggest something. – user3584316 Apr 30 '14 at 10:04
  • Ritesh plz can u tell me how to add a countdown timer in this code to calculate the time left for the interval. – user3584316 May 05 '14 at 06:03
  • Let us keep this thread clean.Plz ask this question in new thread giving reference to this question.I will reply there. – Ritesh Gune May 05 '14 at 06:12
  • thanks Ritesh plz check this link http://stackoverflow.com/questions/23465379/android-eclipse-how-to-set-a-count-down-timer-in-android/23465456?noredirect=1#23465456 – user3584316 May 05 '14 at 06:16
  • Ritesh I have asked this question in the new thread and this is the link http://stackoverflow.com/questions/23465379/android-eclipse-how-to-set-a-count-down-timer-in-android/23465456?noredirect=1#23465456 – user3584316 May 05 '14 at 06:23
  • As your question there is on hold, I can not answer that. Plz put the question properly, or delete that post and ask again properly in new thread. – Ritesh Gune May 05 '14 at 06:45
0

You should really try to use Calendar class that is WAY easier and that has that feature with Calendar.before(calendar).

shkschneider
  • 17,833
  • 13
  • 59
  • 112
0

Anyway, java have problem with Date and Time.

You should use Joda Time It's date and time library to replace JDK date handling

Download: http://mvnrepository.com/artifact/joda-time/joda-time/2.3

user1730007
  • 211
  • 1
  • 3
  • 13