如何将 Joda-Time DateTimeFormat.forStyle() 转换为 JSR 310 Java 时间?

2023-12-13

我正在将 Grails Joda-Time 插件转换为JavaTime.

我有旧的乔达时间代码,如下所示:

    def style
    switch (type) {
        case LocalTime:
            style = '-S'
            break
        case LocalDate:
            style = 'S-'
            break
        default:
            style = 'SS'
    }
    Locale locale = LocaleContextHolder.locale
    return DateTimeFormatter.ofPattern(style, locale).withResolverStyle(ResolverStyle.LENIENT)

如何将其转换为 JSR 310? 我找不到类似的方法forStyle(字符串样式)它接受风格。

UPD我找到了解决方法:

        Locale locale = LocaleContextHolder.locale
        DateTimeFormatter formatter
        switch (type) {
            case LocalTime:
                formatter = DateTimeFormatter.ofLocalizedTime(FormatStyle.SHORT).withLocale(locale)
                break
            case LocalDate:
                formatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT).withLocale(locale)
                break
            default:
                formatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT).withLocale(locale)
        }
        return formatter

但它失败了Instant类型。 Spock 重现规范:

def 'Instant locale formatting'() {
    given:
    Instant inst = Instant.ofEpochMilli(92554380000L)
    DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT).withLocale(UK)
    expect:
    formatter.format(inst) == "07/12/72 05:33"
}

该测试失败并出现错误:

java.time.temporal.UnsupportedTemporalTypeException: Unsupported field: DayOfMonth
    at java.time.Instant.getLong(Instant.java:603)
    at java.time.format.DateTimePrintContext$1.getLong(DateTimePrintContext.java:205)
    at java.time.format.DateTimePrintContext.getValue(DateTimePrintContext.java:298)
    at java.time.format.DateTimeFormatterBuilder$NumberPrinterParser.format(DateTimeFormatterBuilder.java:2543)
    at java.time.format.DateTimeFormatterBuilder$CompositePrinterParser.format(DateTimeFormatterBuilder.java:2182)
    at java.time.format.DateTimeFormatterBuilder$LocalizedPrinterParser.format(DateTimeFormatterBuilder.java:4350)
    at java.time.format.DateTimeFormatterBuilder$CompositePrinterParser.format(DateTimeFormatterBuilder.java:2182)
    at java.time.format.DateTimeFormatter.formatTo(DateTimeFormatter.java:1744)
    at java.time.format.DateTimeFormatter.format(DateTimeFormatter.java:1718)

那么,为什么格式化程序无法格式化Instant?


方法ofLocalizedDate(), ofLocalizedTime() and ofLocalizedDateTime()提供本地化格式。

格式化Instant需要时区。可以使用以下命令将其添加到格式化程序中withZone():

DateTimeFormatter formatter =
    DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT)
                     .withLocale(UK)
                     .withZone(ZoneId.systemDefault());

如果没有该区域,JSR-310 格式化程序就不知道如何将即时日期时间字段转换为人类日期时间字段。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何将 Joda-Time DateTimeFormat.forStyle() 转换为 JSR 310 Java 时间? 的相关文章

随机推荐

  • 在谷歌脚本中将谷歌电子表格转换为xls [重复]

    这个问题在这里已经有答案了 可能的重复 Google Apps 脚本通过电子邮件发送 Google 电子表格 Excel 版本 我到处寻找这个但无法让它工作 我正在发送一封电子邮件 其中包含谷歌应用程序脚本中的附件 该文档是一个谷歌电子表格
  • C 中的 FILE 关键字到底是什么?

    我已经开始学习一些 C 作为一种爱好 并且已经盲目地使用 FILE 作为文件指针的声明相当长一段时间了 我一直在想 这是 C 用来处理文件的关键字或特殊数据类型吗 它是否包含文件内的流和其他数据 为什么定义为指针呢 举一个例子来说明我的意思
  • jQuery - 在文本后添加元素

    我有一个带有一些链接的导航 ul class nav li a class active linkname a li li li ul 现在我需要直接在 linkname 之后添加额外的内容 如下所示 ul class nav li a c
  • 当我的应用程序被删除时呼叫状态更改网络服务

    在我的应用程序中 我使用网络服务 当用户登录到我的应用程序时 我的应用程序发送状态为 1 的请求意味着已登录 当单击注销按钮时 单击发送状态为 0 的请求 现在的问题是 当用户从设备中删除应用程序时 我的服务器中的状态保持为 1 登录 因此
  • 我创建了一个自定义 powershell .psm1 模块,但编辑后它不会更新

    我在中创建了一个自定义的powershell模块 C Program Files WindowsPowerShell Modules PennoniAppManagement目录 每当我对模块中的函数进行更改 然后将模块导入到脚本中时 更新
  • AllowHtml 不适用于 ASP.Net Mvc 3 站点

    我们尝试在 ViewModel 属性之一上使用 AllowHtml 装饰 以便避免 YSOD 从以下位置检测到潜在危险的 Request Form 值 客户端 请求文本 br 当我们尝试提交 html 文本时 例如 br 然后我们希望在控制
  • PushBots 应用程序崩溃

    我正在制作一个 Android 应用程序 它使用 PushBots 来使用推送通知服务 我想要做的是 当我收到推送通知时 我想将消息保存在 TextView 中 以便我可以拥有所有收到消息的历史记录 我做了 PushBots 网站上写的所有
  • 如何使用 Orion 在传出通知中添加自定义标头?

    我阅读了 自定义通知 部分NGSv2 规范我仍然对如何进行这项工作存有疑问 我是否必须将以下代码作为有效负载放入订阅 POST 中 httpCustom url http foo com entity id headers Content
  • 如何从文件中删除与正则表达式不匹配的行?

    我有一个大文件 如下所示 7f0c41d6 f9c6 47aa a034 d40bc629c973 csv 159890 159891 24faaed6 62ee 4175 8430 5d73b09911c8 csv 159907 5bad
  • 如何在后台启动 PhantomJS + Selenium 窗口?

    我在我的应用程序中使用 selenium phantomjs 但我想在后台启动我的应用程序 selenium 和 phantomjs 窗口 我该怎么做 I tried PhantomJSOptions options new Phantom
  • 从 S3 传输到 Google 存储 - 密钥不正确

    过去几个小时我一直在尝试设置从 S3 到我的谷歌存储桶的传输 创建传输时 我不断收到的错误是 访问密钥无效 请确保 S3 存储桶的访问密钥正确 或将存储桶权限设置为授予所有人 访问密钥和秘密都是正确的 因为它们当前在生产中用于 S3 完全访
  • 一组中的 d3 矩形干扰另一组中的矩形

    我有一个小组叫groove它有两个矩形 这些与数据无关 我也有一个群叫group其中有许多与数据绑定的矩形 在第二组中称为group只有三个数据点 但只显示两个 为什么第一个没有被渲染 我以前见过这个 但不记得如何解决 var margin
  • JavaScript 绑定问题

    这是让我感到困惑的代码片段 var timer start function var self this Why the code below doesn t write to this window setInterval self ti
  • 模板类的 CUDA 链接器错误

    在 ubuntu 上使用 CUDA 5 0 和 gcc g 4 6 在使用模板链接 CUDA 代码时遇到错误 cu array cu include cu array hpp template
  • C# - 如何将图像转换为 8 位彩色图像?

    我需要将从文件加载的 PNG 图像转换为另一个设备使用的每像素 8 位字节数组 嵌入式编程 我正在逐像素复制到使用 16 位颜色创建的新图像中 Format16bppRgb565 但是我需要Format8bpp NET Framework
  • Java 中的修剪字符

    如何在 Java 中修剪字符 e g String j joe jill Trim new char j应该 乔 吉尔 String j jack joe jill Trim jack j应该 乔 吉尔 etc 阿帕奇共享区有一个伟大的St
  • UISearchDisplayController隐藏导航栏

    我看到一个奇怪的情况 我在导航栏中放置了一个搜索栏 并将 UISearchDisplayController 与搜索栏链接起来 现在 当用户单击搜索栏时 搜索显示控制器倾向于隐藏导航栏 因此也隐藏搜索栏 为了解决这个问题 我对 UISear
  • CDI Eager Application 作用域 bean

    Since ManagedBean eager true 将被弃用 您如何创建eager 应用程序范围的 cdi bean 通过使用 CDI 扩展框架解决了这个问题 创建预选赛 Qualifier Target value ElementT
  • 将两个数据库表合二为一?

    在将关系数据库建模为库存管理系统时 我遇到了一些麻烦 目前 它只有 3 个简单的表 Product ID Name Price 收入 ID Date Quantity Product ID FK Sales ID Date Quantity
  • 如何将 Joda-Time DateTimeFormat.forStyle() 转换为 JSR 310 Java 时间?

    我正在将 Grails Joda Time 插件转换为JavaTime 我有旧的乔达时间代码 如下所示 def style switch type case LocalTime style S break case LocalDate st