How to get Time Slot interval of 1Hour based two times android

家住魔仙堡 提交于 2020-02-06 09:49:10

问题


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:


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;
 }


来源:https://stackoverflow.com/questions/59597354/how-to-get-time-slot-interval-of-1hour-based-two-times-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!