3

開始日と終了日という2つの日付があります。月が一致した場合は「20-23AUG」のように折りたたまれ、月末に「20 SEP-1 OCT」のように壊れても正しくフォーマットされるように、フォーマットしたいと思います。これを実現するためのライブラリはありますか、または個々のDateFormatを使用して日付の範囲を表示するためのコードルールを渡す必要がありますか?

4

5 に答える 5

6

これは、Javaで日付を処理するための最良のライブラリであるJodaTimeを使用したソリューションです(前回チェックしました)。フォーマットは単純で、カスタムのDateFormatter実装を使用することで間違いなく改善できます。これはまた、年が同じであることをチェックしますが、年を出力しないため、混乱する可能性があります。

import org.joda.time.DateTime;

public class DateFormatterTest {

    public static void main(String[] args) {

        DateTime august23rd = new DateTime(2010, 8, 23, 0, 0, 0, 0);
        DateTime august25th = new DateTime(2010, 8, 25, 0, 0, 0, 0);
        DateTime september5th = new DateTime(2010, 9, 5, 0, 0, 0, 0);

        DateFormatterTest tester = new DateFormatterTest();
        tester.outputDate(august23rd, august25th);
        tester.outputDate(august23rd, september5th);

    }

    private void outputDate(DateTime firstDate, DateTime secondDate) {
        if ((firstDate.getMonthOfYear() == secondDate.getMonthOfYear()) && (firstDate.getYear() == secondDate.getYear())) {
            System.out.println(firstDate.getDayOfMonth() + " - " + secondDate.getDayOfMonth() + " " + firstDate.monthOfYear().getAsShortText());
        } else {
            System.out.println(firstDate.getDayOfMonth() + " " + firstDate.monthOfYear().getAsShortText() + " - " + secondDate.getDayOfMonth() + " " + secondDate.monthOfYear().getAsShortText());
        }
    }
}

出力:

8月23〜25日

8月23日-9月5日

于 2010-08-23T13:49:30.850 に答える
4

私は他の答えに失望した。Jodaの時間はGWTでは機能せず、SimpleDateFormatでも機能しません。とにかく、私はすでにGWTのDateTimeFormatについて知っていました。主な問題は、日付オブジェクトのgetMonth()関数が非推奨になり、月や年を比較する良い方法がないように思われることです。このソリューションには、年のチェック(monthFormatterを変更することで簡単に追加できます)は含まれていませんが、私の場合は重要ではありません。

public final class DateUtility
{
    public static final DateTimeFormat MONTH_FORMAT = DateTimeFormat.getFormat("MMM");
    public static final DateTimeFormat DAY_FORMAT = DateTimeFormat.getFormat("dd");
    public static final DateTimeFormat DAY_MONTH_FORMAT = DateTimeFormat.getFormat("dd MMM");

    public static final String DASH = " - ";

    /**
     * Returns a "collapsed" date range String representing the period of time
     * between two Date parameters. Example: August 19 as a {@code startDate}
     * and August 30 as an {@code endDate} will return "19 - 30 AUG", August 28
     * as a {@code startDate} and September 7 as an {@code endDate} will return
     * "28 AUG - 07 SEP". This means if you pass this two dates which cannot
     * collapse into a shorter form, then the longer form is returned.  Years
     * are ignored, and the start and end dates are not checked to ensure we
     * are not going backwards in time (Aug 10 - July 04 is not checked).
     * 
     * @param startDate
     *            the start Date in the range.
     * @param endDate
     *            the end Date in the range.
     * @return a String representation of the range between the two dates given.
     */
    public static String collapseDate(Date startDate, Date endDate) {
        String formattedDateRange;

        // Get a comparison result to determine if the Months are the same
        String startDateMonth = MONTH_FORMAT.format(startDate);
        String endDateMonth = MONTH_FORMAT.format(endDate);

        if (startDateMonth.equals(endDateMonth))
        {
            formattedDateRange = DAY_FORMAT.format(startDate) + DASH
                    + DAY_MONTH_FORMAT.format(endDate).toUpperCase();
        }
        else
        {
            // Months don't match, split the string across both months
            formattedDateRange = DAY_MONTH_FORMAT.format(startDate).toUpperCase()
                    + DASH + DAY_MONTH_FORMAT.format(endDate).toUpperCase();
        }
        return formattedDateRange;
    }
}
于 2010-09-30T13:44:00.797 に答える
3

javaのSimpleDateFormatクラスをチェックアウトします。日付パターンを文字列として渡すSimpleDateFormatを作成できます。

于 2010-08-23T13:34:39.227 に答える
1

Jeroenの回答に追加するには、SimpleDateFormatを範囲の両端に1回ずつ、合計2回使用します。

于 2010-08-23T13:37:04.463 に答える
0

デフォルトでは、Javaには日範囲などのタイプがないため、この場合の文字列表現にはDateFormatsは適用されません。

私の意見では、TimeSpan型またはTimeDiff型を作成し、メソッドを文字列にオーバーライドするのが最善です。または、2つも解析する必要がある場合は、DateFormmaterを作成することをお勧めします。

于 2010-08-23T13:39:59.957 に答える