Search code examples
androiddatabasedatecalendarstorage

How to store day time slots that generates weekly


Let's say i have an app with a calendar each day is divided into time-slots, example :

Monday : 8-10h 10-12h 12-14h

Tuesday : 8-10h 10-12h 12-14h

and every time slot has an event and the calender is updated weekly which means that this weeks time slots and events will no longer be available and will be replaced by a new week calender with new time slots events. what's the best way of storing such data?


Solution

  • Assuming you don't care about saving previous weeks time slots.

    Depending on your use case, you have 2 option. Either storing in database or in json file.

    Option 1:

    Since each day have multiple time slots, this is clearly one to many relation ship. Day X has N number of time slots.

    So first, create days table like this:

    dayID name
    abc123 Monday
    abcd1234 Tuesday

    Then, create another table for time slots where you have your time slots and the dayID as foreign key, like this:

    timeSlotID fromTime toTime dayID
    efg123 8 10 abc123
    efg1234 10 12 abc123

    Option 2:

    Now, if you only care about storing next week time slots I think storing this in database is not necessary. You can simply store them in a json file, since your data is really small and json files are fast and easy to deal with no matter what language and framework you are using.

    You can store your data in this way:

    {
    "Monday": [
        {
            "fromTime": 8,
            "toTime": 10
        },
        {
            "fromTime": 10,
            "toTime": 12
        }
    ],
    "Tuesday": [
        {
            "fromTime": 9,
            "toTime": 11
        },
        {
            "fromTime": 12,
            "toTime": 13
        }
    ]
    

    }

    And you can easily recreate the json data each week.