我正在尝试将日语日期字符串转换为 JapaneseDate。
\nDateTimeFormatter formatter = DateTimeFormatter.ofPattern("Gyy\xe5\xb9\xb4MM\xe6\x9c\x88dd\xe6\x97\xa5")\n .withChronology(JapaneseChronology.INSTANCE)\n .withResolverStyle(ResolverStyle.LENIENT);\nJapaneseDate d = JapaneseDate.from(formatter.parse("\xe5\xb9\xb3\xe6\x88\x9001\xe5\xb9\xb401\xe6\x9c\x8801\xe6\x97\xa5"));\nSystem.out.println(d);\nSystem.out.println(d.format(formatter));\nRun Code Online (Sandbox Code Playgroud)\n输出:
\nJapanese Reiwa 71-01-01\n\xe4\xbb\xa4\xe5\x92\x8c71\xe5\xb9\xb401\xe6\x9c\x8801\xe6\x97\xa5\nRun Code Online (Sandbox Code Playgroud)\n由于“\xe4\xbb\xa4\xe5\x92\x8c\xe5\x85\x83\xe5\xb9\xb4”是“2019”,“\xe4\xbb\xa4\xe5\x92\x8c71\xe5\xb9\ xb401\xe6\x9c\x8801\xe6\x97\xa5" 为 "2089-01-01"。\n"\xe5\xb9\xb3\xe6\x88\x9001\xe5\xb9\xb401\xe6\x9c\x8801 \xe6\x97\xa5" 必须是 "1989-01-01"。
\n我怎样才能正确地转换它?
\n如果我将解析器样式更改为ResolverStyle.STRICT,它会抛出
java.time.format.DateTimeParseException: Text \'\xe5\xb9\xb3\xe6\x88\x9001\xe5\xb9\xb401\xe6\x9c\x8801\xe6\x97\xa5\'\ncould not be parsed: year, month, and day not valid for Era\nRun Code Online (Sandbox Code Playgroud)\n如果我将解析器样式更改为ResolverStyle.SMART,它会抛出
java.time.format.DateTimeParseException: Text \'\xe5\xb9\xb3\xe6\x88\x9001\xe5\xb9\xb401\xe6\x9c\x8801\xe6\x97\xa5\'\ncould not be parsed: Invalid YearOfEra for Era: Heisei 101\nRun Code Online (Sandbox Code Playgroud)\n 我们是否知道是否存在输出与 相同结果的等效格式字符串DateTimeFormatter.ISO_OFFSET_DATE_TIME?
IE
ZonedDateTime dateTime = ZonedDateTime.now();
System.out.println(dateTime.format(DateTimeFormatter.ofPattern(pattern)));
System.out.println(dateTime.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME));
Run Code Online (Sandbox Code Playgroud)
会输出相同的
我正在使用以下 ISO8601 格式:
YYYY-MM-DDThh:mm:ssZ
Run Code Online (Sandbox Code Playgroud)
我曾经OffsetDateTime.parse()解析过这种格式。我能够通过在此处传递t(而不是T) 和z(而不是Z) 来解析日期时间。
那么谁能告诉它在ISO8601中是否允许,还是仅在解析逻辑中被忽略?
我正在编写一个股票程序,它(到目前为止)通过这样的请求从“Markit on Demand”获取数据:
http://dev.markitondemand.com/Api/v2/Quote/xml?symbol=aapl
这将返回 xml 中的数据,以及股票的各种度量(符号、名称、最后价格、变化、时间戳等)。
我在 Java 8 中创建 DateTimeFormatter 来制作时间戳时遇到问题。
时间戳的一个示例:
Fri Jul 18 15:59:00 UTC-04:00 2014
到目前为止,我拥有的模式如下:
EEE MMM d HH:mm:ss OOOO yyyy
我相信你们中的一些人可以发现,我在偏移方面遇到了麻烦。
从文档:
偏移量 X 和 x:这根据模式字母的数量格式化偏移量。一个字母只输出小时,例如“+01”,除非分钟不为零,在这种情况下也会输出分钟,例如“+0130”。两个字母输出小时和分钟,没有冒号,例如“+0130”。三个字母输出小时和分钟,带一个冒号,例如“+01:30”。四个字母输出小时和分钟以及可选的秒,没有冒号,例如“+013015”。五个字母输出小时和分钟以及可选的秒,带有冒号,例如“+01:30:15”。六个或更多字母抛出 IllegalArgumentException。当要输出的偏移量为零时,模式字母 'X'(大写)将输出 'Z',而模式字母 'x'(小写)将输出 '+00', '
Offset O:根据模式字母的数量格式化本地化的偏移量。一个字母输出本地化偏移量的缩写形式,即本地化偏移量文本,例如“GMT”,小时不带前导零,可选的两位数分钟和秒(如果非零)和冒号,例如“GMT+8” '。四个字母输出完整形式,它是本地化的偏移文本,例如'GMT,带有两位数的小时和分钟字段,可选的第二个字段(如果非零)和冒号,例如'GMT+08:00'。任何其他字母计数都会引发 IllegalArgumentException。
偏移 Z:这会根据模式字母的数量格式化偏移量。一、二或三个字母输出小时和分钟,不带冒号,例如“+0130”。当偏移量为零时,输出将为“+0000”。四个字母输出局部偏移的完整形式,相当于Offset-O的四个字母。如果偏移为零,则输出将是相应的本地化偏移文本。五个字母输出小时、分钟,如果非零,可选秒,带冒号。如果偏移为零,则输出“Z”。六个或更多字母抛出 IllegalArgumentException。
// String rawDate = Fri Jul 18 15:59:00 UTC-04:00 2014
DateTimeFormatter PARSER_PATTERN = DateTimeFormatter.ofPattern("EEE MMM d HH:mm:ss 'UTC'XXX yyyy");
ZonedDateTime timeStamp = ZonedDateTime.parse(rawDate, PARSER_PATTERN);
Run Code Online (Sandbox Code Playgroud)
这有效,但我很好奇为什么(代替'UTC'XXX)OOOO不起作用。
我正在尝试使用 解析包含日期和时间的字符串java.time.format.DateTimeFormatter(我的最终目标是将此字符串中的日期转换为java.time.LocalDate)。
尝试解析日期时,我不断收到 DateTimeParseExceptions。有人可以帮忙吗?
日期的格式为“2015-07-14T11:42:12.000+01:00”。
DateTimeFormatter f = DateTimeFormatter.ofPattern("yyyy-MM-ddTHH:mm:ss.SSSZ");
LocalDateTime temp = LocalDateTime.ofInstant(Instant.from(f.parse(this.dateCreated)),
ZoneId.systemDefault());
LocalDate localDate = temp.toLocalDate();
Run Code Online (Sandbox Code Playgroud)
我在 ofPattern 中尝试了不同的变体,例如尝试通过用单引号将 T 括起来(如上所述)来转义 T,并对 . 我试过同时逃避两者。
冒号也需要转义吗?
感谢您对此的任何帮助。
我正在使用 en_GB 语言环境,但类似的问题也可能会影响其他 en_XX 语言环境。
在 Java 15 下,以下代码有效:
LocalDate.parse("10-Sep-17", DateTimeFormatter.ofPattern("dd-MMM-yy", Locale.UK));
在 Java 16 下它给出:DateTimeParseException: Text '10-Sep-17' could not be parsed at index 3
在调试器中花费了很长时间后,我将此追溯到此提交:8251317:支持 CLDR 版本 38
此提交将上下文相关形式和独立形式的 September 缩写形式make/data/cldr/common/main/en_GB.xml从更改Sep为。Sept其他月份都没有被触及,保留为 3 个字符。
我已经证实这确实是CLDR 版本 37 和 38之间的真正变化,尽管我不确定我们英国人何时开始使用 4 个字母作为 9 月的 3 字母缩写......
现在这很烦人,因为它破坏了我的数据文件处理(尽管我怀疑我可以通过Locale.ENGLISH在代码中指定而不是使用默认区域设置来修复它),但我无法确定它是否算作一个已引入的错误打破了我可靠的 3 字符月匹配模式,或者这是否真的意味着一个功能。
JavaDoc 说:
文本:文本样式根据使用的模式字母的数量确定。少于 4 个模式字母将使用缩写形式。...
然后:
数字/文本:如果模式字母的数量为 3 或更多,请使用上面的文本规则。否则请使用上面的编号规则。
我的遗憾是我从来没有仔细阅读过这篇文章,没有发现文本值的处理方式与数字不同,其中模式中的字母数量设置了宽度。但这让我想知道在输出一个月时应该如何指定固定数量的字符,同样为什么它不能在解析时宽容并接受三字符形式而不是抛出异常?
归根结底,这对我来说仍然感觉像是一种回归。我的代码多年来一直可靠地解析包含 3 个字符的月份的日期,现在没有任何警告,但在 …
我们正在尝试编写一个 DateTimeFormatter 来帮助我们验证ISO 8601,该 ISO 8601 允许最终用户仅输入年份、年份和月份,或者年份、月份和日期。我们还想验证输入的日期是否确实存在。
在下面的代码中,有两个日期和日期验证表现得很时髦的示例。第一个是仅针对一年和可选月份的验证测试。这不能正确验证月份“00”。
第二个示例显示测试(NOT)因不正确的可选月份值而失败。
任何友好的指示将不胜感激。
import org.junit.jupiter.api.Test;
import java.time.LocalDate;
import java.time.Year;
import java.time.YearMonth;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.format.DateTimeParseException;
import java.time.format.ResolverStyle;
import java.time.temporal.TemporalQuery;
import static org.junit.jupiter.api.Assertions.assertThrows;
public class DateTimeFormatterForStackOverflowTest {
@Test
public void test_year_or_year_and_month_not_valid() {
DateTimeFormatter formatter = new DateTimeFormatterBuilder()
.appendPattern("uuuu[-MM]")
.toFormatter()
.withResolverStyle(ResolverStyle.STRICT);
this.expectException("1984-0", formatter, YearMonth::from, Year::from);
// Doesn't throw exception.
this.expectException("1984-00", formatter, YearMonth::from, Year::from);
// Doesn't throw exception.
this.expectException("1984-13", formatter, YearMonth::from, Year::from);
// Doesn't throw exception.
this.expectException("1984-99", formatter, YearMonth::from, Year::from);
}
@Test …Run Code Online (Sandbox Code Playgroud) 我有以下格式的日期:1/1/2020 3:4:7 AM我正在尝试使用DateTimeFormatter.
我有以下带有格式化程序的代码来解析它,但它不起作用。
LocalDateTime date = LocalDateTime.parse("1/1/2020 3:4:7 AM", DateTimeFormatter.ofPattern("MM/dd/yyyy hh:mm:ss a"));
Run Code Online (Sandbox Code Playgroud)
我收到以下异常:
java.time.format.DateTimeParseException: Text '1/1/2020 3:4:7 AM' could not be parsed at index 0
谁能帮我?
java java-time localdatetime datetimeformatter java.time.localdatetime
我正在尝试生成一个随机日期和时间,并将其转换为"yyyy-MM-dd'T'HH:mm:ss'Z'"格式。
这是我尝试过的:
public static String generateRandomDateAndTimeInString() {
LocalDate date = LocalDate.now().minus(Period.ofDays((new Random().nextInt(365 * 70))));
System.out.println("date and time :: " + date.toString());
return formatDate(date) ;
}
public static String formatDate(LocalDate date){
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
return dateFormat.format(date);
}
Run Code Online (Sandbox Code Playgroud)
但在该行中dateFormat.format(date),它抱怨:
java.lang.IllegalArgumentException:无法将给定的对象格式化为日期
第二个问题是,print的输出不包含时间:
date :: 1998-12-24
Run Code Online (Sandbox Code Playgroud)
我不知道如何让它工作。
我有几行代码,如下所示
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MMM-yy");
formatter = formatter.withLocale( Locale.getDefault() );
LocalDate date = LocalDate.parse("11-NOV-20", formatter);
Run Code Online (Sandbox Code Playgroud)
这给出了以下异常
Exception in thread "main" java.time.format.DateTimeParseException: Text '11-NOV-20' could not be parsed at index 3
at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1949)
at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1851)
at java.time.LocalDate.parse(LocalDate.java:400)
at src.DateTimeTest.main(DateTimeTest.java:30)
Run Code Online (Sandbox Code Playgroud)
如果我将月份从11 月更改为11 月,效果很好。我从数据库表中获取这个值。我是否必须以编程方式更改它,或者有什么方法可以处理它?