簡體   English   中英

將日期從“2009-12 Dec”格式轉換為“31-DEC-2009”

[英]convert date from “2009-12 Dec” format to “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'

將在下拉列表中向用戶顯示2009-12 Dec2008-02 Feb的值。 用戶無法選擇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));
        }
    }

將其轉換為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
  • 使用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
  • 避免在數據庫中使用嚴格的日期格式。

從字符串的YYYY-MM部分獲取年份和月份。

使用JODA創建與該月第一天相對應的時間點。 向前移動一個月,向后移動一天。 將時間平坦到您需要的字符串表示。

嗨,你必須解析你的日期,就像這樣

      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

java.time

使用現代java.time類可以更容易地取代問題和其他答案中看到的麻煩的舊日期時間類。

java.time框架內置於Java 8及更高版本中。 這些類取代了舊的麻煩的日期時間類,如java.util.Date.Calendarjava.text.SimpleDateFormat

現在處於維護模式Joda-Time項目還建議遷移到java.time。

要了解更多信息,請參閱Oracle教程 並搜索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值的String表示形式。

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上的許多其他問題和答案中多次顯示。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM