更新日历

时间:2016-10-11 16:45:50

标签: java oop calendar

我对编程很陌生,而且我目前正在学习它的整个面向对象方面。关于如何更新我的日历对象,我一无所知。我不太确定更新方法中需要更新日历的内容。以下是我正在使用的两个班级。

var requestRunning = false;
    jQuery(document).ready(function(){
    var nav = jQuery('#main-navigation');
        nav.find('li>a').on('click',function(e){
            var menu = '',src='';
            var menu = jQuery.trim(jQuery(this).text());
            if (menu == 'Finance Calendar') {
                e.preventDefault();
                src = jQuery(this).attr('href');
                var params = {};
                params.src = src+'&subwin=1&embed=1'; // appending subwin and embed
                params.title = 'Financial Calender';
                params.loadType = 'A'; // Ajax call to make
                if (requestRunning) { // don't do anything if an AJAX request is pending
                    return;
                }
                initPopup(params);
            }
        });
    });

这是我的主要课程:

public class Time {
    private int hour;
    private int minute;
    private int second;
    Calendar cal = Calendar.getInstance();

    public Time(){
        Calendar cal = Calendar.getInstance();
        this.hour = cal.get(Calendar.HOUR_OF_DAY);
        this.minute = cal.get(Calendar.MINUTE);
        this.second = cal.get(Calendar.SECOND);
    }
    public int getHour(){
        return this.hour;
    }
    public int getMinute(){
        return this.minute;
    }
    public int getSecond(){
        return this.second;
    }
    public String toString(){
        return this.hour + ":" + this.minute + ":" + this.second;
    }
    public void update(){
        cal.setTime(new Date());    
    }
}

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:1)

在更新功能中,您只更新cal的实例。您还必须更新小时min和second变量。你可以这样做。

public void update(){
    cal.setTime(new Date());  
    this.hour = cal.get(Calendar.HOUR_OF_DAY);
    this.minute = cal.get(Calendar.MINUTE);
    this.second = cal.get(Calendar.SECOND);
}

为了获得时间上的差异,您可以使用 Thread.Sleep()

import javax.swing.*;
public class Exercise2b {
public void demo() {
    String message1, message2;
    int hour, minute, second;
    Time dt = new Time();
    hour = dt.getHour();
    minute = dt.getMinute();
    second = dt.getSecond();
    message1 = "Klockan är" +minute+"minuter över"+hour+"(+"+second+"sekunder)";
    message2 = dt.toString();
    JOptionPane.showMessageDialog(null, message1);
    JOptionPane.showMessageDialog(null, message2);

    try
    {
        Thread.sleep(5000);
    }
    catch(Exception e)
    {
        System.out.println("Exception Occured");
    }

    dt.update();

    message2 = dt.toString();
    JOptionPane.showMessageDialog(null, message2);

}

public static void main(String[] args) {  
    Exercise2b prog = new Exercise2b();
    prog.demo();    
    }
}