How do i apply custom date formatting from excel in java? - java

I have an 2007 excel file called Test10. The first row has 10 headers, test1 test2 etc to test10. The second row, column 3 has a date, 10-10-2020 on the cell with the 10-10-2020 date there is a custom date format -> dd-mm-yyyy
The date in the cell is written as 2020-10-10.
I have the format from the cell in Java, however when I get it it's dd/-mm/-yyyy;# and the date is 2020-10-10.
I need to somehow apply that format to the date at hand.
I need something more flexible, is there a class that does that format? All I have managed to find so far is how to apply formatting to the excel in java when creating the excel, but not the other way around, when getting it from the Excel file.
I can manually serialize the format and remove the special characters that are not needed but that is a workaround.
I need a class that will apply the strange looking format to the date, in Java. Is there anything like that?
Also the example I provided above is a very simple example.
We're using a custom handler, the date comes as a double and we currently use DateUtil.getJavaDate to get the date as a string.
I have not created the handler so it's quite hard to understand the whole of it, but somewhere in the handler, the custom formatting for the cell is extracted from the excel and put into a variable, at the point where the DateUtil.getJavaDate is called I have access to the formatting variable.
Previously it was using SimpleDateFormat(CONSTANT_FORMAT).format(DateUtil.getJavaDate(d)) to format it, but I need to apply the custom format. Is there a library that parses xlsx date formats. As the dd/-mm/-yyyy;# format will not work with SimpleDateFormat.
Also I cannot serialize the format hardcoded as if I replace mm with MM how do I know if the user wanted minutes or months?

DataFormatter from org.apache.poi handles the weird excel formats

Related

Using java to change excel date re

So I am creating a csv report within my java code and using excel to open the exported csv file. One of the column is a date which I am formatting within my code to be mm/dd/yyyy hh:mm:ss. This comes out as 02/10/2014 3:38:00 PM. Which is exactly how I want it. However the columns in the excel sheet display this as 02/10/2014 3:38. When I click on a cell in the excel sheet, it does display the full date at the top but I want it to display on the column itself so that it is easier to print. It doesn't seem like a column width issue since I have changed the column width but the full date still won't appear. I am however able to achieve it by changing the number format cells setting to custom. Is this something that can be done within java itself? Let me know if you need more information. Thanks!
Comma-separated values (CSV) is stores tabular data in plain-text format. To give Excel an instruction how to format a particular column you would need to user Excel format. In order to achieve it, you may use a Java library to export data in Excel format. One example of such a library is Apache POI - the Java API for Microsoft Documents (http://poi.apache.org/).
In addition, to work better with CSV files in Excel use import from text feature. This is a wizard you can specify the import settings, including column formats, width of the fields etc.
I hope it helps.

Excel cell values containing times are reported as 12 hour instead of 24 hour with JExcelApi

I need to read an Excel document and extract some cell values for further processing. I use JExcelAPI for this. This works well.
We need to have a cell containing wall time as a four character string (like "0810" for ten past eight and "1550" for ten to four) which is achived by calculating the time and custom formatting it with the "ttmm" minute pattern. ("t" is 24 hour in the Danish locale. This is in Denmark which currently is GMT+2, and our Windows machines are with English locale)
My problem is now that when I read in the cells with cell.getContents() the first cell is correctly "0810" but the second is "0350" instead of "1550". I do not have a deep understanding of jxl so I do not know if that value comes straight from the sheet as generated by Excel when saving or is generated deep down in jxl at runtime. Manipulating the Date stored in the DateCell is also cumbersome as the timezone is wrong, and we have other date cells which does not need this treatment.
I was also thinking if we can do the calculations in a field not read, and then convert the contents to a four character string in a field read?
So, how can I get the times in the form I need?
(Note: I will award a 500 point bounty for the most helpful answer. I just cannot open it yet)
I did not find a solution.
We ended up in coercing the date into the desired text string in Excel itself (in a different cell, and ignoring the date cell when reading the sheet in Java).

How to input a date with a specific date format in struts 2.2.3.1?

I want to know if there is a way to force the input format for a date in struts, something like dd/MM/yyyy .
The date is written in a text field. I read that this is a locale sensitive subject, but it seems a too much to switch locales for a simple changing of format.
I know I could call addFieldError(fieldName, errorMessage) inside the method of my Action class but to me it feels like a date format should have been thought of to be configured in an Action-validation.xml file. I searched to configure this with properties file but found no working example.
Is using a custom StrutsTypeConverter and addFieldError(fieldName, errorMessage) the only way to do it? Or is there another version of struts in which the date format inputed in a text field can be specified?
UPDATE:
I assumed I made myself clear enough but some answers demostrate otherwise.
The use case is: User enters manually a date in an arbitrary format in a textfield, afterwards struts2 validates the date format as corresponding to the format set by the programmer or not, displaying a message accordingly.
A jQuery date picker with a <s:date> tag can be an alternative to what I want to achieve, but it it NOT what I asked. Furthermore, Javascript can be disabled, rendering the date picker useless, jQuery being a Javascript library as far as I understand it.
My action implemented ModelDriven but the date format can't be supplied anywhere to be forced in the text field, I did use my own validator, but I changed the mapping of the form element to be a String named in the action class, then used a validator with a SimpleDateFormat with the format retrieved from a resource bundle to convert it to a date, then formated the date again, to see if the representation is the same as the one inputed in the text field. Hardly after all that the format is clear. If there are other suggestions I welcome them, this seems like the only choice.
I find this drawback at the very least annoying.
If you want to have a specific format of date on your page, i will advise you to use struts 2 jquery datepicker. You can find a demo of this following this url. Go to widgets tab, select datepicker.

Apache POI localized Date into Excel cell

I have a problem. I'm use Apache POI 3.8 and I needed set's Date into Excel cell. But I don't understand - how to set the date depending on the user locale. Because date in US format is month/day/year, in russian locale - day/year/month. I'm use next code for my task
CellStyle dateStyle = workbook.createCellStyle();
dateStyle.setDataFormat(dataFormat.getFormat("m/d/yy"));
cell.setCellStyle(dateStyle);
If anybody know - how to set's cell Date value depending on the user locale, please, answer.
Note. If I'm use "m/d/yy h:mm", then I not see my date in Excel file(I see only "######" string in cells).
Thank you.
Try and expand the cells. Sometimes if the cell width is too short ###### is seen as the output. To get the localized pattern you can see this reference
Excel shows dates and numbers according to locale settings of the machine on which document is opened. So you don't need to care about it.
In your code you use pattern "m/d/yy". Excel automatically will convert it to "dd.MM.yyyy" in "de_DE" locale, "dd/MM/yyyy" in "en_GB" locale and so on.
You can test it changing locale of your machine (for Windows: Control Panel -> Clock, Language and Region -> Language -> Change date, time, or number). As you will see the same Excel document will be displayed differently for different locales.
About "######", you can use Sheet#autosizeColumn. But be careful with it, because as it is described in the documentation:
"This process can be relatively slow on large sheets, so this should
normally only be called once per column, at the end of your
processing"
Locale can be set on formatListener
MissingRecordAwareHSSFListener listener = new MissingRecordAwareHSSFListener(this);
formatListener = new FormatTrackingHSSFListener(listener, Locale.UK);

Excel currency format with Apache POI

I use Apache POI to write excel sheets. I'm very happy with it, but I have the following problem: my program is multi-currency and internationalized. In the excel sheet I write, I want my currency cells to be properly formatted. For that,
I need a routine that would build the right Excel currency format (eg: "[$$-409]#,##0.00;-[$$-409]#,##0.00") from java.util.Currency and java..util.Locale parameters.
Something like: public String getExcelCurrencyFormat(Currency currency, Locale locale);
Is anyone aware of such a library?
Couldn't you just flag the field as being a currency field, and let Excel figure out the right format to use? Or does Excel require you to specify the format to produce the relevant 'type'?
I would have suggested using java.text.NumberFormat.getCurrencyInstance() but there appears to be no way to get the actual format used by the instance from the object itself (unless tostring() returns it).
Off the top of my head I can't think of anything else sorry.

Categories

Resources