我的信用卡处理器要求我从信用卡到期日发送两位数的年份.以下是我目前的处理方式:
DropDownList在页面上放了一个4位数的年份.DateTime字段中验证到期日期,以确保传递给CC处理器的到期日期未过期.是否有方法将四位数年份转换为两位数年份.我没有在物体上看到任何东西DateTime.或者我应该像我一样继续处理它?
我需要解析代表格式6位数的日期字符串yymmdd,其中yy范围从59到05年(1959至2005年).根据time模块文档,Python的默认透视年份是1969年,这对我不起作用.
是否有一种简单的方法可以覆盖枢轴年,或者您可以建议其他解决方案吗?我使用的是Python 2.7.谢谢!
我正在使用这个DateTimeFormatter:
DateTimeFormatter.ofPattern("ddMMYY")
Run Code Online (Sandbox Code Playgroud)
我想解析字符串150790,我得到了这个错误:
Unable to obtain LocalDate from TemporalAccessor: {DayOfMonth=15, MonthOfYear=7, WeekBasedYear[WeekFields[MONDAY,4]]=2090},ISO of type java.time.format.Parsed
Run Code Online (Sandbox Code Playgroud)
显然,我希望得到以下内容TemporalAccessor:
{DayOfMonth=15, MonthOfYear=7, WeekBasedYear=1990}
Run Code Online (Sandbox Code Playgroud)
你知道我为什么得到2090而不是1990年?
谢谢你的帮助
如果我使用类似于d/M/yy创建Java 8 DateTimeFormatter的模式(例如使用DateTimeFormatter.ofPattern(pattern);(我将仅用于解析,而不是格式化),它将所有两个字母的年份解释为20xx,例如解析一个字符串,如同13/5/99被解释为2099-05-13,在我的情况下是错误的(它应该是在1999年).
在我的应用程序中,我试图解析OCR'd文档中的日期,例如仍然可以从90年代开始,所以使用旧的SimpleDateFormat行为将日期解释为在当前日期之前和之后的20年之内我很好.但由于各种原因,我仍然希望将整个日期解析逻辑切换到新的Java 8 DateTimeFormatters.
通过Java源代码,我看到这都是相对于常量解释的ReducedPrinterParser.BASE_DATE,但我认为无法更改从模式构建自己的格式化程序时使用的值.这是不可能的,还是我错过了指定解析两个字母年份的行为的可能性?
我希望从我的项目中删除Joda-Time库.
我想把两位数转换成全年.Joda-Time的以下代码可以实现此目的.以下是joda-time的以下代码
DateTimeFormatter TWO_YEAR_FORMATTER = DateTimeFormat.forPattern("yy");
int year = LocalDate.parse("99"", TWO_YEAR_FORMATTER).getYear();
System.out.println(year);
Run Code Online (Sandbox Code Playgroud)
产出:1999年
这是我期望的输出,这在我的情况下是有意义的.但是,当我使用java.time API尝试相同的过程时,它会生成DatetimeParseException.以下是java.time API的以下代码:
DateTimeFormatter TWO_YEAR_FORMATTER = DateTimeFormatter.ofPattern("yy");
int year = LocalDate.parse("99", TWO_YEAR_FORMATTER).getYear();
System.out.println(year);
Run Code Online (Sandbox Code Playgroud)
堆栈跟踪:
Exception in thread "main" java.time.format.DateTimeParseException: Text '99' could not be parsed: Unable to obtain LocalDate from TemporalAccessor: {Year=2099},ISO of type java.time.format.Parsed
at java.base/java.time.format.DateTimeFormatter.createError(DateTimeFormatter.java:2017)
at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1952)
at java.base/java.time.LocalDate.parse(LocalDate.java:428)
at scratch.main(scratch.java:10)
Caused by: java.time.DateTimeException: Unable to obtain LocalDate from TemporalAccessor: {Year=2099},ISO of type java.time.format.Parsed
at java.base/java.time.LocalDate.from(LocalDate.java:396)
at java.base/java.time.format.Parsed.query(Parsed.java:235)
at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1948)
... 2 more
Run Code Online (Sandbox Code Playgroud)
我没能看到堆栈跟踪的原因.如果有人可以帮助我理解以下场景并且还解释如何使用Java 8时间API将两位数年份转换为全年,那将是很好的.
考虑以下生日(as dob):
用Python解析时datetime.strptime(dob, '%d-%b-%y')会产生:
datetime.datetime(2068, 6, 1, 0, 0)datetime.datetime(1969, 6, 1, 0, 0)当然,他们应该出生在同一个十年,但现在甚至不是在同一个世纪!
根据文档,这是完全有效的行为:
当接受2位数年份时,它们将根据POSIX或X/Open标准进行转换:值69-99映射到1969-1999,值0-68映射到2000-2068.
我理解为什么函数设置这样,但是有办法解决这个问题吗?也许定义自己的两位数年份范围?
这个问题涉及在Java 8/JSR 310/threeten.org backport中解析2位数年份.两位数的年份是一个不幸的事实,需要处理.
我只关心ISO日历.默认情况下,JSR 310假定2000年的"基准年"为2位数年份.
我看到对基准年的控制可以通过
DateTimeFormatterBuilder.appendValueReduced(...)
Run Code Online (Sandbox Code Playgroud)
格式模式提供了指定日期格式的便捷机制
dd-MMM-yy
M/d/yy
d/M/yy
Run Code Online (Sandbox Code Playgroud)
在应用格式模式后,我没有看到为两位数年份应用"基准年"的机制.
Joda-time DateTimeFormatter.withPivotYear(int pivotYear)作为解决这个问题的机制.
在JSR 310中,是否有一种机制可以在指定模式后为2位数年份应用基数?
解析基准年可能不是2000年的2位数年份的最佳做法建议是什么?
我有以下(略有缩写)代码来解析String输入到Joda Time DateTime对象.我需要正确处理多种格式,包括四位和两位数年份.
setupValidDateFormats();
DateTime date = convertToDateTime(thisField.getText());
if (date != null) {
thisField.setText(date.toString("MM/dd/yyyy"));
} else {
System.out.println("Invalid date");
}
private void setupValidDateFormats() {
DateTimeParser[] formats = {
DateTimeFormat.forPattern("MM/dd/yyyy").getParser(),
DateTimeFormat.forPattern("MM/dd/yy").getParser(),
DateTimeFormat.forPattern("MM-dd-yyyy").getParser(),
DateTimeFormat.forPattern("MM-dd-yy").getParser(),
DateTimeFormat.forPattern("MMddyyyy").getParser(),
DateTimeFormat.forPattern("MMddyy").getParser()
};
formatter = new DateTimeFormatterBuilder().append(null, formats).appendTwoDigitYear(1950, true).toFormatter()
.withLocale(Locale.US);
}
private DateTime convertToDateTime(String input) {
if (isNullOrEmpty(input)) {
return null;
}
DateTime date;
try {
// When you parse a date, it'll throw an exception for not only invalid formats,
// but for invalid …Run Code Online (Sandbox Code Playgroud) DateTimeFormatterjava.time中的类提供了三种ofLocalized…生成字符串的方法,以表示包含一年的值.例如,ofLocalizedDate.
Locale l = Locale.US ;
DateTimeFormatter f = DateTimeFormatter.ofLocalizedDate( FormatStyle.SHORT ).withLocale( l );
LocalDate today = LocalDate.now( ZoneId.of( "America/Chicago" ) );
String output = today.format( f );
Run Code Online (Sandbox Code Playgroud)
对于我看过的语言环境,年份只是较短FormatStyle样式的两位数.
如何让java.time本地化却迫使年份变为四位数而不是两位数?
我怀疑答案在DateTimeFormatterBuilder课堂上.但我找不到任何改变一年的长度的功能.我也仔细阅读了Java 9源代码,但是不能很好地解释代码以找到答案.
这个问题类似于:
...但是这些问题针对的是现在由java.time类取代的旧日期时间框架.
我如何将这样格式的两位数年份转换为四年:
02 -> 2002
87 -> 1987
Run Code Online (Sandbox Code Playgroud)
ETC...
到目前为止我所拥有的:
char shortYr[3];
char longYr[5];
scanf("%2s", shortYr);
int shortYrAsInt = atoi(shortYr);
if (shortYrAsInt < 99)
;
Run Code Online (Sandbox Code Playgroud)
我该如何转换它?在网上,我读到了如何将 4 位数字转换为 2 位数字,这很容易,但是其他方法呢?
fourdigityear<-c("2003/7/15","2009/6/12")
Run Code Online (Sandbox Code Playgroud)
我应该如何将4位数年份转换为2位数年份,以便最终得到类似"7/15/03","12/6/09"的内容.