将日期从“2009-12 Dec”格式转换为“31-DEC-2009”

时间:2010-10-01 13:26:50

标签: java date date-format simpledateformat

'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 12月 2008-02 2月将在下拉列表中显示给用户。用户无法选择 DAY

应将用户选择的值传递给数据库。但是数据库期望格式为DD-MMM-YYYY的日期。该查询具有“<= USER_DATE”条件。因此,应自动选择月份的最后一天并将其传递到数据库。

Pl帮助我编写完成上述工作的功能。

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

5 个答案:

答案 0 :(得分:7)

将其转换为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

答案 1 :(得分:2)

  • 使用DateFormat(但将其修复为yyyy-dd MMM)来解析日期
  • Date转换为Calendar
  • 使用Calendar.getActualMaximim()
  • 使用dd-MMM-yyyy格式化获取的日期。
  • 致电.toUpperCase()

所以:

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

public static String convertMapedToSqlFormat(final String maped) {
    Date date = dateFormat.parse(mapped);
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
    return dbDateFormat.format(cal.getTime()).toUpperCase();
}

一些注意事项:

  • 如果可能,请使用joda-time DateTime
  • 避免在数据库中使用严格的日期格式。

答案 2 :(得分:1)

从字符串的YYYY-MM部分获取年份和月份。

使用JODA创建与该月第一天相对应的时间点。向前移动一个月,向后移动一天。将时间展平为您需要的字符串表示。

答案 3 :(得分:0)

你好,你必须解析你的约会, 像这样

      SimpleDateFormat df = new SimpleDateFormat("MM/dd/yyyy");
      Date du = new Date();
      du = df.parse(sDate);
      df = new SimpleDateFormat("yyyy-MM-dd");
      sDate = df.format(du);

希望这会有所帮助。 如果有,请告诉我。

<强> PK

答案 4 :(得分:0)

java.time

现在的java.time类更容易取代问题和其他答案中看到的麻烦的旧日期时间类。

java.time框架内置于Java 8及更高版本中。这些类取代了旧的麻烦日期时间类,例如java.util.Date.Calendar和&amp; java.text.SimpleDateFormat

现在在maintenance mode中,Joda-Time项目还建议迁移到java.time。

要了解详情,请参阅Oracle Tutorial。并搜索Stack Overflow以获取许多示例和解释。

大部分java.time功能都被反向移植到Java 6&amp; ThreeTen-Backport中的7,并进一步适应Android中的ThreeTenABP

ThreeTen-Extra项目使用其他类扩展java.time。该项目是未来可能添加到java.time的试验场。

YearMonth

YearMonth课程提供您想要的内容。

YearMonth start = YearMonth.of( 2008 , Month.OCTOBER );
YearMonth stop = YearMonth.of( 2009 , Month.DECEMBER );
List<YearMonth> yms = new ArrayList<>();
YearMonth ym = start ;
while( ! ym.isAfter( stop ) ) {
    yms.add( ym );
    // Set up the next loop.
    ym = ym.plusMonths( 1 ); 
}

要显示,请使用DateTimeFormatter生成YearMonth值的字符串表示。

DateTimeFormatter f = DateTimeFormatter.ofPattern( "uuuu-MM MMM" );
f = f.withLocale( Locale.CANADA_FRENCH );  // Or Locale.US etc.
String output = ym.format( f );

要获取该月的最后一天,请询问YearMonth对象。

LocalDate endOfMonth = ym.atEndOfMonth();

要呈现,请使用DateTimeFormatter。要么实例化一个自动本地化为指定Locale的格式化程序,要么指定自己的格式化模式。在Stack Overflow上的许多其他问题和答案中多次显示。

相关问题