zoukankan      html  css  js  c++  java
  • 重头学习java(5) 日期类

    以下部分为转:http://blog.sina.com.cn/s/blog_45c06e600100pm77.html

    Java中对日期的处理需要用到Calendar类,其中有几个方法在使用时需要新手注意。

    1. 在获取月份时,Calendar.MONTH + 1 的原因
    Java中的月份遵循了罗马历中的规则:当时一年中的月份数量是不固定的,第一个月是JANUARY。而Java中Calendar.MONTH返回的数值其实是当前月距离第一个月有多少个月份的数值,JANUARY在Java中返回“0”,所以我们需要+1。

    2. 在获取星期几 Calendar.DAY_OF_WEEK – 1 的原因
    Java中Calendar.DAY_OF_WEEK其实表示:一周中的第几天,所以他会受到 第一天是星期几 的影响。
    有些地区以星期日作为一周的第一天,而有些地区以星期一作为一周的第一天,这2种情况是需要区分的。
    看下表的返回值

    星期日为一周的第一天 SUN MON TUE WED THU FRI SAT
    DAY_OF_WEEK返回值 1 2 3 4 5 6 7
    星期一为一周的第一天 MON TUE WED THU FRI SAT SUN
    DAY_OF_WEEK返回值 1 2 3 4 5 6 7

    所以Calendar.DAY_OF_WEEK需要根据本地化设置的不同而确定是否需要 “-1”
    Java中设置不同地区的输出可以使用 Locale.setDefault(Locale.地区名) 来实现。

    3. 获取日期时 Calendar.DAY_OF_MONTH 不需要特殊的操作,他直接返回一个月中的第几天

     
        public static void  main(String[] args)
        {
            Locale.setDefault(Locale.CHINA);
             GregorianCalendar now = new GregorianCalendar ( 2013 ,5 , 11 ) ;//6月,月份是从0开始。
             int month = now.get ( Calendar.MONTH ) ;
            
             int today = now.get ( Calendar.DAY_OF_MONTH ) ;
             now.set( Calendar.DAY_OF_MONTH , 1 ) ;//设置日期为1号
             System.out.print(now.toString());
             int weekday = now.get( Calendar.DAY_OF_WEEK ) ;//2013.6.1是星期六,一个星期的第七天
             System.out.println ( weekday ) ;
             int firstweek = now.getFirstDayOfWeek ( );//获取日历开头的星期,一般是星期日
             int index = 0 ;
             int month1 ;
             int day ;
             int i ;
             while ( weekday != firstweek )
             {
               index ++ ;
               now.add( Calendar.DAY_OF_MONTH , -1 ) ;
               weekday = now.get ( Calendar.DAY_OF_WEEK ) ;
              }
             String [] weekname = new DateFormatSymbols ().getShortWeekdays ( ) ;//获取星期一到星期日的英文或中午简写,注意是数组类型
             do {
              System.out.printf ( "%4s" , weekname[ weekday ] ) ;
              now.add ( Calendar.DAY_OF_MONTH , 1 ) ;
              weekday = now.get( Calendar.DAY_OF_WEEK ) ;
             }while ( weekday != firstweek ) ;
             System.out.println ( ) ;
              now.set( Calendar.DAY_OF_MONTH , 1 ) ;
              now.set( Calendar.MONTH , month  ) ;
             for ( i = 1; i <= index ; i ++ )
             {
              System.out.print ( "    ") ;
             };
             do
             {
                day=now.get( Calendar.DAY_OF_MONTH ) ;
                System.out.printf ( "%3d", day ) ;
                if (day == today )
                System.out.print ( "*" ) ;
                else
                System.out.printf ( "   " ) ;
                now.add( Calendar.DAY_OF_MONTH , 1 ) ;
                weekday = now.get ( Calendar.DAY_OF_WEEK ) ;
                if ( weekday == firstweek )
                System.out.println ( ) ;
                month1=now.get( Calendar.MONTH ) ;
             }while ( month1==month ) ;
            
               }
    }

  • 相关阅读:
    打开安装 好的Microsoft Dynamics CRM 4.0 报错误为 Caller does not have enough privilege to set CallerOriginToken to the specified value 的解决办法
    基于 Windows Server 2008 的计算机对 Microsoft Dynamics CRM 4.0 的支持
    Microsoft Dynamics CRM 4.0 如何添加自定义按钮
    Microsoft Dynamics CRM 4.0 Plugin 取值,赋值,查询
    C# 中的 enum(枚举) 类型使用例子
    vue事件的绑定
    表单验证2
    node中模块
    node模块的引入
    node中的读文件
  • 原文地址:https://www.cnblogs.com/aomi/p/3131621.html
Copyright © 2011-2022 走看看