【问题标题】:i got the error java.text.ParseException: Unparseable date我收到错误 java.text.ParseException: Unparseable date
【发布时间】:2011-04-13 06:07:28
【问题描述】:

我希望日期格式为 dd-MMM-yyyy。 我的代码是:

String v_date_str="Sun Mar 06 11:28:16 IST 2011";
        DateFormat formatter;
        formatter = new SimpleDateFormat("dd-MMM-yyyy");
        Date date_temp=null;
        try {
            date_temp = (Date) formatter.parse(v_date_str);
        } catch (ParseException ex) {
            Logger.getLogger(Attendance_Calculation.class.getName()).log(Level.SEVERE, null, ex);
        }
        System.out.println("output: "+date_temp);

但是,我得到的错误是:

    The log message is null.
java.text.ParseException: Unparseable date: "Sun Mar 06 11:28:16 IST 2011"
        at java.text.DateFormat.parse(DateFormat.java:337)
        at org.fes.pis.jsf.main.Attendance_Calculation.btn_show_pending_appl_action(Attendance_Calculation.java:415)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at com.sun.el.parser.AstValue.invoke(AstValue.java:187)
        at com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:297)
        at com.sun.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:68)
        at javax.faces.event.MethodExpressionActionListener.processAction(MethodExpressionActionListener.java:99)
        at javax.faces.event.ActionEvent.processListener(ActionEvent.java:88)
        at javax.faces.component.UIComponentBase.broadcast(UIComponentBase.java:771)
        at javax.faces.component.UICommand.broadcast(UICommand.java:372)
        at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:475)
        at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:756)
        at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:82)
        at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:100)
        at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
        at com.sun.faces.extensions.avatar.lifecycle.PartialTraversalLifecycle.execute(PartialTraversalLifecycle.java:94)
        at javax.faces.webapp.FacesServlet.service(FacesServlet.java:265)
        at org.apache.catalina.core.ApplicationFilterChain.servletService(ApplicationFilterChain.java:427)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:315)
        at org.apache.catalina.core.StandardContextValve.invokeInternal(StandardContextValve.java:287)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:218)
        at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:648)
        at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:593)
        at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:94)
        at com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:98)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:222)
        at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:648)
        at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:593)
        at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:587)
        at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:1093)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:166)
        at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:648)
        at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:593)
        at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:587)
        at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:1093)
        at org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:291)
        at com.sun.enterprise.web.connector.grizzly.DefaultProcessorTask.invokeAdapter(DefaultProcessorTask.java:666)
        at com.sun.enterprise.web.connector.grizzly.DefaultProcessorTask.doProcess(DefaultProcessorTask.java:597)
        at com.sun.enterprise.web.connector.grizzly.DefaultProcessorTask.process(DefaultProcessorTask.java:872)
        at com.sun.enterprise.web.connector.grizzly.DefaultReadTask.executeProcessorTask(DefaultReadTask.java:341)
        at com.sun.enterprise.web.connector.grizzly.ssl.SSLReadTask.process(SSLReadTask.java:444)
        at com.sun.enterprise.web.connector.grizzly.ssl.SSLReadTask.doTask(SSLReadTask.java:230)
        at com.sun.enterprise.web.connector.grizzly.TaskBase.run(TaskBase.java:264)
        at com.sun.enterprise.web.connector.grizzly.ssl.SSLWorkerThread.run(SSLWorkerThread.java:106)

感谢您的帮助....

但是,我希望日期格式为 dd-MMM-yyyy。

【问题讨论】:

  • 我得到了解决方案,String v_date_str="Sun Mar 06 11:28:16 IST 2011"; DateFormat 格式化程序; formatter = new SimpleDateFormat("EEE MMM d HH:mm:ss zzz yyyy");日期 date_temp=null;字符串 d_temp=null; SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MMM-yyyy");尝试 { date_temp = (Date) formatter.parse(v_date_str); d_temp = dateFormat.format(date_temp); System.out.println("val : "+d_temp); } catch (ParseException ex) {// }

标签: java parsing datetime


【解决方案1】:

您应该将代码更改为:

String v_date_str="Sun Mar 06 11:28:16 IST 2011";
DateFormat formatter;
formatter = new SimpleDateFormat("EEE MMM d HH:mm:ss zzz yyyy");
Date date_temp=null;
try {
        date_temp = (Date) formatter.parse(v_date_str);
    } catch (ParseException ex) {
  Logger.getLogger(Attendance_Calculation.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println("output: "+date_temp);

您在解析日期时使用了错误的日期格式。

【讨论】:

  • 你错了。 java.text.SimpleDateFormat 它是 java.text.DateFormat 的子类,代码完美运行。
【解决方案2】:

使用它会起作用:

SimpleDateFormat formatter=new SimpleDateFormat("EEE MMM d HH:mm:ss zzz yyyy");

您无法解析设置为不同格式的SimpleDateFormat 的日期

【讨论】:

  • 但是请看我的回答 - 只是进行更改不会按需要打印日期。
  • 什么意思?我的意思是使用一种 SimpleDateFormat 进行解析,另一种用于打印。
  • @Robin:如果您使用两种甲酸盐,那么它可能会起作用。如果你显示一些演示代码会更可行。
【解决方案3】:

在这段代码中,您告诉 Java 使用给定格式解析日期,然后打印它。因此,您使用的格式字符串必须与输入日期字符串的格式相匹配。既然不行,那么它不起作用也就不足为奇了。

要在两种不同格式之间转换日期,您可能需要使用两个不同的 DateFormat 对象,一个用于解析,一个用于打印。

【讨论】:

    【解决方案4】:

    试试下面的代码:

    import java.text.DateFormat;
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import java.util.Locale;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    
    
    public class SimpleDateFormat02 {
    
    public static void main(String[] args) throws ParseException {
        String v_date_str="Sun Mar 06 11:28:16 IST 2011";
        Date v_date = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.ENGLISH).parse(v_date_str);
        DateFormat formatter = null;
    
        formatter = new SimpleDateFormat("dd-MMM-yyyy");
        Date date_temp=null;
        try {
            date_temp = (Date) formatter.parse("31-Dec-2012"); // String of same format a formatter
    
        } catch (ParseException ex) {
            //Logger.getLogger(Attendance_Calculation.class.getName()).log(Level.SEVERE, null, ex);
            ex.printStackTrace();
        }
        System.out.println("output: "+ formatter.format(v_date));
    }
    }
    

    这个给你想要的输出!

    【讨论】:

      【解决方案5】:

      这对我很有效

      SimpleDateFormat format1 = new SimpleDateFormat("dd-MM-yyyy");

      字符串格式化 = format1.format(objectValue);
      日期 javaDate = format1.parse(formatted);

      【讨论】:

        【解决方案6】:

        我得到了解决方案,

        String v_date_str = "Sun Mar 06 11:28:16 IST 2011";
            DateFormat formatter = new SimpleDateFormat("EEE MMM d HH:mm:ss zzz yyyy");
            String d_temp;
            Date dTemp;
            try {
                dTemp = formatter.parse(v_date_str);
                formatter=new SimpleDateFormat("dd-MMM-yyyy");
                d_temp=formatter.format(dTemp);
                System.out.println("val : " + d_temp);
            } catch (ParseException ex) {
            }
        

        【讨论】:

        • 我认为你不能使用 DateFormat formatter = new SimpleDateFormat,这是一个不兼容的类型错误。
        • @Nizzy 我们可以这样使用。只需使用简单的 java 类尝试我的代码。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-03-16
        • 1970-01-01
        • 2022-12-09
        • 1970-01-01
        • 1970-01-01
        • 2018-02-27
        相关资源
        最近更新 更多