我有以下格式的时间
2015-10-28T18:37:04.899 + 05:30
我必须以以下格式创建广告位
11:00 AM-12:00 PM 15年10月28日
12:00 PM-1.00PM 15年10月28日
1.00PM-2.00PM 15年10月28日
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();
}
通过上面的代码,我得到下面的输出
2015年1月28日星期三20:00:00
2015年1月28日星期三20:30:00
2015年1月28日星期三21:00:00
2015年1月28日星期三21:30:00
2015年1月28日星期三22:00:00
2015年1月28日星期三22:30:00
24小时制
我想要以下输出格式
11:00 AM-12:00 PM 15年10月28日
12:00 PM-1.00PM 15年10月28日
1.00PM-2.00PM 15年10月28日
有人可以帮我吗
您可以使用以下内容,请注意,您应该使用月份MM
代替mm
。我加一个简单的,getHoursValue
因为我不认识你。
希望这可以帮助!
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();
}
}
}
这是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
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句