2016-11-13 10 views
0

私の日付の形式はyyyyMMddHHmmで、この日付の形式から年、月、日を抽出し、新しい日付を作るためにHHmmとして1259を追加する必要があります。 joda date時間。分単位でゼロと1桁の数字を入力するパッド

String date = (Integer.toString(orderDate.getYear()) + Integer.toString(orderDate.getMonthOfYear()) + Integer.toString(orderDate.getDayOfMonth()) + "1259"); 

orderDate = DateTimeFormat.forPattern(DATE_FORMAT).withZone(DateTimeZone.forTimeZone(TimeZone.getTimeZone(date1TimeZone))).parseDateTime(String.valueOf(date)); 

しかしorderDateは201611051212 であることを起こる場合、私は取得しています結果は月の値すなわち20161151259は05は、クロック入力のための任意の書式指定子があるので、私が欲しい5であるのですか?

答えて

2

Jodaからdocs:「パターン文字の数によってフォーマットが決まります。

解決方法は、Joda metohdsを使用して日付を変更することで、文字列を操作するための工夫をする必要がないようにすることです。

// your initial date 
    DateTime initialDate = DateTimeFormat.forPattern("yyyyMMddHHmm").parseDateTime("201611051212"); 

    // the operation you're trying emulate by adding a string, better to use specialized method 
    DateTime resultingDate = initialDate.withTime(12, 59, 0, 0); 

    // resulting string representation matches to what you specified as an expected result 
    String result = DateTimeFormat.forPattern("yyyyMMddHHmm").print(resultingDate); 
    Assert.assertEquals("201611051259", result); 
関連する問題