Processing date format When Java imports Excel (verified OK)

Source: Internet
Author: User

In Excel, the date format, such as 2009-12-24 to convert it to a number format becomes 40171, in Java processing, the reading will be 40171.
If you use a POI to process a cell of a date type in Excel, you will end up with the numeric type if you are only judging whether it is a date type. The correct way to do this is to first determine the type of cell is otherwise numeric type, and then determine whether the cell is a date format, if so,

Create a date format, and then display the contents of the cell in this date format. If the cell is not a date format, then it is OK to get the numeric value directly. The specific code is as follows:

The main is to Judge NUMERIC when a colleague determines whether the cell is a date format if the date format is converted directly into a date format string return value

It's OK.

12345678910111213141516171819202122 if (0 = = cell. Getcelltype()) { //Determine if the date typeif(hssfdateutil. iscelldateformatted(cell)){ //For conversion to date formatDate d = cell. Getdatecellvalue(); dateformat formater = new simpledateformat("Yyyy-mm-dd HH:mm:ss" ); str[k] = formater. Format(d); }Else{ //For formatting numbers, leaving only the integer part of the number decimalformat df = new decimalformat("########"); str[k] = df. Format(cell. Getnumericcellvalue()); }

Processing date format When Java imports Excel (verified OK)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.