Java Hour hourCompletion(boolean hourPresent, int hour, boolean minutePresent, int minute, boolean secondPresent, int second, boolean milliPresent, int milli, String choice)

Here you can find the source of hourCompletion(boolean hourPresent, int hour, boolean minutePresent, int minute, boolean secondPresent, int second, boolean milliPresent, int milli, String choice)

Description

hour Completion

License

Open Source License

Declaration

private static String hourCompletion(boolean hourPresent, int hour, boolean minutePresent, int minute,
            boolean secondPresent, int second, boolean milliPresent, int milli, String choice) 

Method Source Code

//package com.java2s;
/* //from  ww w.ja v  a  2s.co m
 * Copyright 2012 Devoteam http://www.devoteam.com
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 * 
 * 
 * This file is part of Multi-Protocol Test Suite (MTS).
 * 
 * Multi-Protocol Test Suite (MTS) is free software: you can redistribute
 * it and/or modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation, either version 3 of the
 * License.
 * 
 * Multi-Protocol Test Suite (MTS) is distributed in the hope that it will
 * be useful, but WITHOUT ANY WARRANTY; without even the implied warranty 
 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Multi-Protocol Test Suite (MTS).
 * If not, see <http://www.gnu.org/licenses/>.
 * 
 */

import java.util.Calendar;

