I have time in below format
2015-10-28T18:37:04.899+05:30
I have to create slots in below format
11.00AM - 12.00PM 28/10/15
12.00PM - 1.00PM 28/10/15
1.00PM - 2.00PM 28/10/15
String timeValue = "2015-10-28T18:37:04.899+05:30";
StringTokenizer stringTokenizer = new StringTokenizer(timeValue,"T");
String dateValue = stringTokenizer.nextElement().toString();
String endDateValue = "2015-10-30";
String restString= stringTokenizer.nextElement().toString();
StringTokenizer secondTokeniser = new StringTokenizer(restString,":");
String hours = secondTokeniser.nextElement().toString();
String minutes = secondTokeniser.nextElement().toString();
hours = String.valueOf(Integer.parseInt(hours) + 2);
if (Integer.parseInt(minutes) > 30){
minutes = "00";
}else{
minutes = "30";
}
String amOrPm = null;
if(Integer.parseInt(hours) < 12){
amOrPm = "AM";
}else{
amOrPm = "PM";
hours = String.valueOf(getHoursValue(Integer.parseInt(hours)));
}
String time1 = hours + ":" + minutes + " " + amOrPm;
String time2 = "12" + ":" + "00" + " AM ";
String format = "yyyy-mm-dd hh:mm a";
SimpleDateFormat sdf = new SimpleDateFormat(format);
try {
Date dateObj1 = sdf.parse(dateValue + " " + time1);
Date dateObj2 = sdf.parse(endDateValue + " " + time2);
Logger.d(TAG, "Date Start: " + dateObj1);
Logger.d(TAG, "Date End: " + dateObj2);
long dif = dateObj1.getTime();
while (dif < dateObj2.getTime()) {
Date slot = new Date(dif);
Log.d(TAG, "Hour slot = " + slot);
dif += 1800000;
}
}catch (ParseException ex){
ex.printStackTrace();
}
By the above code I am getting below output
Wed Jan 28 20:00:00 IST 2015
Wed Jan 28 20:30:00 IST 2015
Wed Jan 28 21:00:00 IST 2015
Wed Jan 28 21:30:00 IST 2015
Wed Jan 28 22:00:00 IST 2015
Wed Jan 28 22:30:00 IST 2015
which is in 24 hours format
I want the below out put format
11.00AM - 12.00PM 28/10/15
12.00PM - 1.00PM 28/10/15
1.00PM - 2.00PM 28/10/15
Can any one please help me regarding this
I think, this is something you are looking for:
String timeValue = "2015-10-28T18:37:04.899+05:30";
SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
try {
Calendar startCalendar = Calendar.getInstance();
startCalendar.setTime(sdf.parse(timeValue));
if (startCalendar.get(Calendar.MINUTE) < 30) {
startCalendar.set(Calendar.MINUTE, 30);
} else {
startCalendar.add(Calendar.MINUTE, 30); // overstep hour and clear minutes
startCalendar.clear(Calendar.MINUTE);
}
Calendar endCalendar = Calendar.getInstance();
endCalendar.setTime(startCalendar.getTime());
// if you want dates for whole next day, uncomment next line
//endCalendar.add(Calendar.DAY_OF_YEAR, 1);
endCalendar.add(Calendar.HOUR_OF_DAY, 24 - startCalendar.get(Calendar.HOUR_OF_DAY));
endCalendar.clear(Calendar.MINUTE);
endCalendar.clear(Calendar.SECOND);
endCalendar.clear(Calendar.MILLISECOND);
SimpleDateFormat slotTime = new SimpleDateFormat("hh:mma");
SimpleDateFormat slotDate = new SimpleDateFormat(", dd/MM/yy");
while (endCalendar.after(startCalendar)) {
String slotStartTime = slotTime.format(startCalendar.getTime());
String slotStartDate = slotDate.format(startCalendar.getTime());
startCalendar.add(Calendar.MINUTE, 30);
String slotEndTime = slotTime.format(startCalendar.getTime());
Log.d("DATE", slotStartTime + " - " + slotEndTime + slotStartDate);
}
} catch (ParseException e) {
// date in wrong format
}
I need to mention, this code ignore time zone, but it seems you don't need it. Example output:
cz.skywall.stack D/DATE: 07:00PM - 07:30PM, 28/10/15
cz.skywall.stack D/DATE: 07:30PM - 08:00PM, 28/10/15
cz.skywall.stack D/DATE: 08:00PM - 08:30PM, 28/10/15
cz.skywall.stack D/DATE: 08:30PM - 09:00PM, 28/10/15
cz.skywall.stack D/DATE: 09:00PM - 09:30PM, 28/10/15
cz.skywall.stack D/DATE: 09:30PM - 10:00PM, 28/10/15
cz.skywall.stack D/DATE: 10:00PM - 10:30PM, 28/10/15
cz.skywall.stack D/DATE: 10:30PM - 11:00PM, 28/10/15
cz.skywall.stack D/DATE: 11:00PM - 11:30PM, 28/10/15
cz.skywall.stack D/DATE: 11:30PM - 12:00AM, 28/10/15
As I mentioned in code, you can generate slots for whole next day by uncommenting line endCalendar.add(Calendar.DAY_OF_YEAR, 1);
.
You can use the following, please note that for month you shoud use MM
instead of mm
. I add a simple getHoursValue
since I don't know yours.
Hope this helps!
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
displayTimeSlots();
}
private int getHoursValue(int hours){
return hours - 12;
}
private void displayTimeSlots(){
String timeValue = "2015-10-28T18:37:04.899+05:30";
StringTokenizer stringTokenizer = new StringTokenizer(timeValue,"T");
String dateValue = stringTokenizer.nextElement().toString();
String endDateValue = "2015-10-30";
String restString= stringTokenizer.nextElement().toString();
StringTokenizer secondTokeniser = new StringTokenizer(restString,":");
String hours = secondTokeniser.nextElement().toString();
String minutes = secondTokeniser.nextElement().toString();
hours = String.valueOf(Integer.parseInt(hours) + 2);
if (Integer.parseInt(minutes) > 30){
minutes = "00";
}else{
minutes = "30";
}
String amOrPm;
if(Integer.parseInt(hours) < 12){
amOrPm = "AM";
}else{
amOrPm = "PM";
hours = String.valueOf(getHoursValue(Integer.parseInt(hours)));
}
String time1 = hours + ":" + minutes + " " + amOrPm;
String time2 = "12" + ":" + "00" + " AM ";
String format = "yyyy-MM-dd hh:mm a";
SimpleDateFormat sdf = new SimpleDateFormat(format);
try {
Date dateObj1 = sdf.parse(dateValue + " " + time1);
Date dateObj2 = sdf.parse(endDateValue + " " + time2);
Log.d("TAG", "Date Start: " + dateObj1);
Log.d("TAG", "Date End: " + dateObj2);
long dif = dateObj1.getTime();
while (dif < dateObj2.getTime()) {
Date slot1 = new Date(dif);
dif += 3600000;
Date slot2 = new Date(dif);
dif += 3600000;
SimpleDateFormat sdf1 = new SimpleDateFormat("hh:mm a");
SimpleDateFormat sdf2 = new SimpleDateFormat("hh:mm a, dd/MM/yy");
Log.d("TAG", "Hour slot = " + sdf1.format(slot1) + " - " + sdf2.format(slot2));
}
}catch (ParseException ex){
ex.printStackTrace();
}
}
}
Here is the logcat:
11-07 14:54:02.506 24299-24299/? D/TAG: Date Start: Wed Oct 28 20:00:00 GMT+07:00 2015
11-07 14:54:02.506 24299-24299/? D/TAG: Date End: Fri Oct 30 00:00:00 GMT+07:00 2015
11-07 14:54:02.506 24299-24299/? D/TAG: Hour slot = 08:00 PM - 09:00 PM, 28/10/15
11-07 14:54:02.506 24299-24299/? D/TAG: Hour slot = 10:00 PM - 11:00 PM, 28/10/15
11-07 14:54:02.506 24299-24299/? D/TAG: Hour slot = 12:00 AM - 01:00 AM, 29/10/15
11-07 14:54:02.506 24299-24299/? D/TAG: Hour slot = 02:00 AM - 03:00 AM, 29/10/15
11-07 14:54:02.506 24299-24299/? D/TAG: Hour slot = 04:00 AM - 05:00 AM, 29/10/15
11-07 14:54:02.506 24299-24299/? D/TAG: Hour slot = 06:00 AM - 07:00 AM, 29/10/15
11-07 14:54:02.506 24299-24299/? D/TAG: Hour slot = 08:00 AM - 09:00 AM, 29/10/15
11-07 14:54:02.527 24299-24299/? D/TAG: Hour slot = 10:00 AM - 11:00 AM, 29/10/15
11-07 14:54:02.537 24299-24299/? D/TAG: Hour slot = 12:00 PM - 01:00 PM, 29/10/15
11-07 14:54:02.537 24299-24299/com.example.timeslots D/TAG: Hour slot = 02:00 PM - 03:00 PM, 29/10/15
11-07 14:54:02.556 24299-24299/com.example.timeslots D/TAG: Hour slot = 04:00 PM - 05:00 PM, 29/10/15
11-07 14:54:02.566 24299-24299/com.example.timeslots D/TAG: Hour slot = 06:00 PM - 07:00 PM, 29/10/15
11-07 14:54:02.566 24299-24299/com.example.timeslots D/TAG: Hour slot = 08:00 PM - 09:00 PM, 29/10/15
11-07 14:54:02.566 24299-24299/com.example.timeslots D/TAG: Hour slot = 10:00 PM - 11:00 PM, 29/10/15
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With