Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 756433
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T15:14:39+00:00 2026-05-14T15:14:39+00:00

I was wondering if anyone could look over a class I wrote, I am

  • 0

I was wondering if anyone could look over a class I wrote, I am receiving generic warnings in Eclipse and I am just wondering if it could be cleaned up at all. All of the warnings I received are surrounded in ** in my code below.

The class takes a list of strings in the form of (hh:mm AM/PM) and converts them into HourMinute objects in order to find the first time in the list that comes after the current time.

I am also curious about if there are more efficient ways to do this. This works fine but the student in me just wants to find out how I could do this better.

public class FindTime {
    private String[] hourMinuteStringArray;

    public FindTime(String[] hourMinuteStringArray){
        this.hourMinuteStringArray = hourMinuteStringArray;
    }

    public int findTime(){

        HourMinuteList hourMinuteList = convertHMStringArrayToHMArray(hourMinuteStringArray);
        Calendar calendar = new GregorianCalendar();
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        int minute = calendar.get(Calendar.MINUTE);
        HourMinute now = new HourMinute(hour,minute);
        int nearestTimeIndex = findNearestTimeIndex(hourMinuteList, now);
        return nearestTimeIndex;
    }

    private int findNearestTimeIndex(HourMinuteList hourMinuteList, HourMinute now){
        HourMinute current;
        int position = 0;
        Iterator<HourMinute> iterator = **hourMinuteList.iterator()**;
        while(iterator.hasNext()){
            current = (HourMinute) iterator.next();
            if(now.compareTo(current) == -1){
                return position;
            }
            position++;
        }
        return position;
    }


    private static HourMinuteList convertHMStringArrayToHMArray(String[] times){
        FindTime s = new FindTime(new String[1]);
        HourMinuteList list = s.new HourMinuteList();
        String[] splitTime = new String[3];
        for(String time : times ){
            String[] tempFirst = time.split(":");
            String[] tempSecond = tempFirst[1].split(" ");
            splitTime[0] = tempFirst[0];
            splitTime[1] = tempSecond[0];
            splitTime[2] = tempSecond[1];
            int hour = Integer.parseInt(splitTime[0]);
            int minute = Integer.parseInt(splitTime[1]);
            HourMinute hm;
            if(splitTime[2] == "AM"){
                hm = s.new HourMinute(hour,minute);
            }
            else if((splitTime[2].equals("PM")) && (hour < 12)){
                hm = s.new HourMinute(hour + 12,minute);
            }
            else{
                hm = s.new HourMinute(hour,minute);
            }

            **list.add(hm);**
        }
        return list;
    }
    class **HourMinuteList** extends **ArrayList** implements RandomAccess{

    }
    class HourMinute implements **Comparable** {
        int hour;
        int minute;

        public HourMinute(int hour, int minute) {
            setHour(hour);
            setMinute(minute);
        }

        int getMinute() {
            return this.minute;
        }
        String getMinuteString(){
            if(this.minute < 10){
                return "0" + this.minute;
            }else{
                return "" + this.minute;
            }
        }

        int getHour() {
            return this.hour;
        }

        void setHour(int hour) {
            this.hour = hour;
        }

        void setMinute(int minute) {
            this.minute = minute;
        }

        @Override
        public int compareTo(Object aThat) {

            if (aThat instanceof HourMinute) {
                HourMinute that = (HourMinute) aThat;
                if (this.getHour() == that.getHour()) {
                    if (this.getMinute() > that.getMinute()) {
                        return 1;
                    } else if (this.getMinute() < that.getMinute()) {
                        return -1;
                    } else {
                        return 0;
                    }
                } else if (this.getHour() > that.getHour()) {
                    return 1;
                } else if (this.getHour() < that.getHour()) {
                    return -1;
                } else {
                    return 0;
                }
            }

            return 0;
        }

    }


If you have any questions let me know. 

Thanks, 
Rob
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T15:14:39+00:00Added an answer on May 14, 2026 at 3:14 pm

    I wouldn’t use the HourMinute class, unless it has some other added value. If you only need to find the closest event time after a given point in time, convert your strings to Date (or to long values representing time), and store them in some sorted collection.
    The conversion can be done with SimpleDateFormat.

    If items are added dynamically, use TreeSet<Date>, together with ceiling(t) / higher(t) methods.

    If the set of items is not dynamic, use an array Date[], together with Arrays.binarySearch(..).

    Here is a (working) draft of the first approach:

    public class TimedEventsMgr {
        private TreeSet<Date> pointsInTime = new TreeSet<Date>();
        private SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd hh:mm a");
    
        //timeStr: hh:mm AM/PM
        public void add(String timeStr) throws ParseException{
            Date time = sdf.parse("20000101 "+timeStr);
            pointsInTime.add(time);
        }
    
        public Date closestFutureTime(Date time){
            Calendar c = Calendar.getInstance();
            c.setTime(time);
            c.set(Calendar.YEAR, 2000);
            c.set(Calendar.MONTH, 0); //January
            c.set(Calendar.DATE, 1);
            return pointsInTime.higher(c.getTime());
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was wondering if anyone could take a look at this code and help
I was wondering whether anyone could assist me. When the user clicks the menu
I was wondering if anyone could instruct as how to replace hyperlinks with their
I was wondering if anyone could help me implement color selection in my iOS
I was wondering if anyone could tell me about the i++ operator in C#.
I was wondering if anyone could tell me if what I'm trying to accomplish
I'm wondering if anyone could weigh in on pros and cons of different spelling
I was wondering if anyone could give me an example or point me to
I was wondering if anyone could tell me how to implement line 45 of
I was wondering if anyone could advise on a good pattern for loading textures

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.