Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/359.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 2012年8月31日星期五00:00:00的日期格式例外_Java_Simpledateformat - Fatal编程技术网

Java 2012年8月31日星期五00:00:00的日期格式例外

Java 2012年8月31日星期五00:00:00的日期格式例外,java,simpledateformat,Java,Simpledateformat,我试图使用格式设置日期2012年8月31日星期五00:00:00猫但得到了不可解析的日期:“2012年8月31日星期五00:00:00猫” 我正在使用这个代码 String DATE_FORMAT = "EEE MMM dd yyyy hh:mm:ss zzzz yyyy"; SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT); Date date = sdf.parse(myObj.getDate().toString());

我试图使用格式设置日期
2012年8月31日星期五00:00:00猫
但得到了不可解析的日期:“2012年8月31日星期五00:00:00猫”

我正在使用这个代码

String DATE_FORMAT = "EEE MMM dd yyyy hh:mm:ss zzzz yyyy"; 
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT); 
Date date = sdf.parse(myObj.getDate().toString());

我这里遗漏了什么吗?

您的格式中有一个额外的
yyy
。 试试这个:

SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd hh:mm:ss zzzz yyyy");
Date date = sdf.parse("Fri Aug 31 00:00:00 CAT 2012");

您的格式中有一个额外的
yyyy
。 试试这个:

SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd hh:mm:ss zzzz yyyy");
Date date = sdf.parse("Fri Aug 31 00:00:00 CAT 2012");

你有两次年份标记:

String DATE_FORMAT = "EEE MMM dd yyyy hh:mm:ss zzzz yyyy"; 
                                 ^ remove this one.

你有两次年份标记:

String DATE_FORMAT = "EEE MMM dd yyyy hh:mm:ss zzzz yyyy"; 
                                 ^ remove this one.

奇怪的是,您已经有了一个
Date
-对象,希望通过
toString()
使用其标准输出对其进行格式化,然后再次将其解析为
Date
-对象。此过程甚至会释放原始
Date
-对象的毫秒分数(通过
myObj.getDate()
)。无论如何,进行解析的正确格式模式是:

EEE MMM dd HH:mm:ss zzz yyyy

不要忘记将
SimpleDateFormat
-对象的语言环境设置为英语。请注意,您有两次yyyy部分,并且使用了“h”(上午/下午的小时数)而不是“h”(一天的小时数)。另请参见类
java.util.Date
的源代码:

/**
 * Converts this <code>Date</code> object to a <code>String</code>
 * of the form:
 * <blockquote><pre>
 * dow mon dd hh:mm:ss zzz yyyy</pre></blockquote>
 * where:<ul>
 * <li><tt>dow</tt> is the day of the week (<tt>Sun, Mon, Tue, Wed,
 *     Thu, Fri, Sat</tt>).
 * <li><tt>mon</tt> is the month (<tt>Jan, Feb, Mar, Apr, May, Jun,
 *     Jul, Aug, Sep, Oct, Nov, Dec</tt>).
 * <li><tt>dd</tt> is the day of the month (<tt>01</tt> through
 *     <tt>31</tt>), as two decimal digits.
 * <li><tt>hh</tt> is the hour of the day (<tt>00</tt> through
 *     <tt>23</tt>), as two decimal digits.
 * <li><tt>mm</tt> is the minute within the hour (<tt>00</tt> through
 *     <tt>59</tt>), as two decimal digits.
 * <li><tt>ss</tt> is the second within the minute (<tt>00</tt> through
 *     <tt>61</tt>, as two decimal digits.
 * <li><tt>zzz</tt> is the time zone (and may reflect daylight saving
 *     time). Standard time zone abbreviations include those
 *     recognized by the method <tt>parse</tt>. If time zone
 *     information is not available, then <tt>zzz</tt> is empty -
 *     that is, it consists of no characters at all.
 * <li><tt>yyyy</tt> is the year, as four decimal digits.
 * </ul>
 *
 * @return  a string representation of this date.
 * @see     java.util.Date#toLocaleString()
 * @see     java.util.Date#toGMTString()
 */
public String toString() {
    // "EEE MMM dd HH:mm:ss zzz yyyy";
*其中:
    *道琼斯指数是一周中的一天(太阳、周一、周二、周三、, *星期四、五、六)。 *
  • 周一是月份(一月、二月、三月、四月、五月、六月、, *七月、八月、九月、十月、十一月、十二月)。 *
  • dd是一个月中的一天(01到 *31),作为两个十进制数字。 *
  • hh是一天中的小时数(00到 *23),作为两个十进制数字。 *
  • mm是小时内的分钟(00到 *59),作为两个十进制数字。 *
  • ss是一分钟内的第二个(00到 *61,作为两个十进制数字。 *
  • zzz是时区(可能反映夏令时) *标准时区缩写包括 *由方法parse.If时区识别 *信息不可用,则zzz为空- *也就是说,它根本不包含任何字符。 *
  • yyy是年份,用四位小数表示。 *
* *@返回此日期的字符串表示形式。 *@see java.util.Date#tolocalString() *@see java.util.Date#togmString() */ 公共字符串toString(){ //“EEE MMM dd HH:mm:ss zzz yyyy”;
奇怪的是,您已经有了一个
日期
-对象,想通过
toString()
使用其标准输出对其进行格式化,然后再次将其解析为
日期
-对象。此过程甚至会释放原始
日期
-对象的毫秒分数(通过
myObj.getDate()
)。无论如何,要进行分析的正确格式模式是:

EEE MMM dd HH:mm:ss zzz yyyy

不要忘记将
SimpleDataFormat
-对象的区域设置为英语。请注意,yyyy部分有两次,并且使用了“h”(上午/下午的小时数)而不是“h”(一天的小时数)。另请参见类
java.util.Date
的源代码:

/**
 * Converts this <code>Date</code> object to a <code>String</code>
 * of the form:
 * <blockquote><pre>
 * dow mon dd hh:mm:ss zzz yyyy</pre></blockquote>
 * where:<ul>
 * <li><tt>dow</tt> is the day of the week (<tt>Sun, Mon, Tue, Wed,
 *     Thu, Fri, Sat</tt>).
 * <li><tt>mon</tt> is the month (<tt>Jan, Feb, Mar, Apr, May, Jun,
 *     Jul, Aug, Sep, Oct, Nov, Dec</tt>).
 * <li><tt>dd</tt> is the day of the month (<tt>01</tt> through
 *     <tt>31</tt>), as two decimal digits.
 * <li><tt>hh</tt> is the hour of the day (<tt>00</tt> through
 *     <tt>23</tt>), as two decimal digits.
 * <li><tt>mm</tt> is the minute within the hour (<tt>00</tt> through
 *     <tt>59</tt>), as two decimal digits.
 * <li><tt>ss</tt> is the second within the minute (<tt>00</tt> through
 *     <tt>61</tt>, as two decimal digits.
 * <li><tt>zzz</tt> is the time zone (and may reflect daylight saving
 *     time). Standard time zone abbreviations include those
 *     recognized by the method <tt>parse</tt>. If time zone
 *     information is not available, then <tt>zzz</tt> is empty -
 *     that is, it consists of no characters at all.
 * <li><tt>yyyy</tt> is the year, as four decimal digits.
 * </ul>
 *
 * @return  a string representation of this date.
 * @see     java.util.Date#toLocaleString()
 * @see     java.util.Date#toGMTString()
 */
public String toString() {
    // "EEE MMM dd HH:mm:ss zzz yyyy";
*其中:
    *道琼斯指数是一周中的一天(太阳、周一、周二、周三、, *星期四、五、六)。 *
  • 周一是月份(一月、二月、三月、四月、五月、六月、, *七月、八月、九月、十月、十一月、十二月)。 *
  • dd是一个月中的一天(01到 *31),作为两个十进制数字。 *
  • hh是一天中的小时数(00到 *23),作为两个十进制数字。 *
  • mm是小时内的分钟(00到 *59),作为两个十进制数字。 *
  • ss是一分钟内的第二个(00到 *61,作为两个十进制数字。 *
  • zzz是时区(可能反映夏令时) *标准时区缩写包括 *由方法parse.If时区识别 *信息不可用,则zzz为空- *也就是说,它根本不包含任何字符。 *
  • yyy是年份,用四位小数表示。 *
* *@返回此日期的字符串表示形式。 *@see java.util.Date#tolocalString() *@see java.util.Date#togmString() */ 公共字符串toString(){ //“EEE MMM dd HH:mm:ss zzz yyyy”;
如果您试图格式化日期,则不会出现
不可解析日期的异常情况。请提供一个。@AndyTurner请检查我的编辑如果您试图格式化日期,则不会出现
不可解析日期的异常情况。请提供一个。@AndyTurner请检查我的编辑