0

I'm trying to read a date cell from excel file, i'm using DataFormatter but the problem is that the format that i get is not correct (day and month are inverted), example: in the excel file: 15/03/2018 After reading: 03/15/2018

I need to fix this or convert the cell to string format in order to compare it with another string.

thanks in advance.

This is how i read the cell:

DataFormatter formatter = new DataFormatter();
String current_date = formatter.formatCellValue(date_cell);
4

3 回答 3

4

As seen in the javadocs at the top, Locale.US and Locale.UK can get mixed up.

After reading you get the US convention Month/Day/Year instead of the UK's one Day/Month/Year.

DataFormatter formatter = new DataFormatter(Locale.UK);
于 2018-06-04T23:52:54.350 回答
0

Try something like this:

String s = "03/15/2018";    
Date d = new Date(s);   
DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
String t = df.format(d);
System.out.println(t);

(s corresponds to your current_date variable and follows directly after your other code)

NB: you have to use capital Ms because in date/time formats 'm' stand for minute (so my bad on previous code)

NB^2: the code "new Date(s)" is definitely deprecated and bad and wrong as per previous discussion. If it matters there's probably a more canonical answer involving essentially the same thing but with Calendars.

于 2018-06-05T00:11:30.327 回答
-1

You can build a specific date formatter, e.g.

SimpleDateFormat df = new SimpleDateFormat("yyyy-mm-dd");
df.parse(

(etc)

So if it's reading the date correctly (by which I mean days are being read as days, and months are being read as months), then it's a simple matter of making a date formatter specific to the output you want.

e.g.

SimpleDateFormat df = new SimpleDateFormat("dd/mm/yyyy");

and then something like ... uh ...

String current_date = df.format(date_cell);

or if you need the cell formatter to read the value for you it'll be a little bit more complex.

As an aside:

Note that since SimpleDateFormat is relatively simple and easy to use it is probably deprecated and regarded as bad, evil and wrong (tm).

(For the reason that once you start introducing things like time zones, localization and differences between calendar systems things become complex very quickly (because the real world is messy) and simple answers are only good for the quick and dirty 'local' fix)

于 2018-06-04T23:52:34.380 回答