1

我正在尝试创建一个程序来从字符串中解析有意义的日期和时间。我希望能够提供以下类型的输入,并创建一个日期/时间对象:

5 o'clock
5 p.m.
5 a.m.
5
530
530 a.m.
530 p.m.
Tuesday at [insert any above string here]
the 30th at [same as above]
May 12th at [same as above]
today at [same as above]
tomorrow at [same as above]

任何不包含日期/日期的字符串都可以假定为今天,任何没有 AM/PM 指定的时间都可以假定为发生在上午 9 点到晚上 8:59 之间。写完这段代码后,我很快意识到这变得多么混乱:

private void createEvent(String phrase) {
    int hour;
    int day = 0;
    String dayOfWeek = "";

    if (phrase.contains("o'clock")) {
        hour = Integer.parseInt(phrase.substring(phrase.indexOf("o'clock")-3, phrase.indexOf("o'clock")-1).trim());
        out.write(""+hour);
    }

    if (phrase.contains("tomorrow"))
        day = (Calendar.DAY_OF_WEEK % 7)+1;

    if (phrase.contains("sunday") || day == 1) {
        dayOfWeek = "Sunday"; day = 1; }
    else if (phrase.contains("monday") || day == 2) {
        dayOfWeek = "Monday"; day = 2; }
    else if (phrase.contains("tuesday") || day == 3) {
        dayOfWeek = "Tuesday"; day = 3; }
    else if (phrase.contains("wednesday") || day == 4) {
        dayOfWeek = "Wednesday"; day = 4; }
    else if (phrase.contains("thursday") || day == 5) {
        dayOfWeek = "Thursday"; day = 5; }
    else if (phrase.contains("friday") || day == 6) {
        dayOfWeek = "Friday"; day = 6; }
    else if (phrase.contains("saturday") || day == 7) {
        dayOfWeek = "Saturday"; day = 7; }
    else {
        dayOfWeek = "Today"; day = 0; }     
}

任何人都可以提供一些方向吗?

4

2 回答 2

3

你可以使用 DateFormat

String now = new Date().toString();
SimpleDateFormat format = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
Date date = format.parse(now);

也许您可以提前构建一些格式,然后比较解析的日期。

在此处查看教程:http ://www.xyzws.com/Javafaq/how-to-use-simpledateformat-class-formating-parsing-date-and-time/142

于 2013-05-13T05:46:35.570 回答
2

将您的问题分解为可管理的块...

我将首先提供一种方法,您可以根据需要添加新的格式化程序。

我也会尝试找到重用现有代码的方法。时间是您问题的一个非常一致的方面。

这将导致我想到提供某种集中式格式工厂的概念(使管理更容易),但将个别需求分解为更小的可管理块

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class CustomDateFormatter {

    public static void main(String[] args) {
        new CustomDateFormatter();
    }

    public CustomDateFormatter() {

        String values[] = new String[]{
            "5 o'clock",
            "5 p.m.",
            "5 a.m.",
            "5",
            "530",
            "530 a.m.",
            "530 p.m.",};

        for (String value : values) {
            System.out.println(value + " = " + CustomFormatFactory.format(value));
        }
    }

    public static class CustomFormatFactory {

        private static List<CustomFormat> formatters = new ArrayList<>();

        public static Date format(String value) {
            Date date = null;
            for (CustomFormat format : formatters) {
                if (format.canFormat(value)) {
                    date = format.format(value);
                    if (date != null) {
                        break;
                    }
                }
            }
            return date;
        }

        protected static boolean formatTime(String value, Calendar cal) {
            boolean formatted = false;
            if (Character.isDigit(value.charAt(0))) {
                formatted = true;
                StringBuilder timePart = new StringBuilder(4);
                int index = 0;
                while (index < value.length() && Character.isDigit(value.charAt(index))) {
                    timePart.append(value.charAt(index));
                    index++;
                }

                int hour = 0;
                int min = 0;
                if (timePart.length() < 3) {
                    hour = Integer.parseInt(timePart.toString());
                } else {
                    hour = Integer.parseInt(timePart.substring(0, timePart.length() - 2));
                    min = Integer.parseInt(timePart.substring(timePart.length() - 2, 3));
                }

                cal.set(Calendar.HOUR_OF_DAY, hour);
                cal.set(Calendar.MINUTE, min);
                cal.set(Calendar.MILLISECOND, 0);

                String sufix = value.substring(timePart.length()).trim();
                if ("p.m.".equalsIgnoreCase(sufix) || "pm".equalsIgnoreCase(sufix)) {
                    cal.add(Calendar.HOUR, 12);
                }
            }
            return formatted;
        }

        static {
            formatters.add(new SimpleTimeFormat());
        }
    }

    public static interface CustomFormat {

        public boolean canFormat(String value);

        public Date format(String value);
    }

    public static class SimpleTimeFormat implements CustomFormat {

        @Override
        public boolean canFormat(String value) {
            return format(value) != null;
        }

        @Override
        public Date format(String value) {
            Date date = null;
            Calendar cal = Calendar.getInstance();
            if (CustomFormatFactory.formatTime(value, cal)) {
                date = cal.getTime();
            }
            return date;
        }
    }
}

对于扩展的日期格式,我会提取日期部分,Calendar基于日期构建一个,将时间分区单独提取String并使用该CustomFormatFactory.formatTime方法来格式化时间,这样就省去了每次都重新编码的麻烦.

同样,日期格式化程序可以简单地在内部有一个时间格式化程序的实例,或者您可以将工厂设置为具有多种格式化方法,一种采用时间值并循环通过可用的时间格式化程序......这将是我个人的选择;)

于 2013-05-13T06:12:29.920 回答