0

How to get Time Slot interval of 1Hour based two times androidI want to store time slot in the arraylist. i have start time and end time. based on start time it should create time slot. For example if start time is 09:00AM and end time is 21:00PM then it should add into arraylist like below

09:00AM 10:00AM 11:00AM 12:00PM 13:00PM 14:00PM ..... so on 21:00PM

1 Answers1

0

You can do this way :

String firstDate = "26/02/2019";
String firstTime = "00:00 AM";
String secondDate = "26/02/2019";
String secondTime = "12:00 PM";

String format = "dd/MM/yyyy hh:mm a";

SimpleDateFormat sdf = new SimpleDateFormat(format);

  Date dateObj1 = sdf.parse(firstDate + " " + firstTime);
 Date dateObj2 = sdf.parse(secondDate + " " + secondTime);
 System.out.println("Date Start: "+dateObj1);
 System.out.println("Date End: "+dateObj2);

long dif = dateObj1.getTime(); 
while (dif < dateObj2.getTime()) {
 Date slot = new Date(dif);
System.out.println("Hour Slot --->" + slot);
  dif += 3600000;
 }
haresh
  • 1,424
  • 2
  • 12
  • 18
  • Please don’t teach the young ones to use the long outdated and notoriously troublesome `SimpleDateFormat` class. At least not as the first option. And not without any reservation. Today we have so much better in [`java.time`, the modern Java date and time API,](https://docs.oracle.com/javase/tutorial/datetime/) and its `DateTimeFormatter`. – Ole V.V. Jan 05 '20 at 12:19