POI SAX Date data type - java

I'm trying to get date data types from an excel file, but the output when he's reading is 41306.038888888892.
This value just appear for date, is there any way to get the normal date?
I did not find anything searching in the web.
Hope someone can help
thanks

As with your previous question, I'd strongly suggest you try reading and understanding some of the various examples for this, you'll save yourself a lot of time! The two main ones probably being XSSFEventBasedExcelExtractor in Apache POI and XSSFExcelExtractorDecorator in Apache Tika
If you take the easy route, then you can just use XSSFSheetXMLHandler, which will handle all the pesky formatting stuff for you, and give you nicely formatted strings for your dates
Otherwise, if you want to stay at the low level, then you need to check the formatting rule applied to a cell. If it's a date-based format string, you then need to convert it from a number into a Date object. Handily, there's a POI class DateUtil which can both help you check if a cell is Date formatted, and convert it into a Java Date object for you

Related

TextView inputType date in Android - how does it work?

I have not found this in any documentation, tutorial or question. Pretty much all tutorials concerned date picker, so I am confused about its functionality.
I have TextView with inputType="date". How do I handle it? Does actually it do anything other than text field does?
What are best practices?
It only has set/get methods for String type. Am I right in assumption that I have to parse contents manually? And if putting in default value, having to format it myself too?
Is there any definition of expected date format? Does it handle Locale differences (mm/dd/yyyy in some, dd/mm/yyyy in other)?
Is there any validation?
Or is it basically "do it yourself" type thing without much convenience?
input type date means it will open numeric virtual keyboard on the screen when the user tap the textview. To parse it to your required format, you have to use JAVA date time class.
Input type simply specifies which kind of keyboard you want the user to use, to simplify the input. For example, a phone number input field does not require letters. Yes, you have to manually parse it, or use helper classes as stated by Mark Henry.
Textview - inputType

Java Apache POI writing Double value in Cell (german Excel)

i have a problem. When i am writing a double value (say 20.3) in an excel cell and specify a format ("0.0") the "german" Excel does not recognize the value as a number, because in Germany the separator for decimal numbers is a comma, not a point. For example the "english" number 1,000.00 would be 1.000,00
I tried it with a DateFormat like "0,0", but excel still thinks the value is a string.
I need the value to be a number, because of formulas.
And: The clients of my software use Excel with german number format. There is no dicussion about it :(
Is there a way to say POI to write a double value "the german" way?
Or is there any workaround for this problem?
Thanks a lot!
This shouldn't be a problem because in POI the setCellValue(double) method is overridden for double and thus independent of locale settings. It looks like you are using setCellValue(String) or setCellValue(RichTextString). Correct that and it should work.
If that doesn't work for you, you have to post your code.

RegularTimePeriod creation or convertion in java

I want to create a RegularTimePeriod object with hour,min,seconds i have,how to do this i googled and got http://www.jfree.org/jfreechart/api/javadoc/org/jfree/data/time/RegularTimePeriod.html but i dont seem to understand what RegularTimePeriod contain create one.
in short lets say i have 02:33:54 i have broken it into h=02,m=33,s=54 now can i create a regulartimeperiod object with h,m,s?if so how
if not which object can pass to TimeSeries in jfreechart to get hh:mm:ss format along x-axis
Thanks in advance
You can override the date format. Related examples may be found here and here.
axis.setDateFormatOverride(DateFormat.getTimeInstance());

Common datetime formats in log files

I'm looking for a list of common datetime formats used in logs (e.g. webserver, database, etc).
Even better would be a (java) library that can extract date and time from a given string ( < 10KB).
Does anyone know a good one?
this library is likely a good place to start: SimpleDateFormat
The docs contains the an introduction to the standard datetime format strings. But as #Olaf points out, you're going to need to specify what the format is beforehand or there is literally no way differentiate certain dates from one another.
Looks like what you'd want to do is construct a range of date formats that might match, apply all of them to a date string, then see which date is closest to Datetime.now().
Although this doesn't answer your question directly, but Java includes libraries for working with regular expressions. It would be pretty easy to write a library of your own based on that. I've has a lot of success extracting all sorts of data using regular expression. It would certainly be less than 10kb and would require no external dependencies other than the JDK.

XMLAdapter: Configurable Date Format

I'm unmarshaling XML data using JAXB (and a little Saxon for XSLTs). In my XML document I have a date string value.
I did some research and experimented with XMLAdapter and #XMLJavaTypeAdapter. I found a useful Q&A on StackOverflow here: jaxb unmarshal timestamp. The solution outlined there involving a SimpleDateFormat seems to work well.
Here is my problem: The format of the date string in the XML is variable from document to document. It can be in a different date format each time. In the current system (that I am replacing with all this XML manipulation) there are date formats in database tables that are retrieved and applied when the XML document is parsed. Manually. Line by line. (Now you see why I'm replacing it with JAXB, yeesh!)
So the question would be: How would I change out the date format string being fed to the SimpleDateFormat in the XMLAdapter for different documents? Is such a thing even possible? Am I doomed to reading the date string out as a String and then converting it to a Date somewhere else later on?
Edit: I was just going to delete this question, but it got up-voted, so I guess I'll write out a proper answer to it as to what I ended up doing. I'm going to hold off on accepting my own answer for awhile, in case anyone has some insight that leads to an even better answer.
You can use an XmlAdapter to handle the date format. Since the date formats differ from doc to doc you can take advantage of specifying an initialized instance of the adapter on the Unmarshaller. In this way you could set the date format appropriate to the document you want to unmarshal.
Example
http://blog.bdoughan.com/2011/09/mixing-nesting-and-references-with.html
Here is what I ended up going with: I applied the exact solution found in the Q&A I linked to in my question, found here: jaxb unmarshal timestamp.
You may be thinking: "This would appear to leave this guy with the same problem he had originally. He still has a static date format string in his SimpleDateFormat object, but his date strings are still of a variable format! He hasn't solved anything! What an idiot!"
Here is my solution: I'm already applying an XSLT to my XML document; I have a different XSLT for each XML document type. So I'm going to take the XSLT I already (have to) have, and add functionality in it to convert the date string to the same date format as the one specified in my SimpleDateFormat object in my XMLAdapter.
Ta da! I don't need to configure jack now. My configuration is inherent in the XSLT I already (and always) apply before I attempt to unmarshal.

Categories