2017-03-25 54 views
-3

我是android開發中的新手。時間格式轉換過去幾個小時變得非常困難。添加15分鐘至12小時時間格式

我需要添加15分鐘的給定時間。

我已經FROMTIME = '10:00 AM'TOTIME = '02:00 PM'。現在我需要在這兩次之間創建15分鐘的時間段。 (如10:00 AM10:15 AM10:30 AM ...高達02:00 PM

我嘗試使用日曆類添加methid像下面的方式再增加15分。

SimpleDateFormat df = new SimpleDateFormat("HH:mm"); 
Date d = df.parse(fromTime); 
Calendar calendar = Calendar.getInstance(); 
calendar.setTime(d); 
calendar.add(Calendar.MINUTE, 15); 
String newTime = df.format(calendar.getTime()); 

但它會10:00 AM轉換爲10:15 AM但是,轉換TOTIME 02:00 PM02:00 AM我搜索很多地方轉換,但沒有找到任何解決方案。

甚至沒有找到任何適當的解決方案來創建時間噸。

任何幫助將不勝感激。

+0

你想要做什麼? – mehulmpt

回答

1
String fromTime = "10:00 AM"; 
    String toTime = "02:00 PM"; 
    SimpleDateFormat df = new SimpleDateFormat("hh:mm a"); 
    Date dStart = df.parse(fromTime); 
    Date dEnd = df.parse(toTime); 
    System.out.println(dStart); 
    System.out.println(dEnd); 
    Calendar calendarStart = Calendar.getInstance(); 
    calendarStart.setTime(dStart); 
    while(calendarStart.getTime().before(dEnd)){ 
     System.out.println(calendarStart.getTime()); 
     calendarStart.add(Calendar.MINUTE, 15); 
    }