java calendar 源代码_java swing日历calendar demo源代码下载

package can.lyj.com;

public class Mycalander {

/**

*

* @param year

* 年份

* @return

*/

private static boolean isLoopYear(int year) {

return ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0));

}

/**

*

* @param year

* 年份

* @return 获得当前日期的第一天是星期几

*/

private static int getWeek(int year, int month) {

long days = 0, sum = 0;

final int MAX = 366, MIN = 365, Day_OF_WEEK = 7;

for (int y = 1; y < year; y++) {

days = isLoopYear(y) ? MAX : MIN;

sum += days;

}

for (int mon = 1; mon < month; mon++) {

if (mon != 2) {

int dy = isBig(mon) ? 31 : 30;

sum += dy;

} else if (mon == 2) {

sum += isLoopYear(year) ? 29 : 28;

}

}

int week = (int) ((sum + 1) % Day_OF_WEEK);

return week;

}

/**

*

* @param month月份

* 判断是大月还是小月

*/

private static boolean isBig(int month) {

switch (month) {

case 1:case 3:case 5:case 7:case 8 : case 10 :case 12:

return true;

default:

return false;

}

}

private static int getHowDay(int currMonth, int year) {

if (currMonth == 2) {

if (isLoopYear(year)) {

return 29;

} else

return 28;

}

return isBig(currMonth) ? 31 : 30;

}

/**

* 根据年月获得日历数组

*

* @param years

* @param months

* @return

*/

public static int[][] getDateAry(int years, int months) {

int[][] date = new int[6][7];

int howDays = getHowDay(months, years);

final int before=getHowDay( months==1?12:months-1,months==1?years-1:years);

int howWeek = getWeek(years, months);

int first=before-howWeek+1;

int index = 0;

int priveous=1;

for (int i = 0; i < 6; i++) {

for (int j = 0; j < 7 ; j++) {

if (i == 0) {

if (j < howWeek) {

date[0][j]=first++;

} else {

date[0][j] = ++index;

}

} else if( index

date[i][j] = ++index;

}

else{

date[i][j]=priveous;

priveous++;

}

}

}

return date;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
package com.hexiang.utils; import java.text.SimpleDateFormat; import java.util.*; public class CalendarUtil { public static void main(String args[]) { System.out.println("First day of week is : " + new SimpleDateFormat("yyyy-MM-dd") .format(getFirstDateByWeek(new Date()))); System.out.println("Last day of week is : " + new SimpleDateFormat("yyyy-MM-dd") .format(getLastDateByWeek(new Date()))); System.out.println("First day of month is : " + new SimpleDateFormat("yyyy-MM-dd") .format(getFirstDateByMonth(new Date()))); System.out.println("Last day of month is : " + new SimpleDateFormat("yyyy-MM-dd") .format(getLastDateByMonth(new Date()))); } /** * 获得所在星期的第一天 */ public static Date getFirstDateByWeek(Date date) { Calendar now = Calendar.getInstance(); now.setTime(date); int today = now.get(Calendar.DAY_OF_WEEK); int first_day_of_week = now.get(Calendar.DATE) + 2 - today; // 星期一 now.set(Calendar.DATE, first_day_of_week); return now.getTime(); } /** * 获得所在星期的最后一天 */ public static Date getLastDateByWeek(Date date) { Calendar now = Calendar.getInstance(); now.setTime(date); int today = now.get(Calendar.DAY_OF_WEEK); int first_day_of_week = now.get(Calendar.DATE) + 2 - today; // 星期一 int last_day_of_week = first_day_of_week + 6; // 星期日 now.set(Calendar.DATE, last_day_of_week); return now.getTime(); } /** * 获得所在月份的最后一天 * @param 当前月份所在的时间 * @return 月份的最后一天 */ public static Date getLastDateByMonth(Date date) { Calendar now = Calendar.getInstance(); now.setTime(date); now.set(Calendar.MONTH, now.get(Calendar.MONTH) + 1); now.set(Calendar.DATE, 1); now.set(Calendar.DATE, now.get(Calendar.DATE) - 1); now.set(Calendar.HOUR, 11); now.set(Calendar.MINUTE, 59); now.set(Calendar.SECOND, 59); return now.getTime(); } /** * 获得所在月份的第一天 * @param 当前月份所在的时间 * @return 月份的第一天 */ public static Date getFirstDateByMonth(Date date) { Calendar now = Calendar.getInstance(); now.setTime(date); now.set(Calendar.DATE, 0); now.set(Calendar.HOUR, 12); now.set(Calendar.MINUTE, 0); now.set(Calendar.SECOND, 0); return now.getTime(); } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值