How to convert a string to Date and then add it to excel using jexcel api

2.4k views Asked by At

I am currently reading a CSV file. I have to extract the dates from it and add them to excel file in dd-MM-yy format. I am faced with two problems.

  1. I am using String.split(delim) where, delim="[,]". I am getting the date in yyyy-MM-dd format.
  2. I am not able to convert the string into a date format and then add them to excel file. I tried using DateFormat but error says Dateformat is abstract.

Here is the code

dtformat=new SimpleDateFormat("dd-MM-yy");
datenow=dtformat.parse(stringDate);//datenow is what i want to add to add.
datecell=new DateTime(tokenNumber, lineNumber, datenow);
sheet.addCell(datecell);

When I open the excel file, I get wild values.

Please help.Thanks in advance

1

There are 1 answers

2
Margus On BEST ANSWER

Google is your friend.

To parse date:

DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Date date = (Date)formatter.parse("2011 02 07");

To write date:

DateFormat customDateFormat = new DateFormat ("dd-MM-yy"); 
WritableCellFormat dateFormat = new WritableCellFormat (customDateFormat); 
sheet.addCell(new DateTime(/* date */, dateFormat));