标题说明了一切,我需要能够以月/日的形式输入用户输入的开始日期和结束日期,并能够计算两者之间的周数。我想使用JList来解决这个问题,但似乎JList只适用于字符串。
我将使用什么Swing功能,感谢任何帮助,谢谢!
答案 0 :(得分: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;
...