Generate different period of time using joda

39 views Asked by At

I have a scenario to generate specific time between two dates. Let's say Mar 1st to Mar 31st as my input. I need to generate datetime with specific hours as below.

Mar 1st 03:00 - Mar 1st 06:59

Mar 1st 07:00 - Mar 1st 14:59

Mar 1st 15:00 - Mar 2nd 02:59

Mar 2nd 03:00 - Mar 2nd 06:59

Mar 2nd 07:00 - Mar 2nd 14:59

.

.

.

.

Mar 31st 15:00 - Mar 31st 02:59

I am confused how to generate these different time periods using joda. Help me to generate these times.

1

There are 1 answers

0
Laurent On BEST ANSWER

I have implemented a solution for practicing the Joda library:

public class JodaDateTimeExercise {

    private static final String PATTERN = "MMM $$ HH:mm";

    public static void main(String[] args) {
        DateTime dateTimeBegin = new DateTime(2000, 3, 1, 3, 0);
        DateTime dateTimeEnd = dateTimeBegin.plusMinutes(239);

        DateTime dateTimeBeginCopy = dateTimeBegin;
        DateTime dateTimeEndCopy = dateTimeEnd;

        for (int dayIndex = 0; dayIndex < 31; dayIndex++) {
            printDateTime(dateTimeBeginCopy, dateTimeEndCopy);

            dateTimeBeginCopy = dateTimeBeginCopy.plusHours(4);
            dateTimeEndCopy = dateTimeEndCopy.plusHours(8);

            printDateTime(dateTimeBeginCopy, dateTimeEndCopy);

            dateTimeBeginCopy = dateTimeBeginCopy.plusHours(8);
            dateTimeEndCopy = dateTimeEndCopy.plusHours(12);

            printDateTime(dateTimeBeginCopy, dateTimeEndCopy);

            dateTimeBegin = dateTimeBegin.plusDays(1);
            dateTimeEnd = dateTimeEnd.plusDays(1);

            dateTimeBeginCopy = dateTimeBegin;
            dateTimeEndCopy = dateTimeEnd;
        }
    }

    private static void printDateTime(DateTime dateTimeBegin, DateTime dateTimeEnd) {
        System.out.print(dateTimeBegin.toString(PATTERN, Locale.US).replace("$$", formatDayOfMonth(dateTimeBegin.dayOfMonth().get())));
        System.out.print(" - ");
        System.out.println(dateTimeEnd.toString(PATTERN, Locale.US).replace("$$", formatDayOfMonth(dateTimeEnd.dayOfMonth().get())));
        System.out.println();
    }

    public static String formatDayOfMonth(int dayOfMonthIndex) {
        String suffix;

        switch ((dayOfMonthIndex < 20) ? dayOfMonthIndex : dayOfMonthIndex % 10) {
            case 1:
                suffix = "st";
                break;
            case 2:
                suffix = "nd";
                break;
            case 3:
                suffix = "rd";
                break;
            default:
                suffix = "th";
                break;
        }

        return dayOfMonthIndex + suffix;
    }

}

The output is the following:

Mar 1st 03:00 - Mar 1st 06:59

Mar 1st 07:00 - Mar 1st 14:59

Mar 1st 15:00 - Mar 2nd 02:59

Mar 2nd 03:00 - Mar 2nd 06:59

Mar 2nd 07:00 - Mar 2nd 14:59

Mar 2nd 15:00 - Mar 3rd 02:59

...

Mar 31st 03:00 - Mar 31st 06:59

Mar 31st 07:00 - Mar 31st 14:59

Mar 31st 15:00 - Apr 1st 02:59

As you can notice, there is a small difference between my output and your output. If you take the time to understand what I have written, you can easily fix it by yourself.