我尝试使用假日和日期(两个字符串)创建一个ArrayList,然后将日期转换为日历,获取星期几(使用DAY_OF_WEEK)和/或按日期/名称排序,但由于某种原因我为列表中的每个项目得到相同的结果(日历)。
这是我的代码:
public static ArrayList listOfHolidays = new ArrayList(); Holidays.holidaysList.add(new Holidays("Sukkot", "09/10/2014")); Holidays.holidaysList.add(new Holidays("Hanukkah", "17/12/2014")); Holidays.holidaysList.add(new Holidays("Purim", "16/03/2014"));
假期课程:
Calendar calendar = Calendar.getInstance(); @Override public String toString() { //DayOfWeek is just an enum of days (strings) return holidayName + " falls on " + DayOfWeek.values()[Calendar.DAY_OF_WEEK - 1]; // Here i get the same day each time } @Override public int compareTo(Holidays another) { if(MainActivity.sortByName == true) { return holidayName.compareToIgnoreCase(another.holidayName); } return convertStringToCal() - another.convertStringToCal(); } private int convertStringToCal() { int year, month, day; day = Integer.valueOf(holidayDate.substring(0, 2)); month = Integer.valueOf(holidayDate.substring(3, 5)); year = Integer.valueOf(holidayDate.substring(6, 10)); calendar = Calendar.getInstance(); calendar.set(year, month, day); return (int) calendar.getTimeInMillis(); }
我在radioButton方法中调用
Collections.sort()进行排序。
答案 0 :(得分:1)
我看到为什么它可能无效的多种原因。
首先我不知道你是否省略了Holidays类的部分代码,但是你从未实际设置除convertStringToCal()方法之外的日历对象,但我不知道是否在构造函数中也调用了此方法
其次,很可能是你的问题:
@Override
public String toString() {
//DayOfWeek is just an enum of days (strings)
return holidayName + " falls on "
// Here you are using the constant Calendar.DAY_OF_WEEK
+ DayOfWeek.values()[Calendar.DAY_OF_WEEK - 1]; // Here i get the same day each time
}
它应该看起来像这样:
@Override
public String toString() {
//DayOfWeek is just an enum of days (strings)
return holidayName + " falls on "
+ DayOfWeek.values()[calendar.get(Calendar.DAY_OF_WEEK)]; // Now you won't get the same day every time. :)
}
一些补充说明:
我会使用Date
个对象来保存日期,并使用SimpleDateFormat
将字符串转换为日期,反之亦然。
如果您使用这两个,您的Holidays类看起来像这样:
public class Holidays {
private final Calendar calendar = Calendar.getInstance();
private final String holidayName;
public Holidays(String holidayName, Date date) {
this.holidayName = holidayName;
this.calendar.setTime(date);
}
@Override
public int compareTo(Holidays another) {
// Generally you should avoid passing information statically between Activities
if(MainActivity.sortByName == true) {
return holidayName.compareToIgnoreCase(another.holidayName);
}
return getTimeInMillis() - another.getTimeInMillis();
}
public long getTimeInMillis() {
return this.calendar.getTimeInMillis();
}
@Override
public String toString() {
//DayOfWeek is just an enum of days (strings)
DayOfWeek day = DayOfWeek.values()[calendar.get(Calendar.DAY_OF_WEEK)]; // Now you won't get the same day every time. :)
return String.format("%s falls of %s", this.holidayName, day);
}
}
您可以像这样创建Date
个对象:
// Create date objects with calendar.
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, 2014);
calendar.set(Calendar.MONTH, 3);
calendar.set(Calendar.DAY_OF_MONTH, 27);
Date date = calendar.getTime();
Date对象的构造函数(如新日期(年,月,日))已弃用,不应使用。始终使用Calendar实例创建日期对象。
使用SimpleDateFormat
,您可以转换String
和Date
这样的对象:
// The String pattern defines how date strings are parsed and formated
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
String dateString = "09/10/2014";
// Convert a String to Date
Date dateFromDateString = sdf.parse(dateString);
// Convert a Date to a String
String dateStringFromDate = sdf.format(dateFromDateString);
如果您需要的不仅仅是Date
和String
个对象的简单转换,您可以使用DateFormat
。这几乎就是Date String转换的Rolls Royce。它可以以更加通用的方式解析和格式化字符串,而不需要实际模式,它会自动考虑区域设置等等。
或者您可以使用JodaTime:)
答案 1 :(得分:0)
我认为问题在于在这部分代码中转换为int: return(int)calendar.getTimeInMillis();
calendar.getTimeInMillis()返回long,而转换为int应该会导致问题。