CARA MEMBUAT JAM ATAU WAKTU DAN TANGGAL SECARA REAL TIME DENGAN BAHASA PEMROGRAMAN JAVA

-luntas ilmu- berikut ini saya akan memberikan tutorial bagaimana membuat jam atau waktu dan tanggal secara realtime dengan bahasa pemrograman java. Langsung saja ikuti langkah-langkah dibawah ini.

pertama buat 2 buah class java yang pertama berinama Collections.java dan ketikkan source code dibawah ini

package Form;

/**
 *
 * @author sayfudin
 */

import java.text.SimpleDateFormat;
import java.util.Calendar;
 
public class Timing {
    public String showTimeNow() {
        Calendar cal = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
        String hasil = sdf.format(cal.getTime());
        return hasil;
    }
    public String showDateNow() {
        Calendar cal = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("dd MMMM yyyy");
        String hasil = sdf.format(cal.getTime());
        return hasil;
    }
}

setelah itu buat lagi class untuk class utamanya dan masukkan kode dibawah ini. Ingat kelas ini jadikan 1 folder dengan class colections diatas jangan dipisah supaya sesuai kode yang saya tulis.

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import javax.swing.Timer;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.Color;

/**
 *
 * @author sayfudin
 */
public class GUIForm extends javax.swing.JFrame {
     
    private static final long serialVersionUID = 1L;
 
    //public String showTimeNow() {
    private JPanel contentPane;
 
    //membuat variabel timerwaktu dan membuat object dari class Collections
    Timer timerwaktu;
    Collections ST = new Collections();

  
    public GUIForm() {
        initComponents();
        setTitle("Tanggal dan Waktu Secara Realtime");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 94);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(new BorderLayout(0, 0));
        setContentPane(contentPane);
 
        //kita rubah modifier lblTimer menjadi final
        final JLabel lblTimer = new JLabel();
        lblTimer.setForeground(Color.RED);
        lblTimer.setFont(new Font("Tahoma", Font.PLAIN, 20));
        contentPane.add(lblTimer, BorderLayout.WEST);
 
        ActionListener ActionInfoWaktu = new ActionListener(){
            public void actionPerformed(ActionEvent e){
                lblTimer.setText("Tanggal " + GUIForm.this.ST.showDateNow()+ ", Jam " + GUIForm.this.ST.showTimeNow());
            }
        };
        this.timerwaktu = new Timer(1000, ActionInfoWaktu);
        this.timerwaktu.start();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // //GEN-BEGIN:initComponents
    private void initComponents() {

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );

        pack();
    }// //GEN-END:initComponents

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(GUIForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(GUIForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(GUIForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(GUIForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new GUIForm().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables
}
setelah itu jalankan kode nya di run jika berhasil maka akan muncul hasilnya. dan selamat anda berhasil silakan di modif sendiri sesuai kreatifitas anda jika desain ini kurang baik.