util包中Calendar使用

 

/**
* Calendar.java
* writeby jsp update2006-11-2
* util class study
*
*/
import java.util.*;

class MonthErro extends Exception
{
    MonthErro()
    {
        super("月份越界");
    }
}

class YaerErro extends Exception
{
    YaerErro()
    {
        super("年份越界");
    }
}

class CalendarTest
{
     //判断是否是闰年
    public static boolean loop(int year)
    {
          if(((year % 4 ==0)&&(year % 100 !=0)) || (year % 400 ==0))
          {
               return true;
          }
          else
          {
               return false;
          }
    }
   //确定每月的天数
    public static int count(int month,int year)
    {
         if(month == 2)
         {
             if(loop(year))
             {
                   return 29;
             }
             else
             {
                   return 28;
             }
         }
         else if((month == 1) || (month == 3) || (month == 5) || (month == 7) || (month == 8) || (month == 10) || (month == 12))
         {
              return 31;
         }
         else
         {
              return 30;
         }  
     }

    public static void dis(int days,int week)
    {

       System.out.println("/t日/t一/t二/t三/t四/t五/t六");
       System.out.print("/t");
       for(int i = 0; i < week; i++)
       {
         System.out.print("/t");
       }
      
       int week1 = week;
      
       for(int j = 1; j <= days ; j++)
       {
         
          if(week1 == 7)
          {
            System.out.println();
            System.out.print("/t");
            week1 = 0;
          }
          System.out.print(j+"/t");
          week1++;
         }
    }
    public static void main(String[] args)
    {
       try{
       if(args.length!= 2)
       {
           Calendar today = Calendar.getInstance();
           int month = today.get(Calendar.MONTH)+1;
           int year = today.get(Calendar.YEAR);
           GregorianCalendar todayWeek = new GregorianCalendar(year,month-1,1);
           int week = todayWeek.get(Calendar.DAY_OF_WEEK)-1;
           int days = count(month,year);
         
           System.out.println("/t"+year+"-"+month);
           dis (days,week);         
       }
       else
       {
         int month = Integer.parseInt(args[0]);
         if((month > 12) || (month < 1))
         {
              throw new MonthErro();
         }
         int year = Integer.parseInt(args[1]);
         if(year < 0)
         {
              throw new YaerErro();
         }
         GregorianCalendar today = new GregorianCalendar(year,month-1,1);
         int week = today.get(Calendar.DAY_OF_WEEK)-1;
         int days = count(month,year);
         System.out.println("/t"+year+"-"+(month));
         dis (days,week);         
         
       }
       }catch ( MonthErro me)
       {
           System.out.println("Exception in CalendarTest in main:"+me.toString());
           me.printStackTrace();
       }catch ( YaerErro ye)
       {
           System.out.println("Exception in CalendarTest in main:"+ye.toString());
           ye.printStackTrace();
       }
       catch(Exception e)
       {
           System.out.println("Exception in CalendarTest in main:"+e.toString());
           e.printStackTrace();
       }
         
    }
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值