gpt4 book ai didi

java - 将日期从 "2009-12 Dec"格式转换为 "31-DEC-2009"

转载 作者:搜寻专家 更新时间:2023-11-01 01:30:04 25 4
gpt4 key购买 nike

'2009-12 Dec' should be converted to '31-DEC-2009'
'2010-09 Sep' should be converted to '30-SEP-2010'
'2010-02 Feb' should be converted to '28-FEB-2010'
'2008-02 Feb' should be converted to '29-FEB-2008'

2009-12 Dec2008-02 Feb 将在下拉列表中显示给用户。用户无法选择DAY

用户选择的值应该传递给数据库。但数据库期望日期格式为 DD-MMM-YYYY .查询有' <= USER_DATE ' 健康)状况。因此,应自动选择该月的最后一天并将其传递给数据库。

请帮助我编写完成上述工作的函数。

static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM MMM");

public static String convertMapedToSqlFormat(final String maped) {
String convertedMaped = null;
//....
return convertedMaped;
}

@Test
public void testConvertMapedToSqlFormat() {
String[] mapedValues = { "2009-12 Dec", "2009-11 Nov", "2009-10 Oct",
"2009-09 Sep", "2009-08 Aug", "2009-07 Jul", "2009-06 Jun",
"2009-05 May", "2009-04 Apr", "2009-03 Mar", "2009-02 Feb",
"2009-01 Jan", "2008-12 Dec", "2008-11 Nov", "2008-10 Oct" };
for (String maped : mapedValues) {
System.out.println(convertMapedToSqlFormat(maped));
}
}

最佳答案

将其转换为 Calendar并使用 Calendar#getActualMaximum()获取月份的最后一天并用它设置日期。

启动示例:

String oldString = "2009-12 Dec";
Calendar calendar = Calendar.getInstance();
calendar.setTime(new SimpleDateFormat("yyyy-MM").parse(oldString)); // Yes, month name is ignored but we don't need this.
calendar.set(Calendar.DATE, calendar.getActualMaximum(Calendar.DATE));
String newString = new SimpleDateFormat("dd-MMM-yyyy").format(calendar.getTime()).toUpperCase();
System.out.println(newString); // 31-DEC-2009

关于java - 将日期从 "2009-12 Dec"格式转换为 "31-DEC-2009",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3839584/

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