Search code examples
javadatesimpledateformatdate-formatparseexception

Unable to convert string to human-readable format


What is wrong with my code here? I'm unable return a String in the format I'd like...

Method Call:

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

Formatter:

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;    
    }        
}

Exception:

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)

Solution

  • You need two formats. One for parsing the input format and one for formating the Output.

    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;
    
    }