转换日期从“2009-12 Dec”格式为“31-DEC-2009” [英] convert date from "2009-12 Dec" format to "31-DEC-2009"

查看:327
本文介绍了转换日期从“2009-12 Dec”格式为“31-DEC-2009”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

$ _ code>'2009-12 Dec'应转换为'31 -DEC-2009'
'2010-09 Sep'应转换为'30 -SEP-2010'
'2010-02 Feb'应转换为'28 -FEB-2010'
'2008-02 Feb'应转换为'29 -FEB-2008'

2009-12 Dec 2008-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十月,2009-09年9月,2009-08八月,2009-07年七月,2009-06六月,b $ b2009-05五月 2009-04四月,2009 - 03年3月,2009-02二月,2009-01一月,2008-12年12月,2008-11年11月,2008-10年10月};
for(String maped:mapedValues){
System.out.println(convertMapedToSqlFormat(maped));
}
}


解决方案

转换它要 日历 并使用 日历#getActualMaximum() 获取最后一个月的日期,并设置一天。



启动示例:

  String oldString =2009-12 Dec; 
日历calendar = Calendar.getInstance();
calendar.setTime(new SimpleDateFormat(yyyy-MM)。parse(oldString)); //是的,月份名称被忽略,但我们不需要这个。
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


'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'

The values 2009-12 Dec, 2008-02 Feb will be displayed to the User in a drop down. The User have no option to select the DAY.

The user selected value should be passed to the Database. But the database expects the date in the format DD-MMM-YYYY. The query has '<= USER_DATE' condition. So, the last day of the month should be automatically selected and passed to the database.

Pl help me in writing the function that does the above job.

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));
        }
    }

解决方案

Convert it to Calendar and use Calendar#getActualMaximum() to obtain last day of month and set the day with it.

Kickoff example:

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

这篇关于转换日期从“2009-12 Dec”格式为“31-DEC-2009”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