📄 weekdaymapper.java
字号:
/*--------------------------------------------------------------------------*
| Copyright (C) 2006 Christopher Kohlhaas, Bettina Lademann |
| |
| This program is free software; you can redistribute it and/or modify |
| it under the terms of the GNU General Public License as published by the |
| Free Software Foundation. A copy of the license has been included with |
| these distribution in the COPYING file, if not go to www.fsf.org |
| |
| As a special exception, you are granted the permissions to link this |
| program with every library, which license fulfills the Open Source |
| Definition as published by the Open Source Initiative (OSI). |
*--------------------------------------------------------------------------*/
package org.rapla.components.calendarview;
import java.util.Calendar;
import java.util.Locale;
import java.text.SimpleDateFormat;
/** maps weekday names to Calendar.DAY_OF_WEEK.
Example:
<pre>
WeekdayMapper mapper = new WeekdayMapper();
// print name of Sunday
System.out.println(mapper.getName(Calendar.SUNDAY));
// Create a weekday ComboBox
JComboBox comboBox = new JComboBox();
comboBox.setModel(new DefaultComboBoxModel(mapper.getNames()));
// select sunday
comboBox.setSelectedIndex(mapper.getIndexForDay(Calendar.SUNDAY));
// weekday == Calendar.SUNDAY
int weekday = mapper.getDayForIndex(comboBox.getSelectedIndex());
</pre>
*/
public class WeekdayMapper {
String[] weekdayNames;
int[] weekday2index;
int[] index2weekday;
public WeekdayMapper() {
this(Locale.getDefault());
}
public WeekdayMapper(Locale locale) {
weekdayNames = new String[7];
weekday2index = new int[8];
index2weekday = new int[8];
SimpleDateFormat format = new SimpleDateFormat("EEEEEE",locale);
Calendar calendar = Calendar.getInstance(locale);
calendar.set(Calendar.DAY_OF_WEEK, calendar.getFirstDayOfWeek());
for (int i=0;i<7;i++) {
weekday2index[calendar.get(Calendar.DAY_OF_WEEK)] = i;
index2weekday[i] = calendar.get(Calendar.DAY_OF_WEEK);
weekdayNames[i] = format.format(calendar.getTime());
calendar.add(Calendar.DATE,1);
}
}
public String[] getNames() {
return weekdayNames;
}
public String getName(int weekday) {
return getNames()[indexForDay(weekday)];
}
public int dayForIndex(int index) {
return index2weekday[index];
}
public int indexForDay(int weekday) {
return weekday2index[weekday];
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -