Parsing user time input in Java/GWT

社会主义新天地 提交于 2019-12-24 03:24:13

问题


What is the best way to parse time that a user typed in a text field in GWT? Default time formats require users to enter time exactly as the time format for locale specifies it.

I want to be more flexible as there are many different ways users can enter time. For example, entries like "8", "8p", "8pm", "8.15pm", "13:15", "1315", "13.15" should be valid.


回答1:


I ended up with my own method that I want to share. This method returns time in milliseconds, which can be displayed using any data formats for the selected locale.

Any suggestions to improve it are highly appreciated.

EDIT: Improved following suggestions in comments.

public static Long parseTime(String value) {

    // ";" is a common typo - we are not punishing users for it
    value = value.trim().toLowerCase().replace(";", ":");

    RegExp time12 = RegExp.compile("^(1[012]|[1-9])([:.][0-5][0-9])?(\\s)?(a|p|am|pm)?$");
    RegExp time24 = RegExp.compile("^(([01]?[0-9]|2[0-3])[:.]?([0-5][0-9])?)$");

    if (time12.test(value) || time24.test(value)) {

        String hours = "0", minutes = "0";

        if (value.contains(":") || value.contains(".")) {
            String[] values = value.split("[:.]");
            hours =  values[0];
            minutes = values[1].substring(0, 2);
        } else {
            // Process strings like "8", "8p", "8pm", "2300"
            if (value.contains("a")) {
                hours = value.substring(0, value.indexOf("a")).trim();
            } else if (value.contains("p")) {
                hours = value.substring(0, value.indexOf("p")).trim();
            } else if (value.length() < 3) {
                hours = value;
            } else {
                hours =  value.substring(0, value.length() - 2);
                minutes = value.substring(value.length() - 2);
            }
        }
        if (value.contains("a") && hours.equals("12")) {
            // 12am is actually zero hours
            hours = "0";
        }

        Long time = (Long.valueOf(hours) * 60 + Long.valueOf(minutes)) * 60 * 1000;

        if (value.contains("p") && !hours.equals("12")) {
            // "pm" adds 12 hours to the total, except for 12pm
            time += 12 * 60 * 60 * 1000;
        }

        return time;
    }
    return null;
}


来源:https://stackoverflow.com/questions/16683279/parsing-user-time-input-in-java-gwt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!