java 将excel日期转换为mm/dd/yyyy格式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31266163/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
converting excel date into mm/dd/yyyy format
提问by coder
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
06/181/2001
回答by KDP
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
check HSSFDateUtil.isCellDateFormatted() , getExcelDate ,getJavaDate
检查 HSSFDateUtil.isCellDateFormatted() , getExcelDate ,getJavaDate
getJavaDate
获取Java日期
public static java.util.Date getJavaDate(double date)
public static java.util.Date getJavaDate(double date)
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. 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:
注意:如果 Java 中的默认 TimeZone 使用夏令时,则转换回 Excel 日期可能不会给出相同的值,即比较 excelDate == getExcelDate(getJavaDate(excelDate,false)) 并不总是正确的。例如,如果默认时区是欧洲/哥本哈根,则在 2004 年 3 月 28 日,欧洲中部时间 01:59 之后的分钟是欧洲中部时间 03:00,如果 excel 日期表示 02:00 和 03:00 之间的时间,则将其转换为过去03:00 夏令时参数:
date - The Excel date. Returns: Java representation of the date, or null if date is not a valid Excel date
日期 - Excel 日期。返回: 日期的 Java 表示,如果日期不是有效的 Excel 日期,则返回 null
See Also: TimeZone
另请参阅:时区
also refer Reading date values from excel cell using POI HSSF API
回答by shazin
Your Date Format String is wrong. Should be like following
您的日期格式字符串错误。应该像下面这样
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
Read the Java doc for SimpleDateFormat
阅读SimpleDateFormat的 Java 文档
回答by Dawood ibn Kareem
You want to write "MM/dd/yyyy"
as your date format. Capital DD
means day of year, which for 30 June is 181.
你想写成"MM/dd/yyyy"
你的日期格式。大写DD
是一年中的第几天,6 月 30 日是 181。
I strongly recommend reading the JavaDoc for the SimpleDateFormat
class.
我强烈建议阅读该类的JavaDocSimpleDateFormat
。
回答by hinneLinks
Check out the JavaDoc for java.text.SimpleDateFormat
:
查看 JavaDoc 以了解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
您可能希望对d
月和y
年中的日使用小写变体