gpt4 book ai didi

java - 将字符串日期转换为日期对象,并且年份也存储为 YYYY

转载 作者:行者123 更新时间:2023-12-02 04:07:39 25 4
gpt4 key购买 nike

我正在读取一个 Excel 值,它是字符串类型的日期,我将其转换为日期类型,以 dd/MM/YYYY 格式存储日期,所以下面是我这样做的方式

private static final SimpleDateFormat outputDate = new SimpleDateFormat("dd/MM/yyyy");

String dealDateString = cell.getStringCellValue(); // inside this deal date is stored as 05/07/13
if (dealDateString != null) {
java.util.Date dealDate = convertStringToDate(dealDateString);
String dd = outputDate.format(dealDate); // ----> now the date is stored inside dd string as DD/MM/YYYY as 05/07/2013
DateFormat format = new SimpleDateFormat("dd/MM/yyyy", Locale.UK);
java.util.Date date = format.parse(dd); //--> but inside date object the year is stored as 13 that is year is stored as YY
}

下面是我设计的自定义方法,它将接受字符串并将其转换为日期对象

public java.util.Date convertStringToDate(String stringValue) {
String[] formatStrings = { "dd/MM/yyyy", "dd-MMM-yyyy" ,"dd/MM/YY" , "dd-MM-YY"};

for (String formatString : formatStrings) {
try {
return new SimpleDateFormat(formatString).parse(stringValue);
} catch (ParseException e) {
logger.warn("##$$$$$### Error in invoice inside convertStringToDate method : ##$$$$$$#### "
+ ErrorUtility.getStackTraceForException(e));

}
}

return null;
}

现在日期已转换为 java 日期对象,但年份被扣除,假设年份是 2014 年,那么 年份存储为 14 ,请告知我如何将年份存储为 2014 主要问题是年份仅存储为两位数我想将其存储为 YYYY

folks please advise

最佳答案

尝试写下这一行:

String[] formatStrings = { "dd/MM/yyyy", "dd-MMM-yyyy" ,"dd/MM/YY" , "dd-MM-YY"};

像这样:

 String[] formatStrings = { "dd/MM/yyyy", "dd-MMM-yyyy" ,"dd/MM/yy" , "dd-MM-yy"};

关于java - 将字符串日期转换为日期对象,并且年份也存储为 YYYY,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34102748/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com