Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/382.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 无法将字符串转换为人类可读的格式_Java_Date_Simpledateformat_Date Format_Parseexception - Fatal编程技术网

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

Java 无法将字符串转换为人类可读的格式,java,date,simpledateformat,date-format,parseexception,Java,Date,Simpledateformat,Date Format,Parseexception,我的代码有什么问题?我无法以我想要的格式返回字符串 方法调用: 格式化程序: 例外情况: 它对我有用 它适合我您需要两种格式。一个用于解析输入格式,另一个用于格式化输出 public static String formatResolutionTime(String startDateString) { DateFormat dfParse = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZZZZ"); //Format for parsing

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

方法调用:

格式化程序:

例外情况:

它对我有用


它适合我

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

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;

}

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

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;

}

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

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

您将获得异常不可解析的日期:2017-05-26T00: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,因为您试图解析格式为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;

    }

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

您的输入是2017-05-26T00:00:00+02:00。让我们将其分解为:

2017年:格式为yyyy的年份 05:格式为MM的月份 26:格式为dd的日期 T:日期和时间之间的分隔符,格式为字符“T” 00:格式为HH而非HH的小时 00:mm格式的分钟数 00:ss格式中的秒数 +02:00:XXX格式的时区 因此,您的SimpleDataFormat解析器模式将

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

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

您的输入是2017-05-26T00:00:00+02:00。让我们将其分解为:

2017年:格式为yyyy的年份 05:格式为MM的月份 26:格式为dd的日期 T:日期和时间之间的分隔符,格式为字符“T” 00:格式为HH而非HH的小时 00:mm格式的分钟数 00:ss格式中的秒数 +02:00:XXX格式的时区 因此,您的SimpleDataFormat解析器模式将

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年,是时候跳过旧类SimpleDateFormat和Date,并使用DateTimeFormatter和OffsetDateTime了。这更合适,因为原始日期时间字符串符合ISO 8601,新类“理解”为默认格式。因此,我们不需要像SimpleDataFormat那样给出解析的显式格式。

如果没有现代答案,这一系列答案是不完整的

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

05/26/2017

到目前为止,我读到的答案都是正确的,而且已经过时了。2017年,是时候跳过旧类SimpleDateFormat和Date,并使用DateTimeFormatter和OffsetDateTime了。这更合适,因为原始日期时间字符串符合ISO 8601,新类“理解”为默认格式。因此,我们不需要像SimpleDataFormat那样提供解析的显式格式。

您必须定义两个DateFormatter,一个用于输入解析,一个用于输出格式化您必须定义两个DateFormatter,一个用于输入解析,一个用于输出格式化您可以在解决方案的顶部添加解释吗?我会给oyu一个+1,因为你比我快了一点:可能是因为你给出了一个函数代码,但没有解释他的问题是什么,或者为什么他会有问题。如果你看一下例外情况中的日期格式,即2017-05-26T00:00:00+02:00,如果你从格式化程序中删除ZZZZ,上面的代码会起作用。因此,该行看起来像DateFormat dfParse=new SimpleDateFormatyyy MM dd'HH:MM:ss;你能在解答的上面加上解释吗?我会给oyu一个+1,因为你比我快了一点:很可能是因为你给了一个func
tiionnal代码,但没有解释他的问题是什么,也没有解释他为什么会有问题。如果您查看例外情况中的日期格式,即2017-05-26T00:00:00+02:00,如果您从格式化程序中删除ZZZZ,则上述代码有效。因此,该行看起来像DateFormat dfParse=new SimpleDateFormatyyy MM dd'HH:MM:ss@幽灵猫你能告诉我这个问题吗?我把评论放在哪里了?我真的不记得我说了什么。如果我没记错的话,你结束了一个问题,其中有一个NPE是由打字错误引起的,这是合乎逻辑的。我不为此责备你,因为这个问题毫无意义。说你喜欢用“我如何修复NPE”的链接来结束NPE问题并不是一种指责;所以我不能再给你指出了;-你说/问我是否讨厌NPE问题,因为我一直在关闭它们或类似的东西。顺便说一句:这不是最好的答案,但表示我感谢你关注了解这个社区的工作方式;-。。。至少我是这么看的。@GhostCat你能告诉我这个问题我把评论放在哪里了吗?我真的不记得我说了什么。如果我没记错的话,你结束了一个问题,其中有一个NPE是由打字错误引起的,这是合乎逻辑的。我不为此责备你,因为这个问题毫无意义。说你喜欢用“我如何修复NPE”的链接来结束NPE问题并不是一种指责;所以我不能再给你指出了;-你说/问我是否讨厌NPE问题,因为我一直在关闭它们或类似的东西。顺便说一句:这不是最好的答案,但表示我感谢你关注了解这个社区的工作方式;-。。。至少我是这么看的。