我正在使用JodaTime API進行日期時間計算。而且,當我在控制檯上只打印DateTime時,它正確打印。但是當我使用.toString()方法;它是worng;日期正在改變。日期時間tostring()方法返回不同的值java
這是代碼
public static void main(String[] args) {
int[] startHour= {11, 0};
int[] endtHour= {13, 0};
DateTimeZone dtZoneforUser = DateTimeZone.forID("America/New_York");
DateTime dtNow = DateTime.now(dtZoneforUser);
DateTime dtTimeWindowStart = new DateTime(dtNow.getYear(),dtNow.getMonthOfYear(),dtNow.dayOfMonth().get(),startHour[0],startHour[1],dtZoneforUser);
DateTime dtTimeWindowEnd = new DateTime(dtNow.getYear(),dtNow.getMonthOfYear(),dtNow.dayOfMonth().get(),endtHour[0],endtHour[1],dtZoneforUser);
List<String> lstAudio = new ArrayList<String>();
lstAudio.add("1");
lstAudio.add("2");
lstAudio.add("3");
lstAudio.add("4");
lstAudio.add("5");
lstAudio.add("6");
lstAudio.add("7");
lstAudio.add("8");
lstAudio.add("9");
lstAudio.add("10");
lstAudio.add("11");
lstAudio.add("12");
lstAudio.add("13");
List<String> lstOfTimeToPlay = new ArrayList<String>();
int[] randomNumber = { 46, 71, 41, 68, 58, 104, 47, 76, 46, 38, 71, 42, 52 };
DateTime recurringStartingTime = dtTimeWindowStart;
DateTime recurringEndTime = dtTimeWindowEnd;
List<String> timeToPlay = new ArrayList<String>();
int daysCounter = 0;
String pattern1 = "yyyy-MM-dd'T'HH:mm:ss";
DateTimeFormatter dtf = DateTimeFormat.forPattern(pattern1).withZone(dtZoneforUser);
for (int j = 0; j< 13; j++) {
int randomNum = randomNumber[j];
DateTime tempTime = recurringStartingTime.plusMinutes(randomNum);
recurringStartingTime = tempTime;
System.out.println(randomNum);
if (tempTime.isBefore(recurringEndTime))
{
System.out.println("Audio Id is "+j + " and the play time is "+tempTime);
lstOfTimeToPlay.add(tempTime.withZone(dtZoneforUser).toString());
}else {
daysCounter +=1;
recurringStartingTime = dtTimeWindowStart.plusDays(daysCounter).plusMinutes(randomNum);
recurringEndTime = dtTimeWindowEnd.plusDays(daysCounter);
System.out.println("Audio Id is "+j + " and the play time is "+recurringStartingTime);
lstOfTimeToPlay.add(tempTime.withZone(dtZoneforUser).toString());
}
}
for (String string : lstOfTimeToPlay) {
System.out.println(string);
}
輸出是 - 這是正確的O/P。
Audio Id is 0 and the play time is 2014-01-16T11:46:00.000-05:00
Audio Id is 1 and the play time is 2014-01-16T12:57:00.000-05:00
Audio Id is 2 and the play time is 2014-01-17T11:41:00.000-05:00
Audio Id is 3 and the play time is 2014-01-17T12:49:00.000-05:00
Audio Id is 4 and the play time is 2014-01-18T11:58:00.000-05:00
Audio Id is 5 and the play time is 2014-01-19T12:44:00.000-05:00
Audio Id is 6 and the play time is 2014-01-20T11:47:00.000-05:00
Audio Id is 7 and the play time is 2014-01-21T12:16:00.000-05:00
Audio Id is 8 and the play time is 2014-01-22T11:46:00.000-05:00
Audio Id is 9 and the play time is 2014-01-22T12:24:00.000-05:00
Audio Id is 10 and the play time is 2014-01-23T12:11:00.000-05:00
Audio Id is 11 and the play time is 2014-01-23T12:53:00.000-05:00
Audio Id is 12 and the play time is 2014-01-24T11:52:00.000-05:00
**This is List output (toString method() called)**
2014-01-16T11:46:00.000-05:00
2014-01-16T12:57:00.000-05:00
2014-01-16T13:38:00.000-05:00
2014-01-17T12:49:00.000-05:00
2014-01-17T13:47:00.000-05:00
2014-01-18T13:42:00.000-05:00
2014-01-19T13:31:00.000-05:00
2014-01-20T13:03:00.000-05:00
2014-01-21T13:02:00.000-05:00
2014-01-22T12:24:00.000-05:00
2014-01-22T13:35:00.000-05:00
2014-01-23T12:53:00.000-05:00
2014-01-23T13:45:00.000-05:00
在這個名單上面有三個出現在1月16日,應該是僅有的兩個按照上述名單。請建議。
謝謝
2014-01-17T11:41:00.000-05:00 && 2014-01-16T13:38:00.000-05 :00。 時間不一樣? –
難道你不想在11和13之間。你似乎在13:00後有時間。 –
@VinayakPingale是;這種差異即將到來;但第一個列表是正確的,然後是我存儲爲同一個列表toString();但改變價值;爲什麼?? – Kumar