0

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

CyprUS
  • 4,159
  • 9
  • 48
  • 93
  • 1
    What do you mean by "wild values"? Could it be that you just need to tell Excel your date format? – Michael McGowan Mar 02 '11 at 07:33
  • I mean like the date is coming 7/3/39. – CyprUS Mar 02 '11 at 07:42
  • and i am getting consecutive dates as '7/3/30','7/2/28' and '7/2/35' even etc. doesnt make any sense to me. Maybe it is not taking the date only, it is adding the time too. – CyprUS Mar 02 '11 at 07:46
  • When i write System.out.print(datenow), i get values like: Sun Jul 03 00:02:00 IST 2033. Why am i getting such year and time values? – CyprUS Mar 02 '11 at 07:53

1 Answers1

0

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)); 
Margus
  • 19,694
  • 14
  • 55
  • 103