i got the error java.text.ParseException: Unparseable date

95,582

Solution 1

You should change your code to:

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

You are using the wrong date format for parsing the date.

Solution 2

Use this it will work:

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

You cannot parse a date with a SimpleDateFormat that is set up with a different format

Solution 3

In this code you are telling Java to parse the date using the given format, and then print it. The format string you use must therefore match the format of the input date string. Since it doesn't, it's not surprising that it doesn't work.

To convert dates between two different formats you probably want to use two different DateFormat objects, one for the parsing and one for the printing.

Solution 4

Try the following code:

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

This one gives you the desired output!

Share:
95,582

Related videos on Youtube

Annu
Author by

Annu

Java Web Application Developer (Anuradha Jadav )

Updated on July 09, 2022

Comments

  • Annu
    Annu almost 2 years

    I want the date format as dd-MMM-yyyy. My code is:

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

    But, I got the error as:

        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)
    

    Thanks for any help....

    But, I want the date in date format as dd-MMM-yyyy.

  • Robin Green
    Robin Green about 13 years
    But see my answer - just making that change will not print the date as desired.
  • Robin Green
    Robin Green about 13 years
    what do you mean? I meant using one SimpleDateFormat for the parsing, and a different one for the printing.
  • Harry Joy
    Harry Joy about 13 years
    @Robin: If you use two formates then it might work. It will be more feasible if you show some demo code.
  • Nizzy
    Nizzy almost 10 years
    I don't think you can use DateFormat formatter = new SimpleDateFormat, it's an Incompatible Types error.
  • Luciano Fiandesio
    Luciano Fiandesio almost 10 years
    You are wrong. java.text.SimpleDateFormat it's a subclass of java.text.DateFormat the code works perfectly.
  • Annu
    Annu about 7 years
    @Nizzy We can use like that. Just try my code with simple java class.

Related