public class Main {
    private static String hourCompletion(boolean hourPresent, int hour, boolean minutePresent, int minute,
            boolean secondPresent, int second, boolean milliPresent, int milli, String choice) {
        String result = new String();
        if (!choice.equals("hour") && !choice.equals("minute") && !choice.equals("second")
                && !choice.equals("milli"))
            throw new RuntimeException("Unrecognize parameter 'choice'");

        Calendar calendar = Calendar.getInstance();
        int state;

        if (hourPresent) {
            if (minutePresent) {
                if (secondPresent) {
                    if (milliPresent)
                        state = 0;
                    else
                        state = 1;//milli is not present
                } else {
                    if (milliPresent)
                        state = 2;//second is not present
                    else
                        state = 3;//milli and second are not present
                }
            } else {
                if (secondPresent) {
                    if (milliPresent)
                        state = 4;//minute is not present
                    else
                        state = 5;//milli and minute are not present
                } else {
                    if (milliPresent)
                        state = 6;//second and minute are not present
                    else
                        state = 7;//milli, second and minute are not present
                }
            }
        } else {
            if (minutePresent) {
                if (secondPresent) {
                    if (milliPresent)
                        state = 8;//hour is not present
                    else
                        state = 9;//milli and hour are not present
                } else {
                    if (milliPresent)
                        state = 10;//hour and second are not present
                    else
                        state = 11;//milli, second and hour are not present
                }
            } else {
                if (secondPresent) {
                    if (milliPresent)
                        state = 12;//hour and minute are not present
                    else
                        state = 13;//milli, minute and hour are not present
                } else {
                    if (milliPresent)
                        state = 14;//second, minute and hour are not present
                    else
                        state = 15;//nothing is not present
                }
            }
        }
        switch (state) {
        case 0:
            if (choice.equals("hour")) {
                result = String.valueOf(hour);
            } else if (choice.equals("minute")) {
                result = String.valueOf(minute);
            } else if (choice.equals("second")) {
                result = String.valueOf(second);
            } else if (choice.equals("milli")) {
                result = String.valueOf(milli);
            }
            break;
        case 1://milli is not present
            if (choice.equals("hour")) {
                result = String.valueOf(hour);
            } else if (choice.equals("minute")) {
                result = String.valueOf(minute);
            } else if (choice.equals("second")) {
                result = String.valueOf(second);
            } else if (choice.equals("milli")) {
                result = "000";
            }
            break;
        case 2://second is not present
            if (choice.equals("hour")) {
                result = String.valueOf(hour);
            } else if (choice.equals("minute")) {
                result = String.valueOf(minute);
            } else if (choice.equals("second")) {
                result = "00";
            } else if (choice.equals("milli")) {
                result = String.valueOf(milli);
            }
            break;
        case 3://milli and second are not present
            if (choice.equals("hour")) {
                result = String.valueOf(hour);
            } else if (choice.equals("minute")) {
                result = String.valueOf(minute);
            } else if (choice.equals("second")) {
                result = "00";
            } else if (choice.equals("milli")) {
                result = "000";
            }
            break;
        case 4://minute is not present
            if (choice.equals("hour")) {
                result = String.valueOf(hour);
            } else if (choice.equals("minute")) {
                if (calendar.get(Calendar.HOUR_OF_DAY) < hour || calendar.get(Calendar.HOUR_OF_DAY) > hour) {
                    result = "00";
                } else {
                    if (calendar.get(Calendar.SECOND) < second) {
                        result = String.valueOf(calendar.get(Calendar.MINUTE));
                    } else {
                        result = String.valueOf(calendar.get(Calendar.MINUTE) + 1);
                    }
                }
            } else if (choice.equals("second")) {
                result = String.valueOf(second);
            } else if (choice.equals("milli")) {
                result = String.valueOf(milli);
            }
            break;
        case 5://milli and minute are not present
            if (choice.equals("hour")) {
                result = String.valueOf(hour);
            } else if (choice.equals("minute")) {
                if (calendar.get(Calendar.SECOND) < second) {
                    result = String.valueOf(calendar.get(Calendar.MINUTE));
                } else {
                    result = String.valueOf(calendar.get(Calendar.MINUTE) + 1);
                }

            } else if (choice.equals("second")) {
                result = String.valueOf(second);
            } else if (choice.equals("milli")) {
                result = "000";
            }
            break;
        case 6://second and minute are not present
            if (choice.equals("hour")) {
                result = String.valueOf(hour);
            } else if (choice.equals("minute")) {
                if (calendar.get(Calendar.HOUR_OF_DAY) == 0) {
                    if (calendar.get(Calendar.SECOND) < second) {
                        result = String.valueOf(calendar.get(Calendar.MINUTE));
                    } else {
                        result = String.valueOf(calendar.get(Calendar.MINUTE) + 1);
                    }
                } else {
                    result = "00";
                }
            } else if (choice.equals("second")) {
                if (calendar.get(Calendar.HOUR_OF_DAY) == hour) {
                    result = String.valueOf(calendar.get(Calendar.SECOND) + 1);
                } else {
                    result = "00";
                }
            } else if (choice.equals("milli")) {
                result = String.valueOf(milli);
            }
            break;
        case 7://milli, second, minute are not present
            if (choice.equals("hour")) {
                result = String.valueOf(hour);
            } else if (choice.equals("minute")) {
                result = "00";
            } else if (choice.equals("second")) {
                result = "00";
            } else if (choice.equals("milli")) {
                result = "000";
            }
            break;
        case 8://hour is not present
            if (choice.equals("hour")) {
                if (calendar.get(Calendar.MINUTE) < minute
                        || (calendar.get(Calendar.MINUTE) == minute && (calendar.get(Calendar.SECOND) <= second))) {
                    result = String.valueOf(calendar.get(Calendar.HOUR_OF_DAY));
                } else {
                    result = String.valueOf(calendar.get(Calendar.HOUR_OF_DAY) + 1);
                }
            } else if (choice.equals("minute")) {
                result = String.valueOf(minute);
            } else if (choice.equals("second")) {
                result = String.valueOf(second);
            } else if (choice.equals("milli")) {
                result = String.valueOf(milli);
            }
            break;
        case 9://milli and hour are not present
            if (choice.equals("hour")) {
                if (calendar.get(Calendar.MINUTE) < minute
                        || (calendar.get(Calendar.MINUTE) == minute && (calendar.get(Calendar.SECOND) <= second))) {
                    result = String.valueOf(calendar.get(Calendar.HOUR_OF_DAY));
                } else {
                    result = String.valueOf(calendar.get(Calendar.HOUR_OF_DAY) + 1);
                }
            } else if (choice.equals("minute")) {
                result = String.valueOf(minute);
            } else if (choice.equals("second")) {
                result = String.valueOf(second);
            } else if (choice.equals("milli")) {
                result = "000";
            }
            break;
        case 10://hour and second are not present
            if (choice.equals("hour")) {
                if (calendar.get(Calendar.MINUTE) < minute
                        || (calendar.get(Calendar.MINUTE) == minute && (calendar.get(Calendar.SECOND) == second))) {
                    result = String.valueOf(calendar.get(Calendar.HOUR_OF_DAY));
                } else {
                    result = String.valueOf(calendar.get(Calendar.HOUR_OF_DAY) + 1);
                }
            } else if (choice.equals("minute")) {
                result = String.valueOf(minute);
            } else if (choice.equals("second")) {
                result = "00";
            } else if (choice.equals("milli")) {
                result = String.valueOf(milli);
            }
            break;
        case 11://milli, second and hour are not present
            if (choice.equals("hour")) {
                if (calendar.get(Calendar.MINUTE) < minute) {
                    result = String.valueOf(calendar.get(Calendar.HOUR_OF_DAY));
                } else {
                    result = String.valueOf(calendar.get(Calendar.HOUR_OF_DAY) + 1);
                }
            } else if (choice.equals("minute")) {
                result = String.valueOf(minute);
            } else if (choice.equals("second")) {
                result = "00";
            } else if (choice.equals("milli")) {
                result = "000";
            }
            break;
        case 12:
            if (choice.equals("hour")) {
                result = String.valueOf(calendar.get(Calendar.HOUR_OF_DAY));
            } else if (choice.equals("minute")) {
                if (calendar.get(Calendar.SECOND) < second) {
                    result = String.valueOf(calendar.get(Calendar.MINUTE));
                } else {
                    result = String.valueOf(calendar.get(Calendar.MINUTE) + 1);
                }
            } else if (choice.equals("second")) {
                result = String.valueOf(second);
            } else if (choice.equals("milli")) {
                result = String.valueOf(milli);
            }
            break;
        case 13:
            if (choice.equals("hour")) {
                result = String.valueOf(calendar.get(Calendar.HOUR_OF_DAY));
            } else if (choice.equals("minute")) {
                if (calendar.get(Calendar.SECOND) < second) {
                    result = String.valueOf(calendar.get(Calendar.MINUTE));
                } else {
                    result = String.valueOf(calendar.get(Calendar.MINUTE) + 1);
                }
            } else if (choice.equals("second")) {
                result = String.valueOf(second);
            } else if (choice.equals("milli")) {
                result = "000";
            }
            break;
        case 14:
            if (choice.equals("hour")) {
                result = String.valueOf(calendar.get(Calendar.HOUR_OF_DAY));
            } else if (choice.equals("minute")) {
                result = String.valueOf(calendar.get(Calendar.MINUTE));
            } else if (choice.equals("second")) {
                if (calendar.getTime().getTime() % 1000 < milli) {
                    result = String.valueOf(calendar.get(Calendar.SECOND));
                } else {
                    result = String.valueOf(calendar.get(Calendar.SECOND) + 1);
                }
            } else if (choice.equals("milli")) {
                result = String.valueOf(milli);
            }
            break;
        case 15://nothing is present
            if (choice.equals("hour")) {
                result = "00";
            } else if (choice.equals("minute")) {
                result = "00";
            } else if (choice.equals("second")) {
                result = "00";
            } else if (choice.equals("milli")) {
                result = "000";
            }
            break;
        }
        return result;
    }
}

Related

  1. getTicksTill(int hour)
  2. getTicksTillHour()
  3. hour()
  4. hour()
  5. hourAgo()
  6. HourNow()
  7. Hours()
  8. hours(int hours)
  9. hoursAndMinsToMilliseconds(int sign, int hours, int minutes)