我正在尝试创建一个程序来解析字符串中有意义的日期和时间。我希望能够提供以下类型的输入,并创建日期/时间对象:
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; }
}
任何人都可以提供一些方向吗?
答案 0 :(得分: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
答案 1 :(得分: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
方法格式化时间,因此,每次都必须重新编码,以节省你的自我。
同样,日期格式化程序可以简单地在内部具有时间格式化程序的实例,或者您可以将工厂设置为具有多种格式方法,一种采用时间值并循环通过可用时间格式化程序...这将是我的个人选择;)