1

I'm not able to parse the date stored in an Excel file. My code snippets is as follows:

SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy");
Date parsed = format.parse(row.getCell(1).toString());
Date jdate = format.parse(row.getCell(4).toString());

My error is:

Cannot parse the date 08-Jun-2012.

In Excel file the date is stored as 08-06-2012. But it takes it as 08-Jun-2013.

joragupra
  • 692
  • 1
  • 12
  • 23

3 Answers3

0

check the date format in your excel file. It might be DD-Mmm-YYYY. by changing it to DD-MM-YYYY you could parse i guess.

0

Make Strings Explicit

When debugging, tear apart complex statements to verify the pieces. In this case, move those row getter calls to separate lines, assigning the values to String variables.

Be Careful With Formatters

You need to carefully study the doc for formatters, and then double-check the pattern characters.

In your case:

  • The format "dd-MM-yyyy" is for month as digits
  • The format "dd-MMM-yyyy" is for month as text (name of month)

Example Code

String inputDigits = "08-06-2012";
String inputChars = "08-Jun-2013";

java.text.SimpleDateFormat formatDigits = new java.text.SimpleDateFormat( "dd-MM-yyyy" );
java.text.SimpleDateFormat formatChars = new java.text.SimpleDateFormat( "dd-MMM-yyyy" );

java.util.Date dateDigits = null;
java.util.Date dateChars = null;
try {
    dateDigits = formatDigits.parse( inputDigits );
    dateChars = formatChars.parse( inputChars );
} catch ( ParseException ex ) {
    Logger.getLogger( App.class.getName() ).log( Level.SEVERE, null, ex );
}

Dump to console…

System.out.println( "dateDigits: " + dateDigits );
System.out.println( "dateChars: " + dateChars );

When run…

dateDigits: Fri Jun 08 00:00:00 PDT 2012
dateChars: Sat Jun 08 00:00:00 PDT 2013
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
0

You can try this :-

public static void main(String[] args)throws Exception {
        // TODO Auto-generated method stub
        String dat="08-Jun-2013";
        SimpleDateFormat format=new SimpleDateFormat("dd-MMM-yyyy");
        java.util.Date dt=format.parse(dat);        
        System.out.println("Parsed Date :: "+dt);
    }

Its showing - Sat Jun 08 00:00:00 IST 2013

Use dd-MMM-yyyy instead of dd-MM-yyyy.

Hope it will help you.

JDGuide
  • 6,239
  • 12
  • 46
  • 64