0

タイトルはすべてを物語っています。ユーザーが開始日と終了日を月/日の形式で入力できるようにし、その間の週数を計算できるようにする必要があります。これを解決するために JList を使用したかったのですが、JList は文字列でのみ機能するようです。

どの Swing 関数を使用しますか?

4

1 に答える 1

1

あなたの質問はSwingについて言及していません。

目的に応じてJodaTimeを使用する

import org.joda.time.LocalDateTime;
import org.joda.time.Period;
import org.joda.time.PeriodType;

....
    public static Map<Integer, String> getDateTimeDiffMap(String dateA, String dateB) {

    Calendar cal = Calendar.getInstance();
    Map<Integer,String> out = new LinkedHashMap<Integer, String>();

    long timeInMillA = 0;
    long timeInMillB = 0;

    SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT); 

    Date convertedDateA;
    Date convertedDateB;

    try {
    convertedDateA = dateFormat.parse(dateA);           
    cal.setTime(convertedDateA);
    timeInMillA = cal.getTimeInMillis();


    convertedDateB = dateFormat.parse(dateB);           
    cal.setTime(convertedDateB);
    timeInMillB = cal.getTimeInMillis();

} catch (ParseException e) {
    e.printStackTrace();
} 


    LocalDateTime startA = new LocalDateTime(timeInMillA);
    LocalDateTime startB = new LocalDateTime(timeInMillB);

    Period difference = new Period(startA, startB, PeriodType.days());
    int day = difference.getDays();

    difference = new Period(startA, startB, PeriodType.months());
    int month = difference.getMonths();

    difference = new Period(startA, startB, PeriodType.years());
    int year = difference.getYears();

    difference = new Period(startA, startB, PeriodType.weeks());
    int week = difference.getWeeks();

    difference = new Period(startA, startB, PeriodType.hours());
    int hour = difference.getHours();

    difference = new Period(startA, startB, PeriodType.minutes());
    long min = difference.getMinutes();

    difference = new Period(startA, startB, PeriodType.seconds());
    long sec = difference.getSeconds();

    //difference = new Period(startA, startB, PeriodType.millis());
    long mili = timeInMillB - timeInMillA;  


    out.put(7, mili + "");
    out.put(6, sec + "");
    out.put(5, min + "");
    out.put(4, hour + "");
    out.put(3, day + "");
    out.put(2, week + "");
    out.put(1, month + "");
    out.put(0, year + "");      

    return out;
}

たとえば、「01-09-2012 20:9:01」、「01-10-2012 20:9:01」の場合、次の出力が得られます。

year=0;
month = 1;
day=30;
hour=720;
...
于 2013-08-02T21:39:10.673 に答える