3

Hey I want to return a list of time object of 24 hours of time frame. It means i need a logic to get hours in a list starting from 00.00 to 23.00. Inside the list [00.00,01.00,02.00,.....23.00] . If i search a particular index like

println(timeList[3])

it will return

03.00.

How to i achieve this logic throught Calendar object or any other class. Does any one have idea about this how to get this type of list.

Kotlin Learner
  • 3,995
  • 6
  • 47
  • 127

2 Answers2

6

Use can create a List of java.time.LocalTime.

import java.time.LocalTime;
import java.util.ArrayList;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        List<LocalTime> list = new ArrayList<>();
        for (int i = 0; i < 24; i++) {
            list.add(LocalTime.of(i, 0));
        }

        System.out.println(list.get(3));
    }
}

Output:

03:00

Learn more about java.time, the modern date-time API* from Trail: Date Time.

Using legacy API:

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
        List<String> list = new ArrayList<>();
        
        for (int i = 0; i < 24; i++) {
            Calendar calendar = Calendar.getInstance();
            calendar.set(Calendar.HOUR_OF_DAY, i);
            calendar.set(Calendar.MINUTE, 0);
            list.add(sdf.format(calendar.getTime()));
        }

        System.out.println(list.get(3));

    }
}

* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
  • 2
    Looks like exactly what you need, @vivekmodi – deHaar May 23 '21 at 17:44
  • Hi @Arvind sir thanks for your reply. **list.add(LocalTime.of(i, 0));** **LocalTime.of** is giving error **Call requires API level 26 (current min is 16): java.time.LocalTime#of** – Kotlin Learner May 23 '21 at 17:46
  • @vivekmodi - I've posted a solution using the legacy API as well. – Arvind Kumar Avinash May 23 '21 at 17:52
  • Before resorting to `Calendar`, a poorly designed and long outdated class, please do yourself the favour of following the links to *Java 8+ APIs available through desugaring* and to *ThreeTen-Backport*. – Ole V.V. May 23 '21 at 19:02
3

The kotlin version

fun main(){
    var l = mutableListOf<LocalTime>()
    (0..24).forEach {
        l.add(LocalTime.of(it,0))
    }
}
Ananiya Jemberu
  • 1,366
  • 1
  • 9
  • 14
  • Hi @Ananiya thanks for your reply. l.add(LocalTime.of(it, 0)); **LocalTime.of** is giving error **Call requires API level 26 (current min is 16): java.time.LocalTime#of** – Kotlin Learner May 23 '21 at 17:49
  • You could wrap it inside `if(Build.VERSION.SDK_INT >= Build.VERSION.O)` – Ananiya Jemberu May 23 '21 at 17:51
  • 1
    As long as you're on Android Gradle plugin 4.0 or newer, with [coreLibraryDesugaring](https://developer.android.com/studio/write/java8-support-table) you can use java.time. – Ole V.V. May 23 '21 at 18:59