下面列出了java.util.Calendar#getDisplayName ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static void main(String[] args) {
Calendar jcal = Calendar.getInstance(new Locale("ja", "JP", "JP"));
Calendar gcal = Calendar.getInstance(Locale.US);
for (int i = SUNDAY; i <= SATURDAY; i++) {
jcal.set(DAY_OF_WEEK, i);
gcal.set(DAY_OF_WEEK, i);
// Test LONG
String j = jcal.getDisplayName(DAY_OF_WEEK, LONG, Locale.US);
String g = gcal.getDisplayName(DAY_OF_WEEK, LONG, Locale.US);
if (!j.equals(g)) {
throw new RuntimeException("Got " + j + ", expected " + g);
}
// Test SHORT
j = jcal.getDisplayName(DAY_OF_WEEK, SHORT, Locale.US);
g = gcal.getDisplayName(DAY_OF_WEEK, SHORT, Locale.US);
if (!j.equals(g)) {
throw new RuntimeException("Got " + j + ", expected " + g);
}
}
}
public static void main(String[] args) {
Calendar jcal = Calendar.getInstance(new Locale("ja", "JP", "JP"));
Calendar gcal = Calendar.getInstance(Locale.US);
for (int i = SUNDAY; i <= SATURDAY; i++) {
jcal.set(DAY_OF_WEEK, i);
gcal.set(DAY_OF_WEEK, i);
// Test LONG
String j = jcal.getDisplayName(DAY_OF_WEEK, LONG, Locale.US);
String g = gcal.getDisplayName(DAY_OF_WEEK, LONG, Locale.US);
if (!j.equals(g)) {
throw new RuntimeException("Got " + j + ", expected " + g);
}
// Test SHORT
j = jcal.getDisplayName(DAY_OF_WEEK, SHORT, Locale.US);
g = gcal.getDisplayName(DAY_OF_WEEK, SHORT, Locale.US);
if (!j.equals(g)) {
throw new RuntimeException("Got " + j + ", expected " + g);
}
}
}
public static String stringTimeFromLong(@NonNull Context context, long timeStamp, boolean withHoursAndMinutes) {
final DateTime now = DateTime.now(); //immutable
//time will be before now. so this is checking if event occurred today
final DateTime dateTime = new DateTime(timeStamp);
Calendar c = Calendar.getInstance();
c.setTime(dateTime.toDate());
if (dateTime.isAfter(now.withMillisOfDay(0))) {
return withHoursAndMinutes ? S.numberToAlarmString(dateTime.getHourOfDay(), dateTime.getMinuteOfHour())
:
context.getString(R.string.today)
;
} else if (dateTime.isAfter(now.withMillisOfDay(0).minusDays(1))) {
return withHoursAndMinutes ?
String.format("%s %s", context.getString(R.string.yesterday), S.numberToAlarmString(dateTime.getHourOfDay(), dateTime.getMinuteOfHour()))
:
context.getString(R.string.yesterday);
} else if (dateTime.isAfter(now.withMillisOfDay(0).withDayOfWeek(1))) {
return c.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.LONG, Locale.getDefault());
} else {
return new SimpleDateFormat("dd/MM/yyyy", Locale.getDefault()).format(timeStamp);
}
}
public static String getFriendlyDate(Date date, boolean relative) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
Calendar now = Calendar.getInstance();
if (cal.get(Calendar.YEAR) == now.get(Calendar.YEAR)) {
if (cal.get(Calendar.DAY_OF_YEAR) == now.get(Calendar.DAY_OF_YEAR)) {
return TvApp.getApplication().getString(R.string.lbl_today);
}
if (cal.get(Calendar.DAY_OF_YEAR) == now.get(Calendar.DAY_OF_YEAR) + 1) {
return TvApp.getApplication().getString(R.string.lbl_tomorrow);
}
if (cal.get(Calendar.DAY_OF_YEAR) < now.get(Calendar.DAY_OF_YEAR) + 7 && cal.get(Calendar.DAY_OF_YEAR) > now.get(Calendar.DAY_OF_YEAR)) {
return cal.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.LONG, Locale.getDefault());
}
if (relative) {
return TvApp.getApplication().getString(R.string.lbl_in_x_days, cal.get(Calendar.DAY_OF_YEAR) - now.get(Calendar.DAY_OF_YEAR));
}
}
return DateFormat.getDateFormat(TvApp.getApplication()).format(date);
}
public static void main(String[] args) {
Calendar jcal = Calendar.getInstance(new Locale("ja", "JP", "JP"));
Calendar gcal = Calendar.getInstance(Locale.US);
for (int i = SUNDAY; i <= SATURDAY; i++) {
jcal.set(DAY_OF_WEEK, i);
gcal.set(DAY_OF_WEEK, i);
// Test LONG
String j = jcal.getDisplayName(DAY_OF_WEEK, LONG, Locale.US);
String g = gcal.getDisplayName(DAY_OF_WEEK, LONG, Locale.US);
if (!j.equals(g)) {
throw new RuntimeException("Got " + j + ", expected " + g);
}
// Test SHORT
j = jcal.getDisplayName(DAY_OF_WEEK, SHORT, Locale.US);
g = gcal.getDisplayName(DAY_OF_WEEK, SHORT, Locale.US);
if (!j.equals(g)) {
throw new RuntimeException("Got " + j + ", expected " + g);
}
}
}
public static void main(String[] args) {
Calendar jcal = Calendar.getInstance(new Locale("ja", "JP", "JP"));
Calendar gcal = Calendar.getInstance(Locale.US);
for (int i = SUNDAY; i <= SATURDAY; i++) {
jcal.set(DAY_OF_WEEK, i);
gcal.set(DAY_OF_WEEK, i);
// Test LONG
String j = jcal.getDisplayName(DAY_OF_WEEK, LONG, Locale.US);
String g = gcal.getDisplayName(DAY_OF_WEEK, LONG, Locale.US);
if (!j.equals(g)) {
throw new RuntimeException("Got " + j + ", expected " + g);
}
// Test SHORT
j = jcal.getDisplayName(DAY_OF_WEEK, SHORT, Locale.US);
g = gcal.getDisplayName(DAY_OF_WEEK, SHORT, Locale.US);
if (!j.equals(g)) {
throw new RuntimeException("Got " + j + ", expected " + g);
}
}
}
@Override
public void onDateChanged(int oldDay, int oldMonth, int oldYear, int newDay, int newMonth, int newYear) {
if(mDaySelectMode)
mYearPicker.setYear(newYear);
if(newDay < 0 || newMonth < 0 || newYear < 0){
mWeekDay = null;
mMonth = null;
mDay = null;
mYear = null;
}
else {
Calendar cal = mDatePicker.getCalendar();
cal.set(Calendar.YEAR, newYear);
cal.set(Calendar.MONTH, newMonth);
cal.set(Calendar.DAY_OF_MONTH, newDay);
mWeekDay = cal.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.LONG, Locale.getDefault());
mMonth = cal.getDisplayName(Calendar.MONTH, Calendar.SHORT, Locale.getDefault());
mDay = String.format(DAY_FORMAT, newDay);
mYear = String.format(YEAR_FORMAT, newYear);
if(oldMonth != newMonth || oldYear != newYear)
mDatePicker.goTo(newMonth, newYear);
}
mLocationDirty = true;
invalidate(0, 0, mHeaderRealWidth, mHeaderPrimaryRealHeight + mHeaderSecondaryHeight);
if(mOnDateChangedListener != null)
mOnDateChangedListener.onDateChanged(oldDay, oldMonth, oldYear, newDay, newMonth, newYear);
}
public static String getCurrentDateTimeName(int field) {
Calendar calendar = Calendar.getInstance();
if (field == Calendar.YEAR) {
return String.valueOf(calendar.get(Calendar.YEAR));
} else {
return calendar.getDisplayName(field, Calendar.SHORT, Locale.getDefault());
}
}
private TimeOfWeek getCurrentTimeOfWeek() {
Calendar rightNow = Calendar.getInstance();
rightNow.setTimeZone(TimeZone.getTimeZone("America/New_York"));
String dayOfWeek = rightNow.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.LONG, Locale.getDefault());
TimeOfWeek timeOfWeek = TimeOfWeek.Weekday;
if (dayOfWeek.equals("Saturday") || dayOfWeek.equals("Sunday"))
timeOfWeek = TimeOfWeek.Weekend;
else if (dayOfWeek.equals("Friday")) timeOfWeek = TimeOfWeek.Friday;
return timeOfWeek;
}
private static String getWeekName(Calendar calendar) {
String name = calendar.getDisplayName(7, 1, Locale.CHINESE);
if (TextUtils.isEmpty(name)) {
return "";
}
return name.replace(LetvUtils.getString(R.string.week_xinqi), LetvUtils.getString(R.string.week_zhou));
}
public static String getArabicCalender(int year, int mounth, int day) {
GregorianCalendar gCal = new GregorianCalendar(year, mounth, day);
Locale ar = new Locale("ar");
Calendar uCal = new UmmalquraCalendar(ar);
uCal.setTime(gCal.getTime()); // Used to properly format 'yy' pattern
uCal.get(Calendar.YEAR); // 1435
uCal.getDisplayName(Calendar.MONTH, Calendar.LONG, ar); // رجب
uCal.get(Calendar.DAY_OF_MONTH);
String time = uCal.get(Calendar.YEAR) + "/" + uCal.getDisplayName(Calendar.MONTH, Calendar.LONG, ar) + "/" + uCal.get(Calendar.DAY_OF_MONTH);
return isLanguageArabic ? convertToUnicodeFarsiNumber(time) : time;
}
private static String getMonthAndYearString(MonthAdapter.CalendarDay day, Locale locale) {
Calendar cal = Calendar.getInstance();
cal.set(day.year, day.month, day.day);
String sbuf = "";
sbuf += cal.getDisplayName(Calendar.MONTH, Calendar.LONG, locale);
sbuf += " ";
sbuf += YEAR_FORMAT.format(cal.getTime());
return sbuf;
}
private static String getMonthAndYearString(MonthAdapter.CalendarDay day) {
Calendar cal = Calendar.getInstance();
cal.set(day.year, day.month, day.day);
String sbuf = "";
sbuf += cal.getDisplayName(Calendar.MONTH, Calendar.LONG, Locale.getDefault());
sbuf += " ";
sbuf += YEAR_FORMAT.format(cal.getTime());
return sbuf;
}
private static String getMonthAndYearString(MonthAdapter.CalendarDay day) {
Calendar cal = Calendar.getInstance();
cal.set(day.year, day.month, day.day);
String sbuf = "";
sbuf += cal.getDisplayName(Calendar.MONTH, Calendar.LONG, Locale.getDefault());
sbuf += " ";
sbuf += YEAR_FORMAT.format(cal.getTime());
return sbuf;
}
public static String getDayOfWeek(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
return cal.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.LONG, Locale.getDefault());
}
@NbBundle.Messages({
"# {0} - date from", "# {1} - date until", "CTL_SpecificDates={0} - {1}"
})
private static String toDisplayable (IssueScheduleInfo info) {
Date date = info.getDate();
int interval = info.getInterval();
Calendar cal = Calendar.getInstance();
stripTime(cal);
Calendar scheduleDay = Calendar.getInstance();
scheduleDay.setTime(date);
stripTime(scheduleDay);
if (interval <= 1) {
String dayName = scheduleDay.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.LONG, Locale.getDefault());
for (int i = 0; i < 7; ++i) {
if (cal.getTime().equals(scheduleDay.getTime())) {
if (i == 0) {
dayName = Bundle.CTL_Today(dayName);
}
return dayName;
}
cal.add(Calendar.DATE, 1);
}
return DATE_FORMAT.format(date);
} else {
scheduleDay.setTime(date);
stripTime(scheduleDay);
Calendar until = Calendar.getInstance();
until.setTime(date);
stripTime(until);
until.add(Calendar.DATE, interval);
if (isThisWeek(scheduleDay, until)) {
return Bundle.CTL_ThisWeek();
} else if (isNextWeek(scheduleDay, until)) {
return Bundle.CTL_NextWeek();
} else {
return Bundle.CTL_SpecificDates(DATE_FORMAT.format(scheduleDay.getTime()),
DATE_FORMAT.format(until.getTime()));
}
}
}
public static String convertEnglishMonthNameToArabic(int month) {
Calendar cal = new UmmalquraCalendar();
return cal.getDisplayName(Calendar.MONTH, Calendar.SHORT, new Locale("ar"));
}
private static String getArabicStringDay(int dayOfWeek) {
Calendar cal = new UmmalquraCalendar();
return cal.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.LONG, new Locale("ar"));
}
public String getDay(long dt) {
dt *= 1000;
Calendar c = Calendar.getInstance();
c.setTime(new Date(dt));
return c.getDisplayName(Calendar.DAY_OF_WEEK , Calendar.SHORT , new Locale(new Prefs(getActivity()).getLanguage()));
}
private void updateRecurringData(){
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(mRecurring.getStartTime());
int order = Recurring.getWeekDayOrderNum(cal);
String dayOfWeek = cal.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.LONG, Locale.getDefault());
int formattedTextId = MONTH_SAME_WEEKDAY[(order + 1) % MONTH_SAME_WEEKDAY.length];
mSameWeekdayRadioButton.setText(getContext().getResources().getString(formattedTextId, dayOfWeek));
mPeriodEditText.setText(String.valueOf(mRecurring.getPeriod()));
if(mRecurring.getRepeatMode() == Recurring.REPEAT_WEEKLY) {
for(int i = Calendar.SUNDAY; i <= Calendar.SATURDAY; i++)
mWeekView.setSelected(i, mRecurring.isEnabledWeekday(i), true);
}
else{
int day = cal.get(Calendar.DAY_OF_WEEK);
for(int i = Calendar.SUNDAY; i <= Calendar.SATURDAY; i++)
mWeekView.setSelected(i, i == day, true);
if(mRecurring.getRepeatMode() == Recurring.REPEAT_MONTHLY){
mSameDayRadioButton.setCheckedImmediately(mRecurring.getMonthRepeatType() == Recurring.MONTH_SAME_DAY);
mSameWeekdayRadioButton.setCheckedImmediately(mRecurring.getMonthRepeatType() == Recurring.MONTH_SAME_WEEKDAY);
}
else{
mSameDayRadioButton.setCheckedImmediately(true);
mSameWeekdayRadioButton.setCheckedImmediately(false);
}
}
if(mModeSpinner.getSelectedItemPosition() != mRecurring.getRepeatMode())
mModeSpinner.setSelection(mRecurring.getRepeatMode());
else
onModeSelected(mRecurring.getRepeatMode());
mEndNumEditText.setText(String.valueOf(mRecurring.getEndMode() == Recurring.END_FOR_EVENT ? mRecurring.getEventNumber() : 10));
long date = mRecurring.getEndMode() == Recurring.END_UNTIL_DATE ? mRecurring.getEndDate() : (Math.max(System.currentTimeMillis(), mRecurring.getStartTime()) + 86400000L * 31);
mEndDateButton.setText(mDateFormat.format(new Date(date)));
mEndDateButton.setTag(date);
if(mEndSpinner.getSelectedItemPosition() != mRecurring.getEndMode())
mEndSpinner.setSelection(mRecurring.getEndMode());
else
onEndSelected(mRecurring.getEndMode());
}