How to keep date formatting during CSV export

Is there a way to keep date formatting when exporting to CSV? I have a GWT application. My date data, e.g. 03-01-2012 and 02-2012. After exporting to CSV successfully, when I open the CSV file, these dates now look like 3/1/2012 and 2/1/2012 respectively. Is there a way to control how they look in CSV? I debugged and monitored the code right before they were exported, and the dates are correct. I would appreciate any help.

+3
source share
5 answers

As far as I know, it is impossible to control how dates are displayed in Excel when using CSV (it can only be controlled using Excel custom settings). We had a similar problem (with phone numbers), and the only workaround that works reliably is to use the Excel format. We use the Apache POI to write .xls files [*] where we can set the cell type to text.

At least for phone numbers, this is necessary - otherwise Excel drops the leading 0s and treats them as numbers (which makes it impossible to distinguish between national and international phone numbers). However, for dates, it might be worth considering if it's better to just let the user decide how the dates are displayed?

[*] CSV , Excel.

0

java.text.SimpleDateFormat

+4

( ) CSV . , , , ( ):

03-01-2012, = "03.01.2012",

+2

, , Joda Time

DateTimeFormatter formatter = new DateTimeFormatterBuilder().appendMonthOfYear(2)
                          .appendDayOfYear(2).appendYear(4, 4).toFormatter();
LocalDate d = LocalDate.parse(dateStr, formatter);
Date yourJavaDate = d.toDate();
... // work with java date (if needed! :( 
LocalDate outgoing = new LocalDate(yourJavaDate.getTime());
String myOutStr = outgoing.toString(formatter);
0

i.e.,

select ' '||to_char(mydatecolumn,'YYYY-MM-DD HH:MI:SS AM') DATEVALUE from mytable;

CSV, , TEXT, EXCEL , EXCEL Date.

0

All Articles