简体   繁体   English

将Excel日期转换为mm / dd / yyyy格式

[英]converting excel date into mm/dd/yyyy format

I am reading from an excel sheet using poi jar where in there is a date column.If i print out the value of the data cloumn : 我正在使用poi jar从excel表格中读取,其中有一个日期列。如果我打印出数据cloumn的值:

cell.getDateCellValue()

It gives me value like 3124.0 它给我像3124.0一样的价值

So i tried with this code to convert to the mm/dd/yyyy format: 因此,我尝试使用此代码将其转换为mm / dd / yyyy格式:

SimpleDateFormat sdf = new SimpleDateFormat("MM/DD/YYYY");
String  s =  sdf.format(cell.getDateCellValue());
System.out.println(s);

But for reading a date 06/30/2001 i got this value as output: 但是对于读取日期06/30/2001,我得到了此值作为输出:

06/181/2001 2001年6月18日

First check your date format 首先检查您的日期格式

  SimpleDateFormat sdf = new SimpleDateFormat("MM/DD/YYYY");

it should be 它应该是

 SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");

and also check the POI API For more info to deal with dates in excel,there are better ways to handle the dates. 并检查POI API有关在excel中处理日期的更多信息,还有更好的方法来处理日期。

Refer HSSFDateUtil 请参阅HSSFDateUtil

check HSSFDateUtil.isCellDateFormatted() , getExcelDate ,getJavaDate 检查HSSFDateUtil.isCellDateFormatted(),getExcelDate,getJavaDate

getJavaDate getJavaDate

public static java.util.Date getJavaDate(double date) 公共静态java.util.Date getJavaDate(双日期)

Given an Excel date with using 1900 date windowing, and converts it to a java.util.Date. 使用1900年日期窗口给定Excel日期,并将其转换为java.util.Date。

NOTE: If the default TimeZone in Java uses Daylight Saving Time then the conversion back to an Excel date may not give the same value, that is the comparison excelDate == getExcelDate(getJavaDate(excelDate,false)) is not always true. 注意:如果Java中的默认TimeZone使用夏时制,则返回Excel日期的转换可能不会给出相同的值,即excelDate == getExcelDate(getJavaDate(excelDate,false))的比较并不总是正确的。 For example if default timezone is Europe/Copenhagen, on 2004-03-28 the minute after 01:59 CET is 03:00 CEST, if the excel date represents a time between 02:00 and 03:00 then it is converted to past 03:00 summer time Parameters: 例如,如果默认时区为Europe / Copenhagen,则在2004-03-28 CET的后一分钟为CEST 03:00,如果excel日期表示的时间在02:00至03:00之间,则将其转换为过去的时间夏令时03:00参数:

date - The Excel date. date-Excel日期。 Returns: Java representation of the date, or null if date is not a valid Excel date 返回:日期的Java表示形式;如果da​​te不是有效的Excel日期,则返回null

See Also: TimeZone 另请参见:TimeZone

also refer Reading date values from excel cell using POI HSSF API 另请参阅使用POI HSSF API从excel单元读取日期值

Your Date Format String is wrong. 您的日期格式字符串错误。 Should be like following 应该像下面

SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");

Read the Java doc for SimpleDateFormat 阅读Java文档中的SimpleDateFormat

Check out the JavaDoc for java.text.SimpleDateFormat : 签出JavaDoc for java.text.SimpleDateFormat

D is for Day in year D是一年中的一天

Y is for Week in year. Y是一年中的周。

You propably want to use the lowercase variants for d Day in Month and y Year 您可能想在月和y年的d天中使用小写变体

You want to write "MM/dd/yyyy" as your date format. 您想输入"MM/dd/yyyy"作为日期格式。 Capital DD means day of year, which for 30 June is 181. 大写DD表示每年的一天,6月30日为181。

I strongly recommend reading the JavaDoc for the SimpleDateFormat class . 我强烈建议阅读JavaDoc的SimpleDateFormat

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM