Cara Membuat Jam Dan Calendar JAVA



Program sederhana untuk menampilkan jam dan calendar

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

/*
* date.java
*
* Created on Jun 17, 2009, 5:55:27 AM
*/

package jamcalendar;
import javax.swing.*;
import java.io.*;
import java.awt.event.*;
import java.util.*;
/**
*
* @author x1m4
*/
public class date extends javax.swing.JFrame {

/** Creates new form date */
public date() {
initComponents();
setResizable(false);
GregorianCalendar sekarang = new GregorianCalendar();
infoWaktu(sekarang);
Jam();
}

/** 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() {

lblcalendar = new javax.swing.JLabel();
lbljam = new javax.swing.JLabel();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("jam dan calendar");

lbljam.setFont(new java.awt.Font("Tahoma", 1, 20)); // NOI18N
lbljam.setForeground(new java.awt.Color(255, 51, 51));

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(29, 29, 29)
.addComponent(lblcalendar, javax.swing.GroupLayout.PREFERRED_SIZE, 226, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(lbljam, javax.swing.GroupLayout.PREFERRED_SIZE, 126, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(13, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(19, 19, 19)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(lblcalendar, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(lbljam, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(25, Short.MAX_VALUE))
);

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


public void infoWaktu(GregorianCalendar cal){
String hari[] = {" ","Minggu","Senin","Selasa",
"Rabu","Kamis","Jumat","Sabtu"};
String bulan[] = {"Januari","Pebruari","Maret","April","Mei","Juni","Juli",
"Agustus","September","Oktober","November","Desember"};

String day = hari[cal.get(Calendar.DAY_OF_WEEK)];
int date = cal.get(Calendar.DATE);
String month = bulan[cal.get(Calendar.MONTH)];
int year = cal.get(Calendar.YEAR);

lblcalendar.setText("Hari "+day+" Tanggal "+date+" "+month+" "+year);
}

public void Jam() {
ActionListener taskPerformer = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
String waktu[] ={"Pagi","Malam"};

String nol_jam = "";
String nol_menit = "";
String nol_detik = "";
// Membuat Tanggal
Calendar dt = Calendar.getInstance();

// Mengambil nilaj JAM, MENIT, dan DETIK Sekarang
int nilai_jam = dt.get(dt.HOUR_OF_DAY);
int nilai_menit = dt.get(dt.MINUTE);
int nilai_detik = dt.get(dt.SECOND);
String wkt = waktu[dt.get(dt.AM_PM)];

if (nilai_jam <= 9) {
// Menambahkan "0" didepannya
nol_jam = "0";
}
// Jika nilai MENIT lebih kecil dari 10 (hanya 1 digit)
if (nilai_menit <= 9) {
// Menambahkan "0" didepannya
nol_menit = "0";
}
// Jika nilai DETIK lebih kecil dari 10 (hanya 1 digit)
if (nilai_detik <= 9) {
// Tambahkan "0" didepannya
nol_detik = "0";
}
// Membuat String JAM, MENIT, DETIK
String jam = nol_jam + Integer.toString(nilai_jam);
String menit = nol_menit + Integer.toString(nilai_menit);
String detik = nol_detik + Integer.toString(nilai_detik);

// Menampilkan pada Layar
lbljam.setText(jam +" : "+menit+" : "+detik);
}
};
// Timer
new javax.swing.Timer(1000, taskPerformer).start();
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new date().setVisible(true);
}
});
}

// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JLabel lblcalendar;
private javax.swing.JLabel lbljam;
// End of variables declaration//GEN-END:variables

}
Sumber :http://bosojava.blogspot.com