Question

I am using JodaTime API for date time calculation. Moreover, When I am printing simply only DateTime on the console; it is printing correctly. But when I am using .toString() method; it is worng; date is changing.

This is the code

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); 
        }

The output is - This is correct 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

In this above list there are three occurrences on 16 January that should be only two as per above list. Please suggest.

Thanks

Was it helpful?

Solution

Can you spot the difference?

Your code:

System.out.println("Audio Id is "+j + " and the play time is "+recurringStartingTime);
lstOfTimeToPlay.add(tempTime.withZone(dtZoneforUser).toString());

What you probably meant:

System.out.println("Audio Id is "+j + " and the play time is "+recurringStartingTime);
lstOfTimeToPlay.add(recurringStartingTime.withZone(dtZoneforUser).toString());
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top