无法将字符串转换为人类可读的格式

问题描述:

我的代码在这里出了什么问题?我无法返回,我想格式的字符串...无法将字符串转换为人类可读的格式

方法调用:

incident.setTargetDate(DateUtil.formatResolutionTime(targetDateList.get(i))); 

格式化:

public class DateUtil { 

    public static String formatResolutionTime(String startDateString) { 
     DateFormat df = new SimpleDateFormat("MM/dd/yyyy"); 
     Date startDate = null; 

     try { 
      startDate = df.parse(startDateString); 
     } catch (ParseException e) { 
      e.printStackTrace(); 
     } 
     String newDateString = df.format(startDate); 
     System.out.println(newDateString); 
     return newDateString;  
    }   
} 

例外:

java.text.ParseException: Unparseable date: "2017-05-26T00:00:00+02:00" 
    at java.text.DateFormat.parse(DateFormat.java:366) 
    at app.util.DateUtil.formatResolutionTime(DateUtil.java:19) 
    at app.controller.TableViewController.retrieveTicketsForTable(TableViewController.java:194) 
    at app.controller.TableViewController.initialize(TableViewController.java:139) 
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548) 
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441) 
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214) 
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175) 
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148) 
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124) 
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104) 
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097) 
    at app.Main.start(Main.java:14) 
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863) 
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326) 
    at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294) 
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95) 
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method) 
    at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191) 
    at java.lang.Thread.run(Thread.java:745) 
+1

您必须定义两个DateFormatter,一个用于输入(解析),另一个用于输出(格式) – Jens

您需要两种格式。一个用于解析输入格式,另一个用于格式化输出。

public static String formatResolutionTime(String startDateString) { 
    DateFormat dfParse = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZZZZ"); //Format for parsing the Input string 
    DateFormat df = new SimpleDateFormat("MM/dd/yyyy"); //Format for formatting the output 
    Date startDate = null; 

    try { 
     startDate = dfParse.parse(startDateString); 
    } catch (ParseException e) { 
     e.printStackTrace(); 
    } 
    String newDateString = df.format(startDate); 
    System.out.println(newDateString); 
    return newDateString; 

} 
+0

您可以在解决方案的顶部添加解释吗?我会给你一个+1,因为你把我打败了:p – Nathan

+0

可能是因为你给了一个函数代码,但没有解释他的问题是什么,或者他为什么这样做。 – Nathan

+0

如果您查看例外的日期格式,即“2017-05-26T00:00:00 + 02:00”,则如果您从格式化程序中删除“ZZZZ”,则上面的代码将起作用。因此,该行看起来像''DateFormat dfParse = new SimpleDateFormat(“yyyy-MM-dd'T'HH:mm:ss”);'' – santafebound

DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); 
    Date date = new Date(); 
    //System.out.println(dateFormat.format(date)); 
    return dateFormat.format(date); 

它适用于我)

你检查,格式为MM/DD/YYYY的日期,但提供格式YYYY-MM-DD的日期。

+0

@GhostCat你能指点我的问题吗?我不记得我说的是什么。如果我没有记错的话,你可以通过打字错误来解决问题 - 这是合乎逻辑的。我不怪你,因为这个问题没有目的。说你喜欢关闭NPE问题与“我如何解决NPE”的链接不是一个怪。 – Nathan

+0

OP删除了他的问题;所以我不能再指点你了;-)你说/问我是否讨厌NPE问题,因为我一直在“关闭”或类似的东西。 – GhostCat

+0

顺便说一句:不是最大的答案,但表示我对你的重点是理解这个社区是如何工作的赞赏;-) ...至少这是我的看法。 – GhostCat

你所得到的例外Unparseable date: "2017-05-26T00:00:00+02:00",因为你试图解析格式"yyyy-MM-dd'T'hh:mm:ssXXX"的字符串,而是告诉格式化你的字符串类型的MM/dd/yyyy. 你输入字符串格式"2017-05-26T00:00:00+02:00"的,你想在输出格式MM/dd/yyyy,如其他帖子中所建议的,您需要两个格式化程序。

public static String formatResolutionTime(String startDateString) { 

     DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ssXXX"); 
     Date startDate = null; 

     try { 
      startDate = df.parse(startDateString); 
     } catch (ParseException e) { 
      e.printStackTrace(); 
     } 
     DateFormat newFormatter = new SimpleDateFormat("MM/dd/yyyy"); 
     String newDateString = newFormatter.format(startDate); 
     System.out.println(newDateString); 
     return newDateString; 

    } 

您必须使用两个日期格式对象:一个用于解析(输入 - >日期)和一个用于格式化(日期 - >输出)。您的输入是2017-05-26T00:00:00+02:00。让我们来打破它(指SimpleDateFormat):

  • 2017:当年格式yyyy
  • 05:每月格式MM
  • 26:白天在格式dd
  • T:日期和时间之间的分隔符,格式为字符'T'
  • 00:小时在格式HHhh
  • 00:在格式分钟mm
  • 00:秒的格式ss
  • +02:00:在格式XXX

的时区因此,您的SimpleDateFormat解析器模式将为

yyyy-MM-dd'T'HH:mm:ss:XXX 

其他注意事项

在ParseException的的情况下,你的代码可能会导致意外的行为,其实在try/catch语句后阻止你startDate将是无效和格式化会尝试格式化空日期并返回结果,生成一个NullPointerException

说,我的代码是:

private static final String PARSE_PATTERN = "yyyy-MM-dd'T'HH:mm:ssXXX"; 
private static final String FORMAT_PATTERN = "MM/dd/yyyy"; 
// we cannot declare the SimpleDateFormat as static since it isn't thread-safe 

public static String formatResolutionTime(String startDateString) { 

    try { 
     DateFormat parser = new SimpleDateFormat(PARSE_PATTERN); 
     Date startDate = parser.parse(startDateString); 

     DateFormat formatter = new SimpleDateFormat(FORMAT_PATTERN); 
     return formatter.format(startDate); 
    } catch (ParseException e) { 
     // e.printStackTrace(); 
     return "Error"; // or whatever, but return a string here 
    } 
} 

回答这个线程不会没有现代化的答案是完整的。

DateTimeFormatter dtf = DateTimeFormatter.ofPattern("MM/dd/yyyy"); 
    String newDateString = OffsetDateTime.parse(startDateString).format(dtf); 

结果是

05/26/2017 

我读过一直到现在的答案是正确的 - 和过时的。 2017年是时候跳过旧类SimpleDateFormatDate,并使用the newer Java date and time API,在这种情况下为DateTimeFormatterOffsetDateTime。这更加合适,因为您的原始日期时间字符串符合ISO 8601,即新版类别“理解”为其默认格式。所以我们不需要给出明确的解析格式(如需要SimpleDateFormat)。