<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    冷面閻羅

    低調做人&&高調做事
    隨筆 - 208, 文章 - 3, 評論 - 593, 引用 - 0
    數據加載中……

    改進java萬年歷,前幾天看到別人寫的java萬年歷,其中不足之處多多

         昨天網上看到別人blog里面寫的自己做的萬年歷不錯,down下來看看,發現有些地方不妥。
         1。在java中獲取指定年月的天數居然還用閏年來判斷,感覺這個人java肯定用的不精。GregorianCalendar類里面有個方法
          isLeapYear(int year)
              確定給定的年份是否為閏年。
      用這個不就ok么?還像在c中那樣做判斷,能不能被4整除,被4整除不被100整除的。。。。
       獲取指定年月的天數
            Date date = new Date(year_log, month_log + 11); // now
            Calendar cal = Calendar.getInstance();
            cal.setTime(date);
            cal.add(Calendar.MONTH, 
    -1); // 前個月
            month_day_score = cal.getActualMaximum(Calendar.DAY_OF_MONTH);// 最后一天
      2。對java中的ActionEvent不熟,不知道JComboBox等什么時候應該觸發什么event。
      3。對線程理解不好。
    11.bmp
    我把改進后的java代碼貼上來,大家看看,里面肯定還有不足之處。
    主類:MainFrame
    package clock;

    /**
     * MainFrame.java
     * Summary 萬年歷主類
     * Created on
     * 
    @author
     * remark 如有改動請發一份代碼給我,郵箱wsh.grxx@163.com
     
    */


    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.GridLayout;
    import java.awt.Toolkit;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.sql.Date;
    import java.util.Calendar;

    import javax.swing.JComboBox;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;


    class MainFrame extends JFrame {
        
    /**
         * 
         
    */

        
    private static final long serialVersionUID = 1L;
        JPanel panel 
    = new JPanel(new BorderLayout());
        JPanel panel1 
    = new JPanel();
        JPanel panel2 
    = new JPanel(new GridLayout(77));
        JPanel panel3 
    = new JPanel();
        JLabel[] label 
    = new JLabel[49];
        JLabel y_label 
    = new JLabel("年份");
        JLabel m_label 
    = new JLabel("月份");
        JComboBox com1 
    = new JComboBox();
        JComboBox com2 
    = new JComboBox();
        
    int re_year, re_month;
        
    int x_size, y_size;
        String year_num;
        Calendar now 
    = Calendar.getInstance(); // 實例化Calendar

        MainFrame() 
    {
            
    super("萬年歷");
            setSize(
    300350);
            x_size 
    = (int) (Toolkit.getDefaultToolkit().getScreenSize().getWidth());
            y_size 
    = (int) (Toolkit.getDefaultToolkit().getScreenSize().getHeight());
            setLocation((x_size 
    - 300/ 2, (y_size - 350/ 2);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            panel1.add(y_label);
            panel1.add(com1);
            panel1.add(m_label);
            panel1.add(com2);
            
    for (int i = 0; i < 49; i++{
                label[i] 
    = new JLabel("", JLabel.CENTER);// 將顯示的字符設置為居中
                panel2.add(label[i]);
            }

            panel3.add(
    new Clock(this));
            panel.add(panel1, BorderLayout.NORTH);
            panel.add(panel2, BorderLayout.CENTER);
            panel.add(panel3, BorderLayout.SOUTH);
            panel.setBackground(Color.white);
            panel1.setBackground(Color.white);
            panel2.setBackground(Color.white);
            panel3.setBackground(Color.white);
            Init();
            com1.addActionListener(
    new ClockAction());
            com2.addActionListener(
    new ClockAction());

            setContentPane(panel);
            setVisible(
    true);
            setResizable(
    false);
        }


        
    class ClockAction implements ActionListener {
            
    public void actionPerformed(ActionEvent arg0) {
                
    int c_year, c_month, c_week;
                c_year 
    = Integer.parseInt(com1.getSelectedItem().toString()); // 得到當前所選年份
                c_month = Integer.parseInt(com2.getSelectedItem().toString()) - 1// 得到當前月份,并減1,計算機中的月為0-11
                c_week = use(c_year, c_month); // 調用函數use,得到星期幾
                Resetday(c_week, c_year, c_month); // 調用函數Resetday
            }

        }

        
        
    public void Init() {
            
    int year, month_num, first_day_num;
            String log[] 
    = """""""""""""" };
            
    for (int i = 0; i < 7; i++{
                label[i].setText(log[i]);
            }

            
    for (int i = 0; i < 49; i = i + 7{
                label[i].setForeground(Color.red); 
    // 將星期日的日期設置為紅色
            }

            
    for (int i = 6; i < 49; i = i + 7{
                label[i].setForeground(Color.green);
    // 將星期六的日期設置為綠色
            }

            
    for (int i = 1; i < 10000; i++{
                com1.addItem(
    "" + i);
            }

            
    for (int i = 1; i < 13; i++{
                com2.addItem(
    "" + i);
            }

            month_num 
    = (int) (now.get(Calendar.MONTH)); // 得到當前時間的月份
            year = (int) (now.get(Calendar.YEAR)); // 得到當前時間的年份
            com1.setSelectedIndex(year - 1); // 設置下拉列表顯示為當前年
            com2.setSelectedIndex(month_num); // 設置下拉列表顯示為當前月
            first_day_num = use(year, month_num);
            Resetday(first_day_num, year, month_num);
        }


        
    public int use(int reyear, int remonth) {
            
    int week_num;
            now.set(reyear, remonth, 
    1); // 設置時間為所要查詢的年月的第一天
            week_num = (int) (now.get(Calendar.DAY_OF_WEEK));// 得到第一天的星期
            return week_num;
        }


        @SuppressWarnings(
    "deprecation")
        
    public void Resetday(int week_log, int year_log, int month_log) {
            
    int month_day_score; // 存儲月份的天數
            int count;
            month_day_score 
    = 0;
            count 
    = 1;

            Date date 
    = new Date(year_log, month_log + 11); // now
            Calendar cal = Calendar.getInstance();
            cal.setTime(date);
            cal.add(Calendar.MONTH, 
    -1); // 前個月
            month_day_score = cal.getActualMaximum(Calendar.DAY_OF_MONTH);// 最后一天

            
    for (int i = 7; i < 49; i++// 初始化標簽
                label[i].setText("");
            }

            week_log 
    = week_log + 6// 將星期數加6,使顯示正確
            month_day_score = month_day_score + week_log;
            
    for (int i = week_log; i < month_day_score; i++, count++{
                label[i].setText(count 
    + "");
            }

        }


        
    public static void main(String[] args) {
            JFrame.setDefaultLookAndFeelDecorated(
    true);
            
    new MainFrame();
        }

    }

    顯示時間的類:Clock
    package clock;

    /**
     * Clock.java
     * Summary 數字時間顯示
     * Created on
     * 
    @author
     * remark 
     
    */


    import java.awt.Canvas;
    import java.awt.Color;
    import java.awt.Font;
    import java.awt.Graphics;
    import java.text.SimpleDateFormat;
    import java.util.Calendar;
    class Clock extends Canvas implements Runnable{
        
    /**
         * 
         
    */

        
    private static final long serialVersionUID = 3660124045489727166L;
        MainFrame mf;
        Thread t;
        String time;
        
    public Clock(MainFrame mf){
        
    this.mf=mf;
        setSize(
    280,40);
        setBackground(Color.white);
        t
    =new Thread(this);                //實例化線程
        t.start();                        //調用線程
        }

        
    public void run(){
        
    while(true){
        
    try{
        Thread.sleep(
    1000);                    //休眠1秒鐘
        }
    catch(InterruptedException e){
        System.out.println(
    "異常");
        }

        
    this.repaint(100);
        }

        }

        
    public void paint(Graphics g){
        Font f
    =new Font("宋體",Font.BOLD,16);
        SimpleDateFormat SDF
    =new SimpleDateFormat("yyyy'年'MM'月'dd'日'HH:mm:ss");//格式化時間顯示類型
        Calendar now=Calendar.getInstance();
        time
    =SDF.format(now.getTime());        //得到當前日期和時間
        g.setFont(f);
        g.setColor(Color.orange);
        g.drawString(time,
    45,25);
        }

    }

    posted on 2006-12-31 09:58 冷面閻羅 閱讀(19625) 評論(13)  編輯  收藏 所屬分類: java

    評論

    # re: 改進java萬年歷,前幾天看到別人寫的java萬年歷,其中不足之處多多  回復  更多評論   

    http://m.tkk7.com/vip01/archive/2006/12/29/90688.html
    大家可以互相學習下。
    看看偶年月日怎么生成的

    俺的是swt板的
    2006-12-31 14:16 | 關注

    # re: 改進java萬年歷,前幾天看到別人寫的java萬年歷,其中不足之處多多  回復  更多評論   

    好厲害哦。加油啊。
    2007-01-02 09:04 | mixianger

    # re: 改進java萬年歷,前幾天看到別人寫的java萬年歷,其中不足之處多多  回復  更多評論   

    如果能加上農歷的話,那就更cool了
    2007-01-02 22:59 | 佚名[匿名]

    # re: 改進java萬年歷,前幾天看到別人寫的java萬年歷,其中不足之處多多  回復  更多評論   

    恩 ,這個我也想過,不過到現在沒發現java有這方面出來的類吧,這可是中國特色的....
    2007-01-03 15:43 | 冷面閻羅

    # re: 改進java萬年歷,前幾天看到別人寫的java萬年歷,其中不足之處多多  回復  更多評論   

    網上有 C# 和 java 的農歷算法實現,靠java api 去實現那要等了
    2007-01-03 23:11 | switch

    # re: 改進java萬年歷,前幾天看到別人寫的java萬年歷,其中不足之處多多  回復  更多評論   

    加農歷我已經弄好了
    http://m.tkk7.com/soddabao/archive/2007/01/04/91729.html
    2007-01-04 13:13 | 冷面閻羅

    # re: 改進java萬年歷,前幾天看到別人寫的java萬年歷,其中不足之處多多[未登錄]  回復  更多評論   

    Calendar里我也沒有找到isLeapYear()方法,樓主是不是有誤?
    2007-06-19 08:44 | wzjin

    # re: 改進java萬年歷,前幾天看到別人寫的java萬年歷,其中不足之處多多  回復  更多評論   

    不好意思 寫錯了 應該是這個GregorianCalendar類。Calendar類的一個子類。API中是這樣描述這個類的
    public class GregorianCalendarextends CalendarGregorianCalendar is a concrete subclass of Calendar and provides the standard calendar system used by most of the world.

    GregorianCalendar is a hybrid calendar that supports both the Julian and Gregorian calendar systems with the support of a single discontinuity, which corresponds by default to the Gregorian date when the Gregorian calendar was instituted (October 15, 1582 in some countries, later in others). The cutover date may be changed by the caller by calling setGregorianChange().

    Historically, in those countries which adopted the Gregorian calendar first, October 4, 1582 (Julian) was thus followed by October 15, 1582 (Gregorian). This calendar models this correctly. Before the Gregorian cutover, GregorianCalendar implements the Julian calendar. The only difference between the Gregorian and the Julian calendar is the leap year rule. The Julian calendar specifies leap years every four years, whereas the Gregorian calendar omits century years which are not divisible by 400.

    GregorianCalendar implements proleptic Gregorian and Julian calendars. That is, dates are computed by extrapolating the current rules indefinitely far backward and forward in time. As a result, GregorianCalendar may be used for all years to generate meaningful and consistent results. However, dates obtained using GregorianCalendar are historically accurate only from March 1, 4 AD onward, when modern Julian calendar rules were adopted. Before this date, leap year rules were applied irregularly, and before 45 BC the Julian calendar did not even exist.

    Prior to the institution of the Gregorian calendar, New Year's Day was March 25. To avoid confusion, this calendar always uses January 1. A manual adjustment may be made if desired for dates that are prior to the Gregorian changeover and which fall between January 1 and March 24.

    Values calculated for the WEEK_OF_YEAR field range from 1 to 53. Week 1 for a year is the earliest seven day period starting on getFirstDayOfWeek() that contains at least getMinimalDaysInFirstWeek() days from that year. It thus depends on the values of getMinimalDaysInFirstWeek(), getFirstDayOfWeek(), and the day of the week of January 1. Weeks between week 1 of one year and week 1 of the following year are numbered sequentially from 2 to 52 or 53 (as needed).

    For example, January 1, 1998 was a Thursday. If getFirstDayOfWeek() is MONDAY and getMinimalDaysInFirstWeek() is 4 (these are the values reflecting ISO 8601 and many national standards), then week 1 of 1998 starts on December 29, 1997, and ends on January 4, 1998. If, however, getFirstDayOfWeek() is SUNDAY, then week 1 of 1998 starts on January 4, 1998, and ends on January 10, 1998; the first three days of 1998 then are part of week 53 of 1997.

    Values calculated for the WEEK_OF_MONTH field range from 0 to 6. Week 1 of a month (the days with WEEK_OF_MONTH = 1) is the earliest set of at least getMinimalDaysInFirstWeek() contiguous days in that month, ending on the day before getFirstDayOfWeek(). Unlike week 1 of a year, week 1 of a month may be shorter than 7 days, need not start on getFirstDayOfWeek(), and will not include days of the previous month. Days of a month before week 1 have a WEEK_OF_MONTH of 0.

    For example, if getFirstDayOfWeek() is SUNDAY and getMinimalDaysInFirstWeek() is 4, then the first week of January 1998 is Sunday, January 4 through Saturday, January 10. These days have a WEEK_OF_MONTH of 1. Thursday, January 1 through Saturday, January 3 have a WEEK_OF_MONTH of 0. If getMinimalDaysInFirstWeek() is changed to 3, then January 1 through January 3 have a WEEK_OF_MONTH of 1.

    The clear methods set calendar field(s) undefined. GregorianCalendar uses the following default value for each calendar field if its value is undefined. Field
    Default Value

    ERA
    AD

    YEAR
    1970

    MONTH
    JANUARY

    DAY_OF_MONTH
    1

    DAY_OF_WEEK
    the first day of week

    WEEK_OF_MONTH
    0

    DAY_OF_WEEK_IN_MONTH
    1

    AM_PM
    AM

    HOUR, HOUR_OF_DAY, MINUTE, SECOND, MILLISECOND
    0


    Default values are not applicable for the fields not listed above.

    Example:

    // get the supported ids for GMT-08:00 (Pacific Standard Time)
    String[] ids = TimeZone.getAvailableIDs(-8 * 60 * 60 * 1000);
    // if no ids were returned, something is wrong. get out.
    if (ids.length == 0)
    System.exit(0);

    // begin output
    System.out.println("Current Time");

    // create a Pacific Standard Time time zone
    SimpleTimeZone pdt = new SimpleTimeZone(-8 * 60 * 60 * 1000, ids[0]);

    // set up rules for daylight savings time
    pdt.setStartRule(Calendar.APRIL, 1, Calendar.SUNDAY, 2 * 60 * 60 * 1000);
    pdt.setEndRule(Calendar.OCTOBER, -1, Calendar.SUNDAY, 2 * 60 * 60 * 1000);

    // create a GregorianCalendar with the Pacific Daylight time zone
    // and the current date and time
    Calendar calendar = new GregorianCalendar(pdt);
    Date trialTime = new Date();
    calendar.setTime(trialTime);

    // print out a bunch of interesting things
    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_YEAR: " + calendar.get(Calendar.DAY_OF_YEAR));
    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("HOUR: " + calendar.get(Calendar.HOUR));
    System.out.println("HOUR_OF_DAY: " + calendar.get(Calendar.HOUR_OF_DAY));
    System.out.println("MINUTE: " + calendar.get(Calendar.MINUTE));
    System.out.println("SECOND: " + calendar.get(Calendar.SECOND));
    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)));

    System.out.println("Current Time, with hour reset to 3");
    calendar.clear(Calendar.HOUR_OF_DAY); // so doesn't override
    calendar.set(Calendar.HOUR, 3);
    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_YEAR: " + calendar.get(Calendar.DAY_OF_YEAR));
    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("HOUR: " + calendar.get(Calendar.HOUR));
    System.out.println("HOUR_OF_DAY: " + calendar.get(Calendar.HOUR_OF_DAY));
    System.out.println("MINUTE: " + calendar.get(Calendar.MINUTE));
    System.out.println("SECOND: " + calendar.get(Calendar.SECOND));
    System.out.println("MILLISECOND: " + calendar.get(Calendar.MILLISECOND));
    System.out.println("ZONE_OFFSET: "
    + (calendar.get(Calendar.ZONE_OFFSET)/(60*60*1000))); // in hours
    System.out.println("DST_OFFSET: "
    + (calendar.get(Calendar.DST_OFFSET)/(60*60*1000))); // in hours
    方法是:
    boolean isLeapYear(int year)
    Determines if the given year is a leap year.
    2007-06-19 09:06 | 冷面閻羅

    # re: 改進java萬年歷,前幾天看到別人寫的java萬年歷,其中不足之處多多  回復  更多評論   

    panel3.add(new Clock(this));
    這局代碼我運行怎么有錯誤?
    2007-10-24 14:25 | 人非圣賢

    # re: 改進java萬年歷,前幾天看到別人寫的java萬年歷,其中不足之處多多  回復  更多評論   

    你的帖子確實有錯誤,竟然沒有31天的月份
    2007-12-30 10:57 | rili

    # re: 改進java萬年歷,前幾天看到別人寫的java萬年歷,其中不足之處多多[未登錄]  回復  更多評論   

    如果加上陰陽歷轉換就很棒!
    2009-06-23 16:02 | java菜鳥

    # re: 改進java萬年歷,前幾天看到別人寫的java萬年歷,其中不足之處多多  回復  更多評論   

    還是不懂,想請教哦
    2010-05-18 16:14 | 恩恩

    # re: 改進java萬年歷,前幾天看到別人寫的java萬年歷,其中不足之處多多  回復  更多評論   

    怎么用eclipse實現主界面上的星期和日期
    請教 謝謝
    2012-10-22 09:47 | 梳理中
    主站蜘蛛池模板: 美女视频黄a视频全免费网站色| 久久九九久精品国产免费直播| 免费观看国产小粉嫩喷水| 免费精品久久久久久中文字幕| 亚洲精品二区国产综合野狼| 久草视频免费在线| 美女黄色毛片免费看| 久久久无码精品亚洲日韩蜜臀浪潮| AV免费网址在线观看| 中文字幕成人免费高清在线视频| 亚洲在成人网在线看| 精品亚洲一区二区三区在线播放| 在线成人爽a毛片免费软件| 国产成人精品亚洲| 亚洲精品自在线拍| 久久精品国产亚洲5555| 无码中文字幕av免费放| 国内精品99亚洲免费高清| 国产AV旡码专区亚洲AV苍井空| 亚洲精品中文字幕无码蜜桃| 女人与禽交视频免费看| 日本一卡精品视频免费| 国产成人无码免费看片软件 | 亚洲色偷拍区另类无码专区| 51在线视频免费观看视频| jzzjzz免费观看大片免费| 亚洲欧美日韩一区二区三区| 久久精品国产亚洲AV网站| 日韩精品亚洲专区在线观看| 国产在线观看免费观看不卡 | 亚洲精品国产va在线观看蜜芽| 无码国产精品一区二区免费式影视 | 日韩高清在线免费观看| 99re这里有免费视频精品| 好湿好大好紧好爽免费视频| 亚洲色大成网站www久久九| 亚洲精品视频在线免费| 国产亚洲精品无码成人| 老司机亚洲精品影视www| 免费看片A级毛片免费看| 无人在线直播免费观看|