从 SimpleDateFormat 迁移到 DateTimeFormater:额外输入
Migrating from SimpleDateFormat to DateTimeFormater: extra input
我正在将一些旧代码从 SimpleDateFormat 迁移到 DateTimeFormatter。 (Apache MIME4J 库,这将释放显着的性能提升!)
在电子邮件领域工作,我需要遵守 RFC-5322 并想出了以下格式化程序:
public static final DateTimeFormatter RFC_5322 = new DateTimeFormatterBuilder()
.parseCaseInsensitive()
.parseLenient()
.optionalStart()
.appendText(DAY_OF_WEEK, dayOfWeek())
.appendLiteral(", ")
.optionalEnd()
.appendValue(DAY_OF_MONTH, 1, 2, SignStyle.NOT_NEGATIVE)
.appendLiteral(' ')
.appendText(MONTH_OF_YEAR, monthOfYear())
.appendLiteral(' ')
.appendValueReduced(YEAR, 2, 4, INITIAL_YEAR)
.appendLiteral(' ')
.appendValue(HOUR_OF_DAY, 2)
.appendLiteral(':')
.appendValue(MINUTE_OF_HOUR, 2)
.optionalStart()
.appendLiteral(':')
.appendValue(SECOND_OF_MINUTE, 2)
.optionalEnd()
.optionalStart()
.appendLiteral('.')
.appendValue(MILLI_OF_SECOND, 3)
.optionalEnd()
.optionalStart()
.appendLiteral(' ')
.appendOffset("+HHMM", "GMT")
.optionalEnd()
.optionalStart()
.appendLiteral(' ')
.appendOffsetId()
.optionalEnd()
.optionalStart()
.appendLiteral(' ')
.appendPattern("0000")
.optionalEnd()
.optionalStart()
.appendLiteral(' ')
.appendPattern("(zzz)")
.optionalEnd()
.toFormatter()
.withZone(ZoneId.of("GMT"));
对于 Thu, 4 Oct 2001 20:12:26 -0700 (PDT)
.
这样的输入效果很好
然而一些边界电子邮件在之后有额外的字符:Date: Thu, 4 Oct 2001 20:12:26 -0700 (PDT),Thu, 4 Oct 2001 20:12:26 -0700
并导致解析失败...
我想要某种通配符来表示“现在您可以自由地忽略额外的输入”...
基于 SimpleDateFormat 的先前版本可以很好地处理此问题...
这是拉取请求的 link:https://github.com/apache/james-mime4j/pull/44
提前感谢您的帮助!
RFC_5322.parse(body, new ParsePosition(0)) 解决了这个...
DateTimeFormatter#parse(CharSequence, ParsePosition)
任您支配。
演示:
import java.text.ParsePosition;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
String s = "08/01/2021&&";
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("MM/dd/uuuu", Locale.ENGLISH);
LocalDate date = LocalDate.from(dtf.parse(s, new ParsePosition(0)));
System.out.println(date);
}
}
输出:
2021-08-01
我正在将一些旧代码从 SimpleDateFormat 迁移到 DateTimeFormatter。 (Apache MIME4J 库,这将释放显着的性能提升!)
在电子邮件领域工作,我需要遵守 RFC-5322 并想出了以下格式化程序:
public static final DateTimeFormatter RFC_5322 = new DateTimeFormatterBuilder()
.parseCaseInsensitive()
.parseLenient()
.optionalStart()
.appendText(DAY_OF_WEEK, dayOfWeek())
.appendLiteral(", ")
.optionalEnd()
.appendValue(DAY_OF_MONTH, 1, 2, SignStyle.NOT_NEGATIVE)
.appendLiteral(' ')
.appendText(MONTH_OF_YEAR, monthOfYear())
.appendLiteral(' ')
.appendValueReduced(YEAR, 2, 4, INITIAL_YEAR)
.appendLiteral(' ')
.appendValue(HOUR_OF_DAY, 2)
.appendLiteral(':')
.appendValue(MINUTE_OF_HOUR, 2)
.optionalStart()
.appendLiteral(':')
.appendValue(SECOND_OF_MINUTE, 2)
.optionalEnd()
.optionalStart()
.appendLiteral('.')
.appendValue(MILLI_OF_SECOND, 3)
.optionalEnd()
.optionalStart()
.appendLiteral(' ')
.appendOffset("+HHMM", "GMT")
.optionalEnd()
.optionalStart()
.appendLiteral(' ')
.appendOffsetId()
.optionalEnd()
.optionalStart()
.appendLiteral(' ')
.appendPattern("0000")
.optionalEnd()
.optionalStart()
.appendLiteral(' ')
.appendPattern("(zzz)")
.optionalEnd()
.toFormatter()
.withZone(ZoneId.of("GMT"));
对于 Thu, 4 Oct 2001 20:12:26 -0700 (PDT)
.
然而一些边界电子邮件在之后有额外的字符:Date: Thu, 4 Oct 2001 20:12:26 -0700 (PDT),Thu, 4 Oct 2001 20:12:26 -0700
并导致解析失败...
我想要某种通配符来表示“现在您可以自由地忽略额外的输入”...
基于 SimpleDateFormat 的先前版本可以很好地处理此问题...
这是拉取请求的 link:https://github.com/apache/james-mime4j/pull/44
提前感谢您的帮助!
RFC_5322.parse(body, new ParsePosition(0)) 解决了这个...
DateTimeFormatter#parse(CharSequence, ParsePosition)
任您支配。
演示:
import java.text.ParsePosition;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
String s = "08/01/2021&&";
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("MM/dd/uuuu", Locale.ENGLISH);
LocalDate date = LocalDate.from(dtf.parse(s, new ParsePosition(0)));
System.out.println(date);
}
}
输出:
2021-08-01