如何从 CalendarView 获取星期几
How to get day of the week from CalendarView
我的布局中有一个 CalendarView
,我已将日期更改侦听器添加到其中:
calendarView = (CalendarView)findViewById(R.id.calendarView);
calendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
@Override
public void onSelectedDayChange(CalendarView view, int year, int month, int dayOfMonth) {
Log.i("View.getDate", view.getDate() + "");
}
});
我可以访问 selected 日期的年月日。但是我如何获得星期几呢?我想要 CalendarView
.
中 select 的任何日期的信息
我将 view.getDate()
放入日志中,只知道它在 select 的每个日期都给我相同的输出(今天的值)。我错过了什么?
更新:
calendar.set(year, month, dayOfMonth);
正确设置日历值,而以下:
calendar.setTimeInMillis(view.getDate());
没有。不知道是什么原因。
使用 Android 日历 class。
Calendar selected = Calendar.getInstance();
selected.setTimeInMillis(view.getDate());
int dayOfWeek = selected.get(Calendar.DAY_OF_WEEK);
switch (dayOfWeek ) {
case Calendar.SUNDAY:
// ...
case Calendar.MONDAY:
// etc ...
}
这应该不难,例如,这是否回答了您的问题? Android: how to get the current day of the week (Monday, etc...) in the user's language?
尝试使用日历并将其设置为您从日历视图中获得的时间:
Calendar selected = Calendar.getInstance();
selected.setTimeInMillis(calendarView .getDate());
int dayOfWeek = selected.get(Calendar.DAY_OF_WEEK);
例如:
calendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
@Override
public void onSelectedDayChange(CalendarView view, int year, int month, int dayOfMonth) {
Calendar calendar = Calendar.getInstance();
calendar.set(year, month, dayOfMonth);
int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
}
});
public static int dayOfWeek(String date) {
try {
int day = 0;
SimpleDateFormat simpleDateformat = new SimpleDateFormat("yyyy-MM-dd");
Date now = simpleDateformat.parse(date);
Calendar calendar = Calendar.getInstance();
calendar.setTime(now);
day = calendar.get(Calendar.DAY_OF_WEEK);
return day;
} catch (Exception e) {
return 0;
}
}
那么你可以使用Calendar.MONDAY、Calendar.TUESDAY等
希望这对您有所帮助。
使用 Calender class 来自 java api.
calendarView = (CalendarView)findViewById(R.id.calendarView);
calendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
@Override
public void onSelectedDayChange(CalendarView view, int year, int month, int dayOfMonth) {
Log.i("View.getDate", view.getDate() + "");
/*
for month you can use following. like
0 for jan
1 for feb
*/
int newMonth;
if(month > 9){
newMonth= month-1;
}
else{
switch(month){
case (1|01):{
newMonth = 0;
}
break;
case (2|02):{
newMonth = 1;
}
break;
....
}
}
Calendar calendar = new GregorianCalendar(year, newMonth
, dayOfMonth);
int result = calendar.get(Calendar.DAY_OF_WEEK);
switch (result) {
case Calendar.MONDAY:
System.out.println("It's Monday !");
break;
}
}
});
希望对你有所帮助..
太晚了,但好吧,我遇到了同样的问题。我用这个解决了。
public static int dayOfWeek(int day, int month, int year) {
Calendar c = Calendar.getInstance();
c.set(year,month,day);
return c.get(Calendar.DAY_OF_WEEK);
}
此方法return int 中的星期几。如果这一天是星期天 return 1,如果是星期三 return 4.
我的布局中有一个 CalendarView
,我已将日期更改侦听器添加到其中:
calendarView = (CalendarView)findViewById(R.id.calendarView);
calendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
@Override
public void onSelectedDayChange(CalendarView view, int year, int month, int dayOfMonth) {
Log.i("View.getDate", view.getDate() + "");
}
});
我可以访问 selected 日期的年月日。但是我如何获得星期几呢?我想要 CalendarView
.
我将 view.getDate()
放入日志中,只知道它在 select 的每个日期都给我相同的输出(今天的值)。我错过了什么?
更新:
calendar.set(year, month, dayOfMonth);
正确设置日历值,而以下:
calendar.setTimeInMillis(view.getDate());
没有。不知道是什么原因。
使用 Android 日历 class。
Calendar selected = Calendar.getInstance();
selected.setTimeInMillis(view.getDate());
int dayOfWeek = selected.get(Calendar.DAY_OF_WEEK);
switch (dayOfWeek ) {
case Calendar.SUNDAY:
// ...
case Calendar.MONDAY:
// etc ...
}
这应该不难,例如,这是否回答了您的问题? Android: how to get the current day of the week (Monday, etc...) in the user's language?
尝试使用日历并将其设置为您从日历视图中获得的时间:
Calendar selected = Calendar.getInstance();
selected.setTimeInMillis(calendarView .getDate());
int dayOfWeek = selected.get(Calendar.DAY_OF_WEEK);
例如:
calendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
@Override
public void onSelectedDayChange(CalendarView view, int year, int month, int dayOfMonth) {
Calendar calendar = Calendar.getInstance();
calendar.set(year, month, dayOfMonth);
int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
}
});
public static int dayOfWeek(String date) {
try {
int day = 0;
SimpleDateFormat simpleDateformat = new SimpleDateFormat("yyyy-MM-dd");
Date now = simpleDateformat.parse(date);
Calendar calendar = Calendar.getInstance();
calendar.setTime(now);
day = calendar.get(Calendar.DAY_OF_WEEK);
return day;
} catch (Exception e) {
return 0;
}
}
那么你可以使用Calendar.MONDAY、Calendar.TUESDAY等
希望这对您有所帮助。
使用 Calender class 来自 java api.
calendarView = (CalendarView)findViewById(R.id.calendarView);
calendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
@Override
public void onSelectedDayChange(CalendarView view, int year, int month, int dayOfMonth) {
Log.i("View.getDate", view.getDate() + "");
/*
for month you can use following. like
0 for jan
1 for feb
*/
int newMonth;
if(month > 9){
newMonth= month-1;
}
else{
switch(month){
case (1|01):{
newMonth = 0;
}
break;
case (2|02):{
newMonth = 1;
}
break;
....
}
}
Calendar calendar = new GregorianCalendar(year, newMonth
, dayOfMonth);
int result = calendar.get(Calendar.DAY_OF_WEEK);
switch (result) {
case Calendar.MONDAY:
System.out.println("It's Monday !");
break;
}
}
});
希望对你有所帮助..
太晚了,但好吧,我遇到了同样的问题。我用这个解决了。
public static int dayOfWeek(int day, int month, int year) {
Calendar c = Calendar.getInstance();
c.set(year,month,day);
return c.get(Calendar.DAY_OF_WEEK);
}
此方法return int 中的星期几。如果这一天是星期天 return 1,如果是星期三 return 4.