这是我下面的日历代码工作正常,但唯一的问题是不是显示日期在每月的第一天之前看到此http://imgur.com/inl1GzE并且在月的最后一天之后完整的源代码在这里https://www.zeta-uploader.com/1699876023
i want to do something like this
Sun Mon Tues Wed Thur Fri Sat
29 30 31 1 2 3 4
public class HoyahCalendar extends Activity {
public static int mYear;
public static int currentIndex = -1;
public static int mMonth;
public static int mDay;
public static String[][] a = new String[6][7];
Calendar mCalendar = Calendar.getInstance();
mYear = mCalendar.get(Calendar.YEAR);
mMonth = mCalendar.get(Calendar.MONTH) + 1;
mDay = mCalendar.get(Calendar.DAY_OF_MONTH);
last_week.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (mMonth == 1) {
mYear -= 1;
mMonth = 12;
new ShowCalendar(mYear, mMonth, mDay, "last");
showOnScreen();
} else {
// mMonth -= 1;
new ShowCalendar(mYear, mMonth, mDay, "last");
showOnScreen();
}
}
});
next_week.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (mMonth == 12) {
mYear += 1;
mMonth = 1;
new ShowCalendar(mYear, mMonth, mDay, "next");
showOnScreen();
} else {
if (HoyahCalendar.currentIndex == 4) {
HoyahCalendar.currentIndex = 4;
// mMonth += 1;
}
new ShowCalendar(mYear, mMonth, mDay, "next");
showOnScreen();
}
}
});
new ShowCalendar(mYear, mMonth);
showOnScreen();
public class ShowCalendar {
int mYear;
int mMonth;
int mDay;
public ShowCalendar(int mYear, int mMonth){
this.mYear = mYear;
this.mMonth = mMonth;
calculateMonthFirstday();
}
public ShowCalendar(int mYear, int mMonth, int mDay){
this.mYear = mYear;
this.mMonth = mMonth;
HoyahCalendar.currentIndex = 0;
this.mDay = mDay;
calculateMonthFirstday();
}
public int getmDay() {
return mDay;
}
public void setmDay(int mDay) {
this.mDay = mDay;
}
public ShowCalendar(int mYear, int mMonth, int mDay, String time){
this.mYear = mYear;
this.mMonth = mMonth;
if (time == "next"){
HoyahCalendar.currentIndex++;
if (HoyahCalendar.currentIndex == 5){
HoyahCalendar.currentIndex--;
}
this.mDay = mDay + 7;
} else if (time == "last"){
HoyahCalendar.currentIndex--;
if (HoyahCalendar.currentIndex == -1){
HoyahCalendar.currentIndex++;
}
this.mDay = mDay - 7;
}
calculateMonthFirstday();
}
public void calculateMonthFirstday(){
int month, first_day=0;
if((mYear%4==0 && mYear%100!=0)||(mYear%400==0))
month=1;
else
month=0;
int y, y12, c, c12, m, d;
y = mYear%100;
y12 = (mYear-1)%100; //only for January and February
c = mYear/100;
c12 = (mYear-1)/100;
m = mMonth;
d = 1;
switch(mMonth){
case 1: {first_day = y12 + y12/4 +c12/4 - 2*c12 + 26*(13 + 1)/10 + d - 1;break;}
case 2: {first_day = y12 + y12/4 +c12/4 - 2*c12 + 26*(14 + 1)/10 + d - 1;break;}
case 3: {first_day = y + y/4 +c/4 - 2*c + 26*(m + 1)/10 + d - 1;break;}
case 4: {first_day = y + y/4 +c/4 - 2*c + 26*(m + 1)/10 + d - 1;break;}
case 5: {first_day = y + y/4 +c/4 - 2*c + 26*(m + 1)/10 + d - 1;break;}
case 6: {first_day = y + y/4 +c/4 - 2*c + 26*(m + 1)/10 + d - 1;break;}
case 7: {first_day = y + y/4 +c/4 - 2*c + 26*(m + 1)/10 + d - 1;break;}
case 8: {first_day = y + y/4 +c/4 - 2*c + 26*(m + 1)/10 + d - 1;break;}
case 9: {first_day = y + y/4 +c/4 - 2*c + 26*(m + 1)/10 + d - 1;break;}
case 10: {first_day = y + y/4 +c/4 - 2*c + 26*(m + 1)/10 + d - 1;break;}
case 11: {first_day = y + y/4 +c/4 - 2*c + 26*(m + 1)/10 + d - 1;break;}
case 12: {first_day = y + y/4 +c/4 - 2*c + 26*(m + 1)/10 + d - 1;break;}
}
if(first_day<0)
first_day = 7 - (Math.abs(first_day))%7;//first_day每月第一天星期几
else
first_day = first_day%7;
switch(mMonth){
case 1: {CalculateCalendar(1,first_day,31);break;}
case 2: {CalculateCalendar(2,first_day,28+month);break;}
case 3: {CalculateCalendar(3,first_day,31);break;}
case 4: {CalculateCalendar(4,first_day,30);break;}
case 5: {CalculateCalendar(5,first_day,31);break;}
case 6: {CalculateCalendar(6,first_day,30);break;}
case 7: {CalculateCalendar(7,first_day,31);break;}
case 8: {CalculateCalendar(8,first_day,31);break;}
case 9: {CalculateCalendar(9,first_day,30);break;}
case 10:{CalculateCalendar(10,first_day,31);break;}
case 11:{CalculateCalendar(11,first_day,30);break;}
case 12:{CalculateCalendar(12,first_day,31);break;}
}
}
public void CalculateCalendar(int month_no, int week_no, int month_days){
int i, s = 0;
int currentDay;
if (this.mDay == 0){
mDay = 1;
currentDay= HoyahCalendar.mDay;
}else {
currentDay = this.mDay;
}
//String[][] a = new String[6][7];
for (i=0;i<week_no;i++)
HoyahCalendar.a[i/7][i%7] = "";
for(i=week_no; i<week_no + month_days; i++){
s = i - week_no + 1;
HoyahCalendar.a[i/7][i%7] = String.valueOf(s);
if (s == currentDay && HoyahCalendar.currentIndex == -1){
HoyahCalendar.currentIndex = i/7;
}
}
for (i=0; i<7;i++){
if (HoyahCalendar.a[HoyahCalendar.currentIndex][i] == null){
HoyahCalendar.a[0][i] = "";
}else{
HoyahCalendar.a[0][i] =
HoyahCalendar.a[HoyahCalendar.currentIndex][i];
}
}
for(i=week_no+month_days; i<42; i++)
HoyahCalendar.a[i/7][i%7] = "";
}
}
答案 0 :(得分:0)
这是您的问题的解决方案。 首先,您需要一个不同的函数设计,将月历计算为6x7的数组 所以使用这个以年和月为参数的函数,然后它将生成一个准确的月历日数。
/**
*
* @param year the year
* @param month January=0, Feb = 1, ...., Nov = 10, and Dec = 11, you know what I mean
* @return
*/
private static int[][] getArrayCalendar(int year,int month)
{
//VERY IMPORTANT
//SUN = 1, MON = 2, TU = 3, ..., Fri = 6, Sat = 7 according to USA
int[][] a = new int[6][7];
int day = 1; //day must be always one
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, month);
cal.set(Calendar.DAY_OF_MONTH, day);
int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
int goBack = 1 - dayOfWeek;
cal.add(Calendar.DAY_OF_MONTH, goBack);
System.out.println("SU\tMO\tTU\tWE\tTH\tFR\tSA");
for(int i=0;i<a.length;i++)
{
for(int j=0;j<a[0].length;j++)
{
a[i][j] = cal.get(Calendar.DAY_OF_MONTH);
System.out.print(cal.get(Calendar.DAY_OF_MONTH));
System.out.print("\t");
cal.add(Calendar.DAY_OF_MONTH, 1);
}
System.out.println();
}
return a;
}
实施例: getArrayCalendar(2013,4)// May = 4 ,年= 2013 打印结果:
SU MO TU WE TH FR SA
28 29 30 1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31 1
2 3 4 5 6 7 8
- 现在快速解决问题: 修改您的ShowCalendar类如下:
package com.example.calender5example;
import java.util.Calendar;
public class ShowCalendar
{
int mYear;
int mMonth;
int mDay;
public ShowCalendar(int mYear, int mMonth)
{
this.mYear = mYear;
this.mMonth = mMonth;
calculateMonthFirstday();
}
public ShowCalendar(int mYear, int mMonth, int mDay)
{
this.mYear = mYear;
this.mMonth = mMonth;
HoyahCalendar.currentIndex = 0;
this.mDay = mDay;
calculateMonthFirstday();
}
public int getmDay()
{
return mDay;
}
public void setmDay(int mDay)
{
this.mDay = mDay;
}
public ShowCalendar(int mYear, int mMonth, int mDay, String time)
{
this.mYear = mYear;
this.mMonth = mMonth;
if (time == "next")
{
HoyahCalendar.currentIndex++;
if (HoyahCalendar.currentIndex == 5)
{
HoyahCalendar.currentIndex--;
}
this.mDay = mDay + 7;
}
else if (time == "last")
{
HoyahCalendar.currentIndex--;
if (HoyahCalendar.currentIndex == -1)
{
HoyahCalendar.currentIndex++;
}
this.mDay = mDay - 7;
}
calculateMonthFirstday();
}
public void calculateMonthFirstday()
{
int month, first_day = 0;
if ((mYear % 4 == 0 && mYear % 100 != 0) || (mYear % 400 == 0))
month = 1;
else
month = 0;
int y, y12, c, c12, m, d;
y = mYear % 100;
y12 = (mYear - 1) % 100; // only for January and February
c = mYear / 100;
c12 = (mYear - 1) / 100;
m = mMonth;
d = 1;
switch (mMonth)
{
case 1:
{
first_day = y12 + y12 / 4 + c12 / 4 - 2 * c12 + 26 * (13 + 1) / 10 + d - 1;
break;
}
case 2:
{
first_day = y12 + y12 / 4 + c12 / 4 - 2 * c12 + 26 * (14 + 1) / 10 + d - 1;
break;
}
case 3:
{
first_day = y + y / 4 + c / 4 - 2 * c + 26 * (m + 1) / 10 + d - 1;
break;
}
case 4:
{
first_day = y + y / 4 + c / 4 - 2 * c + 26 * (m + 1) / 10 + d - 1;
break;
}
case 5:
{
first_day = y + y / 4 + c / 4 - 2 * c + 26 * (m + 1) / 10 + d - 1;
break;
}
case 6:
{
first_day = y + y / 4 + c / 4 - 2 * c + 26 * (m + 1) / 10 + d - 1;
break;
}
case 7:
{
first_day = y + y / 4 + c / 4 - 2 * c + 26 * (m + 1) / 10 + d - 1;
break;
}
case 8:
{
first_day = y + y / 4 + c / 4 - 2 * c + 26 * (m + 1) / 10 + d - 1;
break;
}
case 9:
{
first_day = y + y / 4 + c / 4 - 2 * c + 26 * (m + 1) / 10 + d - 1;
break;
}
case 10:
{
first_day = y + y / 4 + c / 4 - 2 * c + 26 * (m + 1) / 10 + d - 1;
break;
}
case 11:
{
first_day = y + y / 4 + c / 4 - 2 * c + 26 * (m + 1) / 10 + d - 1;
break;
}
case 12:
{
first_day = y + y / 4 + c / 4 - 2 * c + 26 * (m + 1) / 10 + d - 1;
break;
}
}
if (first_day < 0)
first_day = 7 - (Math.abs(first_day)) % 7;// first_day每月第一天星期几
else
first_day = first_day % 7;
switch (mMonth)
{
case 1:
{
CalculateCalendar(1, first_day, 31);
break;
}
case 2:
{
CalculateCalendar(2, first_day, 28 + month);
break;
}
case 3:
{
CalculateCalendar(3, first_day, 31);
break;
}
case 4:
{
CalculateCalendar(4, first_day, 30);
break;
}
case 5:
{
CalculateCalendar(5, first_day, 31);
break;
}
case 6:
{
CalculateCalendar(6, first_day, 30);
break;
}
case 7:
{
CalculateCalendar(7, first_day, 31);
break;
}
case 8:
{
CalculateCalendar(8, first_day, 31);
break;
}
case 9:
{
CalculateCalendar(9, first_day, 30);
break;
}
case 10:
{
CalculateCalendar(10, first_day, 31);
break;
}
case 11:
{
CalculateCalendar(11, first_day, 30);
break;
}
case 12:
{
CalculateCalendar(12, first_day, 31);
break;
}
}
}
public void CalculateCalendar(int month_no, int week_no, int month_days)
{
int i, s = 0;
int currentDay;
if (this.mDay == 0)
{
mDay = 1;
currentDay = HoyahCalendar.mDay;
}
else
{
currentDay = this.mDay;
}
// String[][] a = new String[6][7];
for (i = 0; i < week_no; i++)
HoyahCalendar.a[i / 7][i % 7] = "";
for (i = week_no; i < week_no + month_days; i++)
{
s = i - week_no + 1;
HoyahCalendar.a[i / 7][i % 7] = String.valueOf(s);
if (s == currentDay && HoyahCalendar.currentIndex == -1)
{
HoyahCalendar.currentIndex = i / 7;
}
}
for (i = 0; i < 7; i++)
{
if (HoyahCalendar.a[HoyahCalendar.currentIndex][i] == null)
{
HoyahCalendar.a[0][i] = "";
}
else
{
HoyahCalendar.a[0][i] = HoyahCalendar.a[HoyahCalendar.currentIndex][i];
}
}
for (i = week_no + month_days; i < 42; i++)
HoyahCalendar.a[i / 7][i % 7] = "";
//my fix starts here, I don't want to miss with your code
int a[][] = getArrayCalendar(mYear, mMonth);
HoyahCalendar.a = toStringArray(a);// Here I replaced your array
}
private String[][] toStringArray(int[][] a)
{
String[][] array = new String[6][7];
for (int i = 0; i < a.length; i++)
{
for (int j = 0; j < a[0].length; j++)
{
array[i][j] = Integer.toString(a[i][j]);
}
System.out.println();
}
return array;
}
/**
*
* @param year
* the year
* @param month
* January=0, Feb = 1, ...., Nov = 10, and Dec = 11, you know
* what I mean
* @return
*/
private static int[][] getArrayCalendar(int year, int month)
{
// VERY IMPORTANT
// SUN = 1, MON = 2, TU = 3, ..., Fri = 6, Sat = 7 according to USA
int[][] a = new int[6][7];
int day = 1; // day must be always one
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, month);
cal.set(Calendar.DAY_OF_MONTH, day);
int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
int goBack = 1 - dayOfWeek;
cal.add(Calendar.DAY_OF_MONTH, goBack);
System.out.println("SU\tMO\tTU\tWE\tTH\tFR\tSA");
for (int i = 0; i < a.length; i++)
{
for (int j = 0; j < a[0].length; j++)
{
a[i][j] = cal.get(Calendar.DAY_OF_MONTH);
System.out.print(cal.get(Calendar.DAY_OF_MONTH));
System.out.print("\t");
cal.add(Calendar.DAY_OF_MONTH, 1);
}
System.out.println();
}
return a;
}
}
现在您可以使用该功能为任何(年,月)生成完整的monthely日历,并使用它来浏览您的UI。