Search code examples
androidandroid-calendarandroid-timer

Time value getting beyond 60 min android


getting the wrong end time value (goes beyond to 60mins)

I am working on the calendar view. When I click an empty view I need to get the start time and end time I am getting start time properly but end time is going beyond 60 minutes

Eg: 4:69pm, 4:79pm

I need as 5:09pm, 5:19pm

Please check my code, when I add 60 minutes, it is going beyond 60 minutes

mWeekView.setEmptyViewClickListener(new
 WeekView.EmptyViewClickListener() {

     @Override
     public void onEmptyViewClicked(Calendar time) {

         String startTime = String.format("%02d:%02d",time.get(Calendar.HOUR_OF_DAY),time.get(Calendar.MINUTE));
         String endtime   = String.format("%02d:%02d",time.get(Calendar.HOUR_OF_DAY),60+time.get(Calendar.MINUTE));/*time.getTimeInMillis()+60*60*1000*/
         String startdate = String.format("%d/%s/%s",time.get(Calendar.DAY_OF_MONTH),time.get(Calendar.MONTH)+1,time.get(Calendar.YEAR));


         Intent intent = new Intent(getActivity(),AddAppointment.class);
         intent.putExtra("daystartdate", startdate);
         intent.putExtra("daystartTime",startTime);
         intent.putExtra("dayendtime",endtime);
         startActivity(intent);
     }
 });

Solution

  • I am assuming you want to add an hour to the current Calendar object, so you will need to create a new calendar object and add the time you want to it or you can use the already passed time object. For example,

    mWeekView.setEmptyViewClickListener(new
     WeekView.EmptyViewClickListener() {
    
         @Override
         public void onEmptyViewClicked(Calendar time) {
    
             String startTime = String.format("%02d:%02d",time.get(Calendar.HOUR_OF_DAY),time.get(Calendar.MINUTE));
             time.add(Calendar.MONTH, 1);
             String startdate = String.format("%d/%s/%s",time.get(Calendar.DAY_OF_MONTH),time.get(Calendar.MONTH)+1,time.get(Calendar.YEAR));
             time.add(Calendar.MONTH, -1);
             time.add(Calendar.MINUTE, 60);
             String endtime   = String.format("%02d:%02d",time.get(Calendar.HOUR_OF_DAY),time.get(Calendar.MINUTE));/*time.getTimeInMillis()+60*60*1000*/
    
    
             Intent intent = new Intent(getActivity(),AddAppointment.class);
             intent.putExtra("daystartdate", startdate);
             intent.putExtra("daystartTime",startTime);
             intent.putExtra("dayendtime",endtime);
             startActivity(intent);
         }
     });
    

    Hope this helps!