Skip to main content
added comments
Source Link
200_success
  • 144.2k
  • 22
  • 188
  • 473

You're working too hard.

Parsing is easier using a regular expression (import java.util.regex.*).

Figuring out the mirrored time is easier if you convert everything into minutes first. Then the code would model how an analog clock works. You don't want your code to be littered with special cases.

Formatting the output is easier using String.format().

private static final int MINUTES_PER_HALF_DAY = 12 * 60;

private static final Pattern TIME_FMT = Pattern.compile(
    "(0?[1-9]|1[0-2])" +  // Group 1: Hour 01-12 (optional leading zero)
    ":" +                 //  colon
    "([0-5]\\d)\\s*" +        // Group 2: Minute 00-59 (0-5 followed by any 2nd digit)
    "\\s*" +              //  optional whitespace
    "([ap]m)", Pattern.CASE_INSENSITIVE);
private static final int MINUTES_PER_HALF_DAY = 12 * 60;    // Group 3: am or pm
    , Pattern.CASE_INSENSITIVE);

public static String parallelUniverseTime(String time) {
    Matcher m = TIME_FMT.matcher(time);
    if (!m.matches()) {
        return null;
    } 

    // Hour as an int, but mapping "12" to 0
    int hour = Integer.parseInt(m.group(1)) % 12;
    int minute = Integer.parseInt(m.group(2));
    String ampm = m.group(3).toLowerCase();

    int minutes = 60 * hour + minute;

    //  ^^^ Normal universe ^^^
    //        ** WARP! **
    // vvv Parallel universe vvv

    minutes = MINUTES_PER_HALF_DAY - minutes;

    // Likely 0  hour =< minutes12, /but 60;
possibly hour = 12 if (hourminutes == 0) {.
    // We'll canonicalize hour = 0 and hour = 12;12 to "12" on output.
    }hour = minutes / 60;
    minute = minutes % 60;
    ampm = ("pm".equals(ampm)) ? "am" : "pm";

    return String.format("%02d:%02d %s", (hour == 0 ? 12 : hour), minute, ampm);
}

You're working too hard.

Parsing is easier using a regular expression (import java.util.regex.*).

Figuring out the mirrored time is easier if you convert everything into minutes first. Then the code would model how an analog clock works. You don't want your code to be littered with special cases.

Formatting the output is easier using String.format().

private static final Pattern TIME_FMT = Pattern.compile("(0?[1-9]|1[0-2]):([0-5]\\d)\\s*([ap]m)", Pattern.CASE_INSENSITIVE);
private static final int MINUTES_PER_HALF_DAY = 12 * 60;

public static String parallelUniverseTime(String time) {
    Matcher m = TIME_FMT.matcher(time);
    if (!m.matches()) {
        return null;
    }

    int hour = Integer.parseInt(m.group(1)) % 12;
    int minute = Integer.parseInt(m.group(2));
    String ampm = m.group(3).toLowerCase();

    int minutes = 60 * hour + minute;

    //  ^^^ Normal universe ^^^
    //        ** WARP! **
    // vvv Parallel universe vvv

    minutes = MINUTES_PER_HALF_DAY - minutes;

    hour = minutes / 60;
    if (hour == 0) {
        hour = 12;
    }
    minute = minutes % 60;
    ampm = ("pm".equals(ampm)) ? "am" : "pm";

    return String.format("%02d:%02d %s", hour, minute, ampm);
}

You're working too hard.

Parsing is easier using a regular expression (import java.util.regex.*).

Figuring out the mirrored time is easier if you convert everything into minutes first. Then the code would model how an analog clock works. You don't want your code to be littered with special cases.

Formatting the output is easier using String.format().

private static final int MINUTES_PER_HALF_DAY = 12 * 60;

private static final Pattern TIME_FMT = Pattern.compile(
    "(0?[1-9]|1[0-2])" +  // Group 1: Hour 01-12 (optional leading zero)
    ":" +                 //  colon
    "([0-5]\\d)" +        // Group 2: Minute 00-59 (0-5 followed by any 2nd digit)
    "\\s*" +              //  optional whitespace
    "([ap]m)"             // Group 3: am or pm
    , Pattern.CASE_INSENSITIVE);

public static String parallelUniverseTime(String time) {
    Matcher m = TIME_FMT.matcher(time);
    if (!m.matches()) {
        return null;
    } 

    // Hour as an int, but mapping "12" to 0
    int hour = Integer.parseInt(m.group(1)) % 12;
    int minute = Integer.parseInt(m.group(2));
    String ampm = m.group(3).toLowerCase();

    int minutes = 60 * hour + minute;

    //  ^^^ Normal universe ^^^
    //        ** WARP! **
    // vvv Parallel universe vvv

    minutes = MINUTES_PER_HALF_DAY - minutes;

    // Likely 0  hour < 12, but possibly hour = 12 if minutes == 0.
    // We'll canonicalize hour = 0 and hour = 12 to "12" on output.
    hour = minutes / 60;
    minute = minutes % 60;
    ampm = ("pm".equals(ampm)) ? "am" : "pm";

    return String.format("%02d:%02d %s", (hour == 0 ? 12 : hour), minute, ampm);
}
added 115 characters in body
Source Link
200_success
  • 144.2k
  • 22
  • 188
  • 473

You're working too hard.

Parsing is easier using a regular expression (import java.util.regex.*).

Figuring out the mirrored time is easier if you convert everything into minutes first. Then the code would model how an analog clock works. You don't want your code to be littered with special cases.

Formatting the output is easier using String.format().

private static final Pattern TIME_FMT = Pattern.compile("(0?[1-9]|1[0-2]):([0-5]\\d)\\s*([ap]m)", Pattern.CASE_INSENSITIVE);
private static final int MINUTES_PER_HALF_DAY = 12 * 60;

public static String parallelUniverseTime(String time) {
    Matcher m = TIME_FMT.matcher(time);
    if (!m.matches()) {
        return null;
    }

    int hour = Integer.parseInt(m.group(1)) % 12;
    int minute = Integer.parseInt(m.group(2));
    String ampm = m.group(3).toLowerCase();

    int minutes = 60 * hour + minute;

    //  ^^^ Normal universe ^^^
    //        ** WARP! **
    // vvv Parallel universe vvv

    minutes = MINUTES_PER_HALF_DAY - minutes;

    hour = minutes / 60;
    if (hour == 0) {
        hour = 12;
    }
    minute = minutes % 60;
    ampm = ("pm".equals(ampm)) ? "am" : "pm";

    return String.format("%02d:%02d %s", hour, minute, ampm);
}

You're working too hard.

Parsing is easier using a regular expression (import java.util.regex.*).

Figuring out the mirrored time is easier if you convert everything into minutes first.

Formatting the output is easier using String.format().

private static final Pattern TIME_FMT = Pattern.compile("(0?[1-9]|1[0-2]):([0-5]\\d)\\s*([ap]m)", Pattern.CASE_INSENSITIVE);
private static final int MINUTES_PER_HALF_DAY = 12 * 60;

public static String parallelUniverseTime(String time) {
    Matcher m = TIME_FMT.matcher(time);
    if (!m.matches()) {
        return null;
    }

    int hour = Integer.parseInt(m.group(1)) % 12;
    int minute = Integer.parseInt(m.group(2));
    String ampm = m.group(3).toLowerCase();

    int minutes = 60 * hour + minute;

    //  ^^^ Normal universe ^^^
    //        ** WARP! **
    // vvv Parallel universe vvv

    minutes = MINUTES_PER_HALF_DAY - minutes;

    hour = minutes / 60;
    if (hour == 0) {
        hour = 12;
    }
    minute = minutes % 60;
    ampm = ("pm".equals(ampm)) ? "am" : "pm";

    return String.format("%02d:%02d %s", hour, minute, ampm);
}

You're working too hard.

Parsing is easier using a regular expression (import java.util.regex.*).

Figuring out the mirrored time is easier if you convert everything into minutes first. Then the code would model how an analog clock works. You don't want your code to be littered with special cases.

Formatting the output is easier using String.format().

private static final Pattern TIME_FMT = Pattern.compile("(0?[1-9]|1[0-2]):([0-5]\\d)\\s*([ap]m)", Pattern.CASE_INSENSITIVE);
private static final int MINUTES_PER_HALF_DAY = 12 * 60;

public static String parallelUniverseTime(String time) {
    Matcher m = TIME_FMT.matcher(time);
    if (!m.matches()) {
        return null;
    }

    int hour = Integer.parseInt(m.group(1)) % 12;
    int minute = Integer.parseInt(m.group(2));
    String ampm = m.group(3).toLowerCase();

    int minutes = 60 * hour + minute;

    //  ^^^ Normal universe ^^^
    //        ** WARP! **
    // vvv Parallel universe vvv

    minutes = MINUTES_PER_HALF_DAY - minutes;

    hour = minutes / 60;
    if (hour == 0) {
        hour = 12;
    }
    minute = minutes % 60;
    ampm = ("pm".equals(ampm)) ? "am" : "pm";

    return String.format("%02d:%02d %s", hour, minute, ampm);
}
added 29 characters in body
Source Link
200_success
  • 144.2k
  • 22
  • 188
  • 473

You're working too hard.

Parsing is easier using a regular expression (import java.util.regex.*).

Figuring out the mirrored time is easier if you convert everything into minutes first.

Formatting the output is easier using String.format().

private static final Pattern TIME_FMT = Pattern.compile("(0?[1-9]|1[0-2]):([0-5]\\d)\\s*([ap]m)", Pattern.CASE_INSENSITIVE);
private static final int MINUTES_PER_HALF_DAY = 12 * 60;

public static String parallelUniverseTime(String time) {
    Matcher m = TIME_FMT.matcher(time);
    if (!m.matches()) {
        return null;
    }

    int hour = Integer.parseInt(m.group(1)) % 12;
    int minute = Integer.parseInt(m.group(2));
    String ampm = m.group(3).toLowerCase();

    int minutes = 60 * hour + minute;

    //  ^^^ Normal universe ^^^
    //        ** WARP! **
    // vvv Parallel universe vvv

    minutes = MINUTES_PER_HALF_DAY - minutes;

    hour = minutes / 60;
    if (hour == 0) {
        hour = 12;
    }
    minute = minutes % 60;
    ampm = ("pm".equals(ampm)) ? "am" : "pm";

    return String.format("%02d:%02d %s", hour, minute, ampm);
}

You're working too hard.

Parsing is easier using a regular expression.

Figuring out the mirrored time is easier if you convert everything into minutes first.

Formatting the output is easier using String.format().

private static final Pattern TIME_FMT = Pattern.compile("(0?[1-9]|1[0-2]):([0-5]\\d)\\s*([ap]m)", Pattern.CASE_INSENSITIVE);
private static final int MINUTES_PER_HALF_DAY = 12 * 60;

public static String parallelUniverseTime(String time) {
    Matcher m = TIME_FMT.matcher(time);
    if (!m.matches()) {
        return null;
    }

    int hour = Integer.parseInt(m.group(1)) % 12;
    int minute = Integer.parseInt(m.group(2));
    String ampm = m.group(3).toLowerCase();

    int minutes = 60 * hour + minute;

    //  ^^^ Normal universe ^^^
    //        ** WARP! **
    // vvv Parallel universe vvv

    minutes = MINUTES_PER_HALF_DAY - minutes;

    hour = minutes / 60;
    if (hour == 0) {
        hour = 12;
    }
    minute = minutes % 60;
    ampm = ("pm".equals(ampm)) ? "am" : "pm";

    return String.format("%02d:%02d %s", hour, minute, ampm);
}

You're working too hard.

Parsing is easier using a regular expression (import java.util.regex.*).

Figuring out the mirrored time is easier if you convert everything into minutes first.

Formatting the output is easier using String.format().

private static final Pattern TIME_FMT = Pattern.compile("(0?[1-9]|1[0-2]):([0-5]\\d)\\s*([ap]m)", Pattern.CASE_INSENSITIVE);
private static final int MINUTES_PER_HALF_DAY = 12 * 60;

public static String parallelUniverseTime(String time) {
    Matcher m = TIME_FMT.matcher(time);
    if (!m.matches()) {
        return null;
    }

    int hour = Integer.parseInt(m.group(1)) % 12;
    int minute = Integer.parseInt(m.group(2));
    String ampm = m.group(3).toLowerCase();

    int minutes = 60 * hour + minute;

    //  ^^^ Normal universe ^^^
    //        ** WARP! **
    // vvv Parallel universe vvv

    minutes = MINUTES_PER_HALF_DAY - minutes;

    hour = minutes / 60;
    if (hour == 0) {
        hour = 12;
    }
    minute = minutes % 60;
    ampm = ("pm".equals(ampm)) ? "am" : "pm";

    return String.format("%02d:%02d %s", hour, minute, ampm);
}
Source Link
200_success
  • 144.2k
  • 22
  • 188
  • 473
Loading