以文本方式查看主题

-  课外天地 李树青  (http://www.njcie.com/bbs/index.asp)
--  Java程序语言课件  (http://www.njcie.com/bbs/list.asp?boardid=17)
----  作业之四:时间显示与日历  (http://www.njcie.com/bbs/dispbbs.asp?boardid=17&id=485)

--  作者:admin
--  发布时间:2008/5/7 14:06:36
--  作业之四:时间显示与日历

1、日历类的基本用法
class exec
{
    public static void main(String args[])
    {
        java.util.Calendar rightNow = java.util.Calendar.getInstance();
        System.out.println(rightNow.getTime());
        System.out.println("今年是"+rightNow.get(java.util.Calendar.YEAR)+"年");
        
                String[] monthOfYear={"一","二","三","四","五","六","七","八","九","十","十一","十二"};
                
                System.out.println("月份是"+monthOfYear[rightNow.get(java.util.Calendar.MONTH)]+"月");
                System.out.println("日期是"+rightNow.get(java.util.Calendar.DATE)+"日");
                
                String[] dayOfWeek={"日","一","二","三","四","五","六"};
                System.out.println("今天是星期"+dayOfWeek[rightNow.get(java.util.Calendar.DAY_OF_WEEK)-1]);
                
                System.out.println("现在是"+rightNow.get(java.util.Calendar.HOUR_OF_DAY)+"小时"+rightNow.get(java.util.Calendar.MINUTE)+"分"+rightNow.get
(java.util.Calendar.SECOND)+"秒");
                
                System.out.println(java.text.DateFormat.getDateInstance(java.text.DateFormat.FULL).format(rightNow.getTime()));
    }
}

2、格式化日历类
import java.text.DateFormat;
import java.util.Date;

public class Time
{

        public static void main(String[] args)
        {
                Date d = new Date();
                String s;
                s = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.LONG).format(d);
                System.out.println("现在日期和时间是:" + s);
        }

}

3、更为复杂的完整月日历程序(只能显示特定的月份)
import java.util.*;

class exec
{
    public static void main(String args[])
    {
        System.out.println("日 一 二 三 四 五 六");
        Calendar calendar = Calendar.getInstance();
        calendar.set(2004, 9, 1);
        int day = calendar.get(Calendar.DAY_OF_WEEK) - 1;
        String a[] = new String[day + 31]; //存放号码的一维数组
        for (int i = 0; i < day; i++)
        {
            a[i] = "  ";
        }
        for (int i = day, n = 1; i < day + 31; i++)
        {
            if (n <= 9)
                a[i] = String.valueOf(n) + " ";
            else
                a[i] = String.valueOf(n);
            n++;
        }
        //打印数组
        for (int i = 0; i < a.length; i++)
        {
            if (i % 7 == 0)
            {
                System.out.println(""); //换行
            }
            System.out.print(" " + a[i]);
        }
        System.out.println();
    }
}


4、最为灵活的显示任何当前月份的月日历程序
import java.util.*;

public class exec
{
        public static void main(String[] args)
        {
                java.text.DecimalFormat myformat = new java.text.DecimalFormat("00");//
                java.util.GregorianCalendar gregorianCalendar = new java.util.GregorianCalendar();
                int maxDay = gregorianCalendar
                                .getActualMaximum(gregorianCalendar.DAY_OF_MONTH);
                int year = gregorianCalendar.get(gregorianCalendar.YEAR);
                int month = gregorianCalendar.get(gregorianCalendar.MONTH) + 1;
                int day = gregorianCalendar.get(gregorianCalendar.DAY_OF_MONTH);
                int weekday = gregorianCalendar.get(gregorianCalendar.DAY_OF_WEEK);
                int hour = gregorianCalendar.get(gregorianCalendar.HOUR);
                int minite = gregorianCalendar.get(gregorianCalendar.MINUTE);
                int second = gregorianCalendar.get(gregorianCalendar.SECOND);
                int j = 1;
                System.out.println("今天是" + year + "年" + month + \'月\' + day + "日" + "星期"
                                + (weekday - 1));
                gregorianCalendar.set(gregorianCalendar.DAY_OF_MONTH, 1);
                int space = gregorianCalendar.get(gregorianCalendar.DAY_OF_WEEK);
                int counter = space - 1;
                String[] days = new String[counter + maxDay];

                for (int i = 0; i < counter; i++)
                {
                        days[i] = "  ";
                }
                for (int i = counter; i < counter + maxDay; i++)
                {
                        days[i] = myformat.format(j);
                        j++;
                }
                System.out.print("日\\t一\\t二\\t三\\t四\\t五\\t六\\n");
                int m = 1;
                for (int i = 0; i < counter + maxDay; i++)
                {

                        System.out.print("" + days[i] + "\\t");
                        if ((m++) % 7 == 0)
                                System.out.print("\\n");
                }
        }
}


 

[此贴子已经被作者于2010-12-12 08:25:03编辑过]