<legend id='dXhjV'><style id='dXhjV'><dir id='dXhjV'><q id='dXhjV'></q></dir></style></legend>
  • <i id='dXhjV'><tr id='dXhjV'><dt id='dXhjV'><q id='dXhjV'><span id='dXhjV'><b id='dXhjV'><form id='dXhjV'><ins id='dXhjV'></ins><ul id='dXhjV'></ul><sub id='dXhjV'></sub></form><legend id='dXhjV'></legend><bdo id='dXhjV'><pre id='dXhjV'><center id='dXhjV'></center></pre></bdo></b><th id='dXhjV'></th></span></q></dt></tr></i><div id='dXhjV'><tfoot id='dXhjV'></tfoot><dl id='dXhjV'><fieldset id='dXhjV'></fieldset></dl></div>

    <small id='dXhjV'></small><noframes id='dXhjV'>

    <tfoot id='dXhjV'></tfoot>

        <bdo id='dXhjV'></bdo><ul id='dXhjV'></ul>
      1. java DateTimeFormatterBuilder 在测试时间失败

        时间:2023-05-28

        1. <legend id='96ZqE'><style id='96ZqE'><dir id='96ZqE'><q id='96ZqE'></q></dir></style></legend>
          <i id='96ZqE'><tr id='96ZqE'><dt id='96ZqE'><q id='96ZqE'><span id='96ZqE'><b id='96ZqE'><form id='96ZqE'><ins id='96ZqE'></ins><ul id='96ZqE'></ul><sub id='96ZqE'></sub></form><legend id='96ZqE'></legend><bdo id='96ZqE'><pre id='96ZqE'><center id='96ZqE'></center></pre></bdo></b><th id='96ZqE'></th></span></q></dt></tr></i><div id='96ZqE'><tfoot id='96ZqE'></tfoot><dl id='96ZqE'><fieldset id='96ZqE'></fieldset></dl></div>
            <bdo id='96ZqE'></bdo><ul id='96ZqE'></ul>
            • <small id='96ZqE'></small><noframes id='96ZqE'>

                <tbody id='96ZqE'></tbody>

                  <tfoot id='96ZqE'></tfoot>
                  本文介绍了java DateTimeFormatterBuilder 在测试时间失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我对 DateTimeFormatterBuilder 有一个简单的 jUnit 测试.在运行时,当 Spring-MVC 处理程序 (@RequestParam)

                  I have a simple jUnit test for DateTimeFormatterBuilder. At runtime it works, when some String comes on Spring-MVC hanlder (@RequestParam)

                  在测试时它以相同的 String 值失败.

                  At testtime it fails with the same String value.

                  测试值:25-May-2018 11:10

                  待测方法:

                  public void getTimeDifference(@RequestParam String startDate, @RequestParam String endDate) {
                      DateTimeFormatter DATE_TIME_FORMAT = new DateTimeFormatterBuilder().parseCaseInsensitive().appendPattern("dd-MMM-yyyy HH:mm").toFormatter();
                      LocalDateTime.parse(startDate,DATE_TIME_FORMAT);
                      return   messages;
                  }
                  

                  测试方法:

                  @Test
                  public void testFormat() throws Exception {
                      final String startDateFormatA = "25-May-2018 11:10";
                      final String endDateFormatA = "25-May-2018 11:10";
                      assertEquals("06:00", callDbController.getTimeDifference(startDateFormatA, endDateFormatA)[1]);
                  }
                  

                  我的测试:在运行时我设置一个断点并在 Display-View 上测试它:

                  My Test: At runtime I set a break-point and test it on Display-View:

                  LocalDateTime.parse("25-May-2018 11:10",DATE_TIME_FORMAT)
                  

                  在测试时使用相同的 spring-aplication-context 我在运行时做同样的事情,但它失败了.

                  At testtime with the same spring-aplication-context I do the same like on runtime and it fails.

                  有人有想法吗?

                  推荐答案

                  月份名称是英文的,所以你最好在formatter中设置一个java.util.Locale.

                  The month name is in English, so you'd better set a java.util.Locale in the formatter.

                  如果不设置,格式化程序将使用 JVM 默认语言环境.而且如果不是英文,可能会报错(而且不同的环境可能有不同的配置,所以最好设置locale而不是依赖JVM的默认值).

                  If you don't set it, the formatter will use the JVM default locale. And if it's not English, you might get an error (and different environments might have different configurations, so it's better to set the locale instead of relying on the JVM's default).

                  只需执行 toFormatter(Locale.ENGLISH) 而不是 toFormatter() 即可.

                  Just do toFormatter(Locale.ENGLISH) instead of just toFormatter() and that's it.

                  这篇关于java DateTimeFormatterBuilder 在测试时间失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:SimpleDateFormat 上的 java HH:mm 和 hh:mm 之间的区别 下一篇:将文件大小格式化为 MB、GB 等

                  相关文章

                1. <i id='8PEmm'><tr id='8PEmm'><dt id='8PEmm'><q id='8PEmm'><span id='8PEmm'><b id='8PEmm'><form id='8PEmm'><ins id='8PEmm'></ins><ul id='8PEmm'></ul><sub id='8PEmm'></sub></form><legend id='8PEmm'></legend><bdo id='8PEmm'><pre id='8PEmm'><center id='8PEmm'></center></pre></bdo></b><th id='8PEmm'></th></span></q></dt></tr></i><div id='8PEmm'><tfoot id='8PEmm'></tfoot><dl id='8PEmm'><fieldset id='8PEmm'></fieldset></dl></div>

                  <small id='8PEmm'></small><noframes id='8PEmm'>

                    <bdo id='8PEmm'></bdo><ul id='8PEmm'></ul>
                    1. <tfoot id='8PEmm'></tfoot>

                      <legend id='8PEmm'><style id='8PEmm'><dir id='8PEmm'><q id='8PEmm'></q></dir></style></legend>