-1

This is my class where I have listOfmeeting with title and start time I have to keep all meeting with it calendar date as key and value as List<MeetingModel> like suppose today's calendar instance is Calendar.getInstance() that will be as key and two list of meeting as value same like other. how i will achieve this?

public class Meeting {

    public static void main(String[] args) {

        ArrayList<Long> dateinmiliis = new ArrayList<>();
        dateinmiliis.add(1512538200000L);
        dateinmiliis.add(1516235400000L);
        dateinmiliis.add(1516318200000L);
        dateinmiliis.add(1516397400000L);
        dateinmiliis.add(1516404600000L);
        dateinmiliis.add(1516498200000L);
        dateinmiliis.add(1516573800000L);
        dateinmiliis.add(1516663800000L);
        ArrayList<String> title = new ArrayList<>();
        title.add("Decemer 6 meeting");
        title.add("18thmeeting");
        title.add("19th meeting");
        title.add("20th first meeting");
        title.add("20 the second meeing");
        title.add("21th meeting");
        title.add("21th second meeting");
        title.add("23rd meeting");

        ArrayList<MeetingModel> listOfmeeting = new ArrayList<>();
        for (int i = 0; i < title.size(); i++) {
            MeetingModel model = new MeetingModel();
            model.setStarttime(dateinmiliis.get(i));
            model.setTitle(title.get(i));
            listOfmeeting.add(model);

        }
        HashMap<Calendar, List<MeetingModel>> mapEvents = new HashMap<>();

    }

    static void setMidnight(Calendar calendar) {
        if (calendar != null) {

            calendar.set(Calendar.HOUR_OF_DAY, 0);
            calendar.set(Calendar.MINUTE, 0);
            calendar.set(Calendar.SECOND, 0);
            calendar.set(Calendar.MILLISECOND, 0);
        }
    }
}
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Manoj Kumar
  • 97
  • 1
  • 1
  • 10
  • you have to create hash map of that and store date as key and titile is value – Mehul Tank Jan 20 '18 at 09:45
  • But @MehulTank actully i have to get today event as a single key you understand mean to say i have to convert key as only date not a time we need to consider – Manoj Kumar Jan 20 '18 at 10:43
  • 1
    Consider throwing away the long outmoded classes `Calendar` and `Date`, and adding [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP) to your Android project in order to use `java.time`, the modern Java date and time API. It is so much nicer to work with. – Ole V.V. Jan 20 '18 at 14:56
  • Can you use Java streams API at your API level? If so, you may want to look into streams for this task. – Ole V.V. Jan 20 '18 at 15:00
  • 1
    You need to decide a time zone before you can convert your milliseconds from the epoch to calendar dates. For example, 1512538200000L is Wednesday, December 6, 2017 5:30:00 AM UTC, but at this time it was still Tuesday, December 5 in most of the USA. – Ole V.V. Jan 20 '18 at 15:16
  • For discaring the time of day and getting just the calendar date, see for example [How do I get a Date without time in Java?](https://stackoverflow.com/questions/5050170/how-do-i-get-a-date-without-time-in-java) – Ole V.V. Jan 21 '18 at 03:30
  • You haven’t really shown an effort on your part. Please search and research before posting a question. In the question report what you have found and in what way it was insufficient. Also tell us what you have tried and in what way it failed. One, we can help you much more precisely from there. Two, there is a greater tendency to help someone who shows an effort. See more on [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) – Ole V.V. Jan 21 '18 at 12:04

1 Answers1

0
    final ZoneId zone = ZoneId.of("Asia/Bangkok");
    Map<LocalDate, List<MeetingModel>> mapEvents = listOfMeeting.stream()
                .collect(Collectors.groupingBy(mm -> mm.getDate(zone)));
    mapEvents.entrySet()
                .forEach(e -> System.out.println("" + e.getKey() + " -> " + e.getValue()));

On my computer this printed:

2018-01-23 -> [2018-01-22T23:30:00Z 23rd meeting]
2018-01-22 -> [2018-01-21T22:30:00Z 21th second meeting]
2017-12-06 -> [2017-12-06T05:30:00Z Decemer 6 meeting]
2018-01-21 -> [2018-01-21T01:30:00Z 21th meeting]
2018-01-20 -> [2018-01-19T21:30:00Z 20th first meeting, 2018-01-19T23:30:00Z 20 the second meeing]
2018-01-19 -> [2018-01-18T23:30:00Z 19th meeting]
2018-01-18 -> [2018-01-18T00:30:00Z 18thmeeting]

The dates don’t come in any particular order, but you can sort the map entries if you like, or even use a SortedMap, typically a TreeMap.

To get the above snippet to work, I fitted MeetingModel with a convenience method:

public LocalDate getDate(ZoneId zone) {
    return Instant.ofEpochMilli(startTime)
            .atZone(zone)
            .toLocalDate();
}

LocalDate is a class in java.time, the modern Java date and time API. I am using it for at least two reasons: (1) The modern API is so much nicer to work with compared to the outdated Calendar class. (2) LocalDate is a date without time of day, exactly what we need in your situation, so it greatly simplifies your task. On newer Android devices java.time comes built-in, I am told. On older devices you can use it through ThreeTenABP, see the links at the bottom.

It seems from the output that I didn’t guess your desired time zone correctly, since 21th second meeting has been mapped from January 22. I trust that you can substitute your desired time zone instead of Asia/Bangkok and thereby fix this issue.

I also added a toString method to MeetingModel. My toString method prints the time in UTC, and again, you can do better.

Tip: Use a constructor with arguments

As an aside, fit your class with the proper constructor to make initialization of your list simpler and more readable:

    ArrayList<MeetingModel> listOfMeeting = new ArrayList<>(List.of(
            new MeetingModel(1512538200000L, "Decemer 6 meeting"),
            new MeetingModel(1516235400000L, "18thmeeting"),
            new MeetingModel(1516318200000L, "19th meeting"),
            new MeetingModel(1516397400000L, "20th first meeting"),
            new MeetingModel(1516404600000L, "20 the second meeing"),
            new MeetingModel(1516498200000L, "21th meeting"),
            new MeetingModel(1516573800000L, "21th second meeting"),
            new MeetingModel(1516663800000L, "23rd meeting")));

In the above I am using the new List.of method introduced in Java 9. In earlier Java versions just use good old Arrays.asList instead:

    ArrayList<MeetingModel> listOfMeeting = new ArrayList<>(Arrays.asList(

There is a difference, though: Arrays.asList will accept a null meeting, List.of won’t.

Links

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • thanx but i want to keep all todays event in first position @Ole V.V. like if today is 21 or 20 we have to keep position ...top – Manoj Kumar Jan 21 '18 at 13:01
  • If I understand you correctly, @ManojKumar, isn’t that just a matter of doing `listOfMeeting.get(LocalDate.now(zone)` and printing the resulting list first (if we get one)? And then, if you want, some filter to avoid printing it again later. I won’t recommend having to sort all the meetings in a new way internally every time a new day begins. Only in the presentation. – Ole V.V. Jan 21 '18 at 13:26