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

        Java:不可解析的日期异常

        Java: unparseable date exception(Java:不可解析的日期异常)

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

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

                  本文介绍了Java:不可解析的日期异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  在尝试转换日期格式时出现异常:无法解析的日期并且不知道如何解决此问题.

                  While trying to transform the date format I get an exception:unparseable date and don't know how to fix this problem.

                  我收到一个代表事件日期的字符串,并希望在 GUI 中以不同的格式显示该日期.

                  I am receiving a string which represents an event date and would like to display this date in different format in GUI.

                  我想要做的是:

                  private String modifyDateLayout(String inputDate){
                          try {
                              //inputDate = "2010-01-04 01:32:27 UTC";
                              Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z").parse(inputDate);
                              return new SimpleDateFormat("dd.MM.yyyy HH:mm:ss").format(date);
                          } catch (ParseException e) {
                              e.printStackTrace();
                              return "15.01.2010";
                          }
                      }
                  

                  不管怎样

                  String modifiedDateString = originalDate.toString();
                  

                  是假的.我想获取以下格式的日期字符串:

                  is dummy. I would like to get a date string in the following format:

                  dd.MM.yyyy HH:mm:ss

                  dd.MM.yyyy HH:mm:ss

                  输入字符串示例如下:

                  2010-01-04 01:32:27 UTC

                  2010-01-04 01:32:27 UTC

                  有谁知道如何将上面的示例日期(字符串)转换为字符串格式dd.MM.yyyy HH:mm:ss?

                  Does anyone know how to convert the example date (String) above into a String format dd.MM.yyyy HH:mm:ss?

                  谢谢!

                  我修复了错误的输入日期格式,但仍然无法正常工作.上面是粘贴的方法,下面是调试会话的屏幕图像.

                  I fixed the wrong input date format but still it doesn't work. Above is the pasted method and below is the screen image from debugging session.

                  替代文字 http://img683.imageshack.us/img683/193/dateproblem.png

                  #更新我跑了

                  String[] timezones = TimeZone.getAvailableIDs();
                  

                  并且数组中有UTC字符串.这是一个奇怪的问题.

                  and there is UTC String in the array. It's a strange problem.

                  我做了一个有效的肮脏黑客:

                  I did a dirty hack that works:

                  private String modifyDateLayout(String inputDate){
                      try {
                          inputDate = inputDate.replace(" UTC", "");
                          Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(inputDate);
                          return new SimpleDateFormat("dd.MM.yyyy HH:mm:ss").format(date);
                      } catch (ParseException e) {
                          e.printStackTrace();
                          return "15.01.2010";
                      }
                  }
                  

                  但我仍然希望在不削减时区的情况下转换原始输入.

                  But still I would prefer to transform the original input without cutting timezone away.

                  此代码是为使用 JDK 1.6 的 Android 手机编写的.

                  This code is written for Android phone using JDK 1.6.

                  推荐答案

                  你在这里基本上做的是依赖 Date#toString() 已经有一个固定的模式.要将 Java Date 对象转换为另一种人类可读的字符串模式,您需要 SimpleDateFormat#format().

                  What you're basically doing here is relying on Date#toString() which already has a fixed pattern. To convert a Java Date object into another human readable String pattern, you need SimpleDateFormat#format().

                  private String modifyDateLayout(String inputDate) throws ParseException{
                      Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z").parse(inputDate);
                      return new SimpleDateFormat("dd.MM.yyyy HH:mm:ss").format(date);
                  }
                  

                  顺便说一下,这里只能SimpleDateFormat#parse().这意味着 inputDate 不在预期的模式 "yyyy-MM-dd HH:mm:ss z" 中.您可能需要修改模式以匹配 inputDate 的实际模式.

                  By the way, the "unparseable date" exception can here only be thrown by SimpleDateFormat#parse(). This means that the inputDate isn't in the expected pattern "yyyy-MM-dd HH:mm:ss z". You'll probably need to modify the pattern to match the inputDate's actual pattern.

                  更新:好的,我做了一个测试:

                  Update: Okay, I did a test:

                  public static void main(String[] args) throws Exception {
                      String inputDate = "2010-01-04 01:32:27 UTC";
                      String newDate = new Test().modifyDateLayout(inputDate);
                      System.out.println(newDate);
                  }
                  

                  这正确打印:

                  03.01.2010 21:32:27
                  

                  (我在 GMT-4)

                  更新 2: 根据您的编辑,您确实得到了一个 ParseException.最可疑的部分将是 UTC 的时区.这在您的 Java 环境中实际上是已知吗?您使用的是什么 Java 版本和什么操作系统版本?检查 TimeZone.getAvailableIDs().中间必须有一个UTC.

                  Update 2: as per your edit, you really got a ParseException on that. The most suspicious part would then be the timezone of UTC. Is this actually known at your Java environment? What Java version and what OS version are you using? Check TimeZone.getAvailableIDs(). There must be a UTC in between.

                  这篇关于Java:不可解析的日期异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  How can I detect integer overflow on 32 bits int?(如何检测 32 位 int 上的整数溢出?)
                  Local variables before return statements, does it matter?(return 语句之前的局部变量,这有关系吗?)
                  How to convert Integer to int?(如何将整数转换为整数?)
                  How do I create an int array with randomly shuffled numbers in a given range(如何在给定范围内创建一个随机打乱数字的 int 数组)
                  Inconsistent behavior on java#39;s ==(java的行为不一致==)
                  Why is Java able to store 0xff000000 as an int?(为什么 Java 能够将 0xff000000 存储为 int?)
                    <tbody id='xaW2O'></tbody>

                1. <tfoot id='xaW2O'></tfoot>

                      1. <legend id='xaW2O'><style id='xaW2O'><dir id='xaW2O'><q id='xaW2O'></q></dir></style></legend>

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

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