为什么模拟器和手机中的显示效果不一样

时间:2016-03-05 22:11:12

标签: java android date

String[] datesReason = {
            "New Years Day",
            "Martin Luther King, Jr.'s Birthday",
            "Asian Lunar New Year"
};
public String[] suspendedDates = {
            "Friday, January 1",
            "Monday, January 18",
            "Saturday, March 5"
};

DateFormat df = new SimpleDateFormat("EEEEE, LLLL d", Locale.US);

Spannable WordToSpan = new SpannableString("Today is: ");
WordToSpan.setSpan(new ForegroundColorSpan(R.color.dateholiday), 0, WordToSpan.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
tvTD.setText(WordToSpan);

Spannable WordToSpan1 = new SpannableString("" + df.format(new Date()) + "");
WordToSpan1.setSpan(new ForegroundColorSpan(Color.parseColor("#4787ED")), 0, WordToSpan1.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
tvTD.append(WordToSpan1);

当我从PC调试到Android模拟器时,上面的代码显示Today is: Saturday, March 5。当我使用Android设备调试(运行Lollipop,Jellybean等)时,它会显示Today is: S, March 5

我也使用上面的内容来比较当天做其他功能的日期,我有以下内容,将日期与今天进行比较:

SetRows myImage = data.get(position); //SetRows is what I am using to populate a ListView with texts/images
int inReason = myImage.name.indexOf(",");
String strR = myImage.name.substring(0, inReason);
Spannable WordToSpan = new SpannableString(strR); //e.g. strR = "Saturday"
WordToSpan.setSpan(new ForegroundColorSpan(Color.parseColor("#4787ED")), 0, WordToSpan.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
String strRNext = myImage.name.substring(inReason, myImage.name.length());
Spannable WordToSpan1 = new SpannableString(strRNext); //e.g. strRNext = "March 5"
WordToSpan1.setSpan(new ForegroundColorSpan(R.color.dateholiday), 0, WordToSpan1.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

if (myImage.name.contains(df.format(Calendar.getInstance(Locale.US).getTime()))) { //if Today is Saturday, March 5, the set the background of the list item...
   Toast.makeText(getContext(), String.valueOf(myImage.name.contains(df.format(Calendar.getInstance(Locale.US).getTime()))), 2000).show();
   row.setBackgroundColor(Color.parseColor("#F0DEEDF3"));
}
else { //otherwise use the default background...
   row.setBackgroundColor(Color.parseColor("#CCF5F5F5"));
}

在模拟器中,正确的列表项将获得背景,但在手机中,它不会,因为它显示S而不是Saturday

知道为什么会这样,我该如何解决呢。

1 个答案:

答案 0 :(得分:4)

来自documentation

  每周的E日(文字)E / EE / EEE:周二,EEEE:周二,EEEEE:T

您正在使用" EEEEE"这将只显示第一个字母,因此您需要删除一个E' E'如果你想展示一整天。