我在本教程的帮助下制作了日历:http://droidwalk.blogspot.it/2012/11/android-calendar-sample.html ..但是这个日历从星期天开始,我想从星期一开始。。我试图改变价值观在函数中设置对象GregorianCalendar,但它不会给出所需的结果。本周从周一开始,但在某些月份,我们失去了第一周。我发现了一个类似的问题:从周一开始制作日历,但没有解决方案。如果有人有任何想法,请帮忙。
日历视图。JAVA
public GregorianCalendar month, itemmonth;// calendar instances.
public CalendarAdapter adapter;// adapter instance
public Handler handler;// for grabbing some event values for showing the dot
// marker.
public ArrayList<String> items; // container to store calendar items which
// needs showing the event marker
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.calendar);
Locale.setDefault( Locale.US );
month = (GregorianCalendar) GregorianCalendar.getInstance();
itemmonth = (GregorianCalendar) month.clone();
items = new ArrayList<String>();
adapter = new CalendarAdapter(this, month);
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(adapter);
handler = new Handler();
handler.post(calendarUpdater);
TextView title = (TextView) findViewById(R.id.title);
title.setText(android.text.format.DateFormat.format("MMMM yyyy", month));
RelativeLayout previous = (RelativeLayout) findViewById(R.id.previous);
previous.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
setPreviousMonth();
refreshCalendar();
}
});
RelativeLayout next = (RelativeLayout) findViewById(R.id.next);
next.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
setNextMonth();
refreshCalendar();
}
});
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
((CalendarAdapter) parent.getAdapter()).setSelected(v);
String selectedGridDate = CalendarAdapter.dayString
.get(position);
String[] separatedTime = selectedGridDate.split("-");
String gridvalueString = separatedTime[2].replaceFirst("^0*",
"");// taking last part of date. ie; 2 from 2012-12-02.
int gridvalue = Integer.parseInt(gridvalueString);
// navigate to next or previous month on clicking offdays.
if ((gridvalue > 10) && (position < 8)) {
setPreviousMonth();
refreshCalendar();
} else if ((gridvalue < 7) && (position > 28)) {
setNextMonth();
refreshCalendar();
}
((CalendarAdapter) parent.getAdapter()).setSelected(v);
if(selectedGridDate.equals("2013-06-20")) //mettergli confronto con date evento
setContentView(R.layout.activity_inizio);
}
});
}
protected void setNextMonth() {
if (month.get(GregorianCalendar.MONTH) == month
.getActualMaximum(GregorianCalendar.MONTH)) {
month.set((month.get(GregorianCalendar.YEAR) + 1),
month.getActualMinimum(GregorianCalendar.MONTH), 1);
} else {
month.set(GregorianCalendar.MONTH,
month.get(GregorianCalendar.MONTH) + 1);
}
}
protected void setPreviousMonth() {
if (month.get(GregorianCalendar.MONTH) == month
.getActualMinimum(GregorianCalendar.MONTH)) {
month.set((month.get(GregorianCalendar.YEAR) - 1),
month.getActualMaximum(GregorianCalendar.MONTH), 1);
} else {
month.set(GregorianCalendar.MONTH,
month.get(GregorianCalendar.MONTH) - 1);
}
}
protected void showToast(String string) {
Toast.makeText(this, string, Toast.LENGTH_SHORT).show();
}
public void refreshCalendar() {
TextView title = (TextView) findViewById(R.id.title);
adapter.refreshDays();
adapter.notifyDataSetChanged();
handler.post(calendarUpdater); // generate some calendar items
title.setText(android.text.format.DateFormat.format("MMMM yyyy", month));
}
public Runnable calendarUpdater = new Runnable() {
@Override
public void run() {
items.clear();
// Print dates of the current week
DateFormat df = new SimpleDateFormat("yyyy-MM-dd",Locale.US);
String itemvalue;
for (int i = 0; i < 7; i++) { //AL POSTO DI SETTE METTERE IL NUMERO DEGLI EVENTI
itemvalue = df.format(itemmonth.getTime());
itemmonth.add(GregorianCalendar.DATE, 1);
items.add("2013-06-21"); //METTERE DATE EVENTI
items.add("2012-10-07");
items.add("2012-10-15");
items.add("2012-10-20");
items.add("2012-11-30");
items.add("2012-11-28");
}
adapter.setItems(items);
adapter.notifyDataSetChanged();
}
};
}
日历适配器。JAVA
private Context mContext;
private java.util.Calendar month;
public GregorianCalendar pmonth; // Istanzia un calendario per il mese precedente
/**
* calendar instance for previous month for getting complete view
*/
public GregorianCalendar pmonthmaxset;
private GregorianCalendar selectedDate;
int firstDay;
int maxWeeknumber;
int maxP;
int calMaxP;
int lastWeekDay;
int leftDays;
int mnthlength;
String itemvalue, curentDateString;
DateFormat df;
private ArrayList<String> items;
public static List<String> dayString;
private View previousView;
public CalendarAdapter(Context c, GregorianCalendar monthCalendar) {
CalendarAdapter.dayString = new ArrayList<String>();
Locale.setDefault( Locale.US );
month = monthCalendar;
selectedDate = (GregorianCalendar) monthCalendar.clone();
mContext = c;
month.set(GregorianCalendar.DAY_OF_MONTH, 1);
this.items = new ArrayList<String>();
df = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
curentDateString = df.format(selectedDate.getTime());
refreshDays();
}
public void setItems(ArrayList<String> items) {
for (int i = 0; i != items.size(); i++) {
if (items.get(i).length() == 1) {
items.set(i, "0" + items.get(i));
}
}
this.items = items;
}
public int getCount() {
return dayString.size();
}
public Object getItem(int position) {
return dayString.get(position);
}
public long getItemId(int position) {
return 0;
}
// create a new view for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
TextView dayView;
if (convertView == null) { // if it's not recycled, initialize some
// attributes
LayoutInflater vi = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.calendar_item, null);
}
dayView = (TextView) v.findViewById(R.id.date);
// separates daystring into parts.
String[] separatedTime = dayString.get(position).split("-");
// taking last part of date. ie; 2 from 2012-12-02
String gridvalue = separatedTime[2].replaceFirst("^0*", "");
// checking whether the day is in current month or not.
if ((Integer.parseInt(gridvalue) > 1) && (position < firstDay)) {
// setting offdays to white color.
dayView.setTextColor(Color.WHITE);
dayView.setClickable(false);
dayView.setFocusable(false);
} else if ((Integer.parseInt(gridvalue) < 7) && (position > 28)) {
dayView.setTextColor(Color.WHITE);
dayView.setClickable(false);
dayView.setFocusable(false);
} else {
// setting curent month's days in blue color.
dayView.setTextColor(Color.BLUE);
}
if (dayString.get(position).equals(curentDateString)) {
setSelected(v);
previousView = v;
} else {
v.setBackgroundResource(R.drawable.list_item_background);
}
dayView.setText(gridvalue);
// create date string for comparison
String date = dayString.get(position);
if (date.length() == 1) {
date = "0" + date;
}
String monthStr = "" + (month.get(GregorianCalendar.MONTH) + 1);
if (monthStr.length() == 1) {
monthStr = "0" + monthStr;
}
// show icon if date is not empty and it exists in the items array
ImageView iw = (ImageView) v.findViewById(R.id.date_icon);
if (date.length() > 0 && items != null && items.contains(date)) {
iw.setVisibility(View.VISIBLE);
} else {
iw.setVisibility(View.INVISIBLE);
}
return v;
}
public View setSelected(View view) {
if (previousView != null) {
previousView.setBackgroundResource(R.drawable.list_item_background);
}
previousView = view;
view.setBackgroundResource(R.drawable.calendar_cel_selectl);
return view;
}
public void refreshDays() {
// clear items
items.clear();
dayString.clear();
Locale.setDefault( Locale.ITALY );
pmonth = (GregorianCalendar) month.clone();
// month start day. ie; sun, mon, etc
firstDay = month.get(GregorianCalendar.DAY_OF_WEEK);
// finding number of weeks in current month.
maxWeeknumber = month.getActualMaximum(GregorianCalendar.WEEK_OF_MONTH);
// allocating maximum row number for the gridview.
mnthlength = maxWeeknumber * 7;
maxP = getMaxP(); // previous month maximum day 31,30....
calMaxP = maxP - (firstDay - 1);// calendar offday starting 24,25 ...
/**
* Calendar instance for getting a complete gridview including the three
* month's (previous,current,next) dates.
*/
pmonthmaxset = (GregorianCalendar) pmonth.clone();
/**
* setting the start date as previous month's required date.
*/
pmonthmaxset.set(GregorianCalendar.DAY_OF_MONTH, calMaxP +1);
/**
* filling calendar gridview.
*/
for (int n = 0; n < mnthlength; n++) {
itemvalue = df.format(pmonthmaxset.getTime());
pmonthmaxset.add(GregorianCalendar.DATE, 1);
dayString.add(itemvalue);
}
}
private int getMaxP() {
int maxP;
if (month.get(GregorianCalendar.MONTH) == month
.getActualMinimum(GregorianCalendar.MONTH)) {
pmonth.set((month.get(GregorianCalendar.YEAR) - 1),
month.getActualMaximum(GregorianCalendar.MONTH), 1);
} else {
pmonth.set(GregorianCalendar.MONTH,
month.get(GregorianCalendar.MONTH) - 1);
}
maxP = pmonth.getActualMaximum(GregorianCalendar.DAY_OF_MONTH);
return maxP;
}
}
首先更改所有区域设置。把我们带到现场。因为美国日历从周日开始,英国日历从周一开始
在CalendarViewFragment中从周一开始日历
在setNextMonth()中更改
月getActualMinimum(Gregorianalendar.月),1);到下个月。getActualMinimum(Gregorianalendar.MONTH),日历。星期六);
和
在setPreviousMonth()中更改
月getActualMaximum(Gregorianalendar.月),1);到下个月。getActualMaximum(Gregorianalendar.MONTH),日历。星期六);
而在这场变革中
月集合(Gregorianalendar.DAY_OF u MONTH,1);到下个月。套装(公历,日历,日历,星期六);
解决方案:
在日历适配器中。爪哇:
更改为:
month.set(GregorianCalendar.DAY_OF_MONTH, 1);
到
month.set(GregorianCalendar.DAY_OF_MONTH, Calendar.SATURDAY);
在CalendarV中iew.java方法setPreviousMonth和setNextMonth:
来自:
month.getActualMaximum(GregorianCalendar.MONTH), 1);
month.getActualMinimum(GregorianCalendar.MONTH), 1);
致:
month.getActualMaximum(GregorianCalendar.MONTH), Calendar.SATURDAY);
month.getActualMinimum(GregorianCalendar.MONTH), Calendar.SATURDAY);
问题内容: 我有一个小问题,我正在开发一个应用程序,我需要将每周的开始日期从星期一更改为另一个(星期四,星期六)。在Android中这可能吗,我需要计算周的开始及其知道日期的结束。(例如,星期几从星期四开始) 注意:我只是android开发的初学者。这是我的代码SimpleDateFormat dateformate = new SimpleDateFormat(“ dd / MM”); 使用上面
我有一个小问题,我正在开发一个应用程序,我需要将一周的开始日从星期一更改为另一个(星期四,星期六)。这在android中可能吗,我需要计算一周的开始和结束知道日期。(例如,一周从星期四开始) 注意:我只是android开发的初学者。这是我的代码SimpleDataFormat DateFormat=new SimpleDataFormat(“dd/MM”); 使用上面的代码,我得到了结果,但星期一
我需要在我的dim_date中添加新的业务wee,实际上我有一个月中的星期,如下表所示,每7天计算一次。 但是对于这个商业周专栏,一周应该从星期一开始,到星期日结束,即使第一周只有一天(当一个月从星期日开始时),但如果月份从星期五开始,我应该将其视为该月的第一周。 你有什么建议来计算这个吗?谢谢
问题内容: 我在这个问题上看到了一些变体,但我相信这个问题尚未得到解答。 我需要获取一周的开始日期和结束日期,并由年份和星期数选择(而不是日期) 例: 输入: 输出: 返回值将类似于一个数组,其中第一个条目是星期开始日期,第二个条目是结束日期。 可选:当我们需要时,日期格式必须为(正常日期格式,无前导零。 我尝试编辑几乎可以完成我想要的功能,但是到目前为止我还没有运气。 请帮帮我,谢谢。 问题答案
也许我想多了。我想每周一上午9点发送本地通知,直到满足某些条件。我知道这可以通过设置的和组件来完成。我遇到的问题是我将传递给的值取决于正在使用的。如果我理解正确,则取决于用户的设置。例如,如果用户的日历周从周日开始,我会传递1到,但如果一周从周六开始,我会传递2。 我的问题是,我该如何解释时区、夏令时和周开始日之类的事情?
用户可以像这样在手机中设置他们的自定义开始日期。再加上不同的文化,一周开始的日子也不同: 澳大利亚,英国:周日,周一。。。,星期六 中国:周一,周二。。。,星期日 这意味着在开发日历功能时,我们可能有许多不同的一周中的日子从(从左到右)开始我们的日历,以使用户体验最佳。 我们可以定制日历,以确定一周中的具体开始日期: 问题在于将我的自定义日历更新为用户的系统日历。 当我使用Calender(从用户