我正在使用JodaTime API进行日期时间计算。而且,当我在控制台上只打印DateTime时;它打印正确。但是当我使用.toString()方法时;它是怪物;日期正在改变。
这是代码
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日有三次出现,按照上面的列表,应该只有两次。请建议。
由于
答案 0 :(得分:2)
你能发现差异吗?
您的代码:
System.out.println("Audio Id is "+j + " and the play time is "+recurringStartingTime);
lstOfTimeToPlay.add(tempTime.withZone(dtZoneforUser).toString());
你可能意味着什么:
System.out.println("Audio Id is "+j + " and the play time is "+recurringStartingTime);
lstOfTimeToPlay.add(recurringStartingTime.withZone(dtZoneforUser).toString());