繁体   English   中英

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

[英]Unable to convert string to human-readable format

我的代码在这里有什么问题? 我无法以我想要的格式返回字符串...

方法调用:

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)

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

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;

}

您正在获取异常Unparseable date: "2017-05-26T00:00:00+02:00" ,因为您正试图解析格式为"yyyy-MM-dd'T'hh:mm:ssXXX" ,但告诉格式化程序您的字符串为MM/dd/yyyy.类型MM/dd/yyyy. 您输入的String的格式为"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;

    }

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

您必须使用两个DateFormat对象:一个用于解析(输入->日期),另一个用于格式化(日期->输出)。

您的输入是2017-05-26T00:00:00+02:00 让我们分解一下(请参阅SimpleDateFormat ):

  • 2017 :格式为yyyy
  • 05MM格式的月份
  • 26 :日期格式为dd
  • T :日期和时间之间的分隔符,格式为'T'
  • 00 :小时,格式为HH不是 hh
  • 00 :以mm格式的分钟
  • 00ss格式的秒
  • +02:00 :时区,格式为XXX

因此,您的SimpleDateFormat解析器模式将是

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

补充笔记

如果发生ParseException,则您的代码可能会导致意外行为,实际上,在try / catch块之后,您的startDate将为null,格式化程序将尝试格式化null日期并返回结果,从而生成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 ,并使用较新的Java日期和时间API了 ,在这种情况下为DateTimeFormatterOffsetDateTime 由于原始日期时间字符串符合ISO 8601(较新的类将其默认理解为该格式)的格式,因此这更加合适。 因此,我们不需要给出明确的解析格式(如SimpleDateFormat )。

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

这个对我有用)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM