小方的Java博客

  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  27 随笔 :: 17 文章 :: 115 评论 :: 0 Trackbacks
1.Method_Add
/*
输出:
It is now 星期四 2006/08/17 at 05:55:57 下午 CST
Two years ago was 星期二 2004/08/17 at 05:55:57 下午 CST
*/

package  api.java.util.Calendar;

import  java.text.SimpleDateFormat;
import  java.util.Calendar;

public   class  Method_Add  {
    
public   static   void  main(String[] a)  throws  Exception  {

        Calendar now 
=  Calendar.getInstance();
        
        SimpleDateFormat formatter 
=   new  SimpleDateFormat(
                
" E yyyy/MM/dd 'at' hh:mm:ss a zzz " );
        System.out.println(
" It is now  "   +  formatter.format(now.getTime()));
        
/*
         * public abstract void add(int field, int
         * amount)根据日历的规则,为给定的日历字段添加或减去指定的时间量。
         
*/

        now.add(Calendar.YEAR, 
- 2 );
        System.out.println(
" Two years ago was  "
                
+  formatter.format(now.getTime()));
    }

}


主要经验值获得:
1. java.text.SimpleDateFormat 的用法
2. java.util.Calendar.add( ) 的用法,如何进行日期计算。

----------------------------------------------------------------------------------

2.Class_GregorianCalendar
/*
输出:
136 days remain in current year
*/

package api.java.util.Calendar;

import java.util.Calendar;
import java.util.GregorianCalendar;

public class Class_GregorianCalendar {
    
public static void main(String args[]) {

        Calendar calendar1 
= Calendar.getInstance();
        
int doy1 = calendar1.get(Calendar.DAY_OF_YEAR);

        
int year = calendar1.get(Calendar.YEAR);
        
        
//在具有默认语言环境的默认时区内构造一个带有给定日期设置的 GregorianCalendar
        Calendar calendar2 = new GregorianCalendar(year, 1131);
        
int doy2 = calendar2.get(Calendar.DAY_OF_YEAR);
        
        
//今年最后一天减去今天的剩余天数
        int days = doy2 - doy1;
        System.out.println(days 
+ " days remain in current year");
    }

}


主要经验值获得:
1. java.util.GregorianCalendar 的作用
2. 如何返回指定的日期的Calendar

次要经验值获得
1. 如何计算据今年结束的天数

----------------------------------------------------------------------------------

3.Class_Calendar
/*
输出:
ERA: 1
YEAR: 2006
MONTH: 7
WEEK_OF_YEAR: 33
WEEK_OF_MONTH: 3
DATE: 17
DAY_OF_MONTH: 17
DAY_OF_WEEK: 5
DAY_OF_WEEK_IN_MONTH: 3
AM_PM: 1
MILLISECOND: 92
ZONE_OFFSET: 8
DST_OFFSET: 0

*/

package api.java.util.Calendar;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class Class_Calendar {
    
public static void main(String[] a) {
        Calendar calendar 
= new GregorianCalendar();
        calendar.setTime(
new Date());

        System.out.println(
"ERA: " + calendar.get(Calendar.ERA));
        System.out.println(
"YEAR: " + calendar.get(Calendar.YEAR));
        System.out.println(
"MONTH: " + calendar.get(Calendar.MONTH));
        System.out.println(
"WEEK_OF_YEAR: "
                
+ calendar.get(Calendar.WEEK_OF_YEAR));
        System.out.println(
"WEEK_OF_MONTH: "
                
+ calendar.get(Calendar.WEEK_OF_MONTH));
        System.out.println(
"DATE: " + calendar.get(Calendar.DATE));
        System.out.println(
"DAY_OF_MONTH: "
                
+ calendar.get(Calendar.DAY_OF_MONTH));
        System.out
                .println(
"DAY_OF_WEEK: " + calendar.get(Calendar.DAY_OF_WEEK));
        System.out.println(
"DAY_OF_WEEK_IN_MONTH: "
                
+ calendar.get(Calendar.DAY_OF_WEEK_IN_MONTH));
        System.out.println(
"AM_PM: " + calendar.get(Calendar.AM_PM));
        System.out
                .println(
"MILLISECOND: " + calendar.get(Calendar.MILLISECOND));
        System.out.println(
"ZONE_OFFSET: "
                
+ (calendar.get(Calendar.ZONE_OFFSET) / (60 * 60 * 1000)));
        System.out.println(
"DST_OFFSET: "
                
+ (calendar.get(Calendar.DST_OFFSET) / (60 * 60 * 1000)));
    }

}


主要经验值获得:
1. Calendar 的一些变量和方法用法

----------------------------------------------------------------------------------

4.
posted on 2006-08-17 18:00 方佳玮 阅读(1851) 评论(1)  编辑  收藏 所属分类: JAVA API 自学

评论

# re: [整理] java.util.Calendar 2012-10-09 23:52 杨正益
import java.util.Calendar; import java.util.Scanner; public class Calendar{ public static void main(String[] args){ int m,n; System.out.println("请从键盘输入年份:"); Scanner ym=new Scanner(System.in); m=ym.nextInt(); System.out.println("请从键盘输入月份:"); n=ym.nextInt(); CalendarBean cb=new CalendarBean(); cb.setYear(m); cb.setMonth(n); String[] str="日一二三四五六".toCharArry(); for(char c:str){ System.out.println("%4c", c); } for(int i=0;i<a.length;i++){ if(i%7==0) System.out.println(); System.out.println("%4s",a[i]); } } } package xt9_8; import java.util.Calendar; public class CalendarBean{ String[] day; int year=2008,morth=0; public void setYear(int year){ this.year=year; } public void setMonth(int month){ this.month=month; } public String[] getCalendar(){ String[] a=new String[42]; Calendar 日历 =Calendar.getIntstance(); 日历.set(year,month-1,1); int 星期几 =日历.get(Calendar.Day_OF_WEEK)-1; int day=0; if(month==1||month==3||month==5||month==7||month==8||month10||month==12) day=31; if(month==4||month==6||month==9||month==11) day=30; if(month==2){ if(((year%4==0)&&(year%100!=0))||(year%400==0)) day=29; else day=28; } for(int i=0;i<星期几;i++) a[i]=" "; for(int i=星期几,n=1;i<星期几+day;i++){ a[i]=String.Valueof(n); n++; } for(int i=星期几+day;i<a.length;i++) a[i]=" "; return a; } }请问:这题怎么改  回复  更多评论
  


只有注册用户登录后才能发表评论。


网站导航: