【问题标题】:convert date from "2009-12 Dec" format to "31-DEC-2009"将日期从“2009-12 月”格式转换为“31-DEC-2009”
【发布时间】:2011-04-19 20:51:26
【问题描述】:
'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));
        }
    }

【问题讨论】:

  • 您的 2008 年样本结果中有错字。
  • @martin Clayton:更正了。谢谢:)
  • 真的,你需要使用<和下个月的第一天,否则你会失去当月最后一天的事件。示例2009-12-31T13:00:00 应该在2009-12 Dec 范围内,但使用您的方法则不会。
  • @Ben
  • 是的,因为2009-12-31T13:00:00 <= 2009-12-31 是假的。正确的检查是2009-12-31T13:00:00 < 2010-01-01

标签: java date date-format simpledateformat


【解决方案1】:

您好,您必须解析您的日期, 像这样

      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

【讨论】:

  • 没有要解析的“日”部分。
  • 请不要笑看别人的答案。
【解决方案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
  • 避免在数据库中使用严格的日期格式。

【讨论】:

  • 你如何获得当月最后一天的部分?
  • 小修正 - 返回 dbDateFormat.format(date.getTime());应该返回 dbDateFormat.format(cal.getTime());
【解决方案3】:

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

使用 JODA 创建对应于该月第一天的时间点。前进一个月,后退一天。将时间扁平化为您需要的字符串表示形式。

【讨论】:

  • 向前移动一个月并使用<,否则您将丢失该月最后一天除午夜之外的任何时间发生的任何事件。
【解决方案4】:

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

【讨论】:

    【解决方案5】:

    java.time

    现代 java.time 类取代了问题和其他答案中出现的麻烦的旧日期时间类,现在变得容易多了。

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

    现在在maintenance modeJoda-Time 项目也建议迁移到 java.time。

    要了解更多信息,请参阅Oracle Tutorial。并在 Stack Overflow 上搜索许多示例和解释。

    大部分 java.time 功能在ThreeTen-Backport 中向后移植到Java 6 和7,并进一步适应ThreeTenABP 中的Android

    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 上的许多其他问题和答案中多次显示。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-23
      • 2015-04-29
      • 2011-04-02
      • 1970-01-01
      相关资源
      最近更新 更多