Membuat Calendar dan Jam
public void jam(){
ActionListener taskPerformer = new ActionListener() {
public void actionPerformed(ActionEvent e) {
String nol_bulan = "";
String nol_hari = "";
String nol_jam = "";
String nol_menit = "";
String nol_detik = "";
//membuat calender
Calendar dt = Calendar.getInstance();
//mengambil nilai jam, menit, detik sekarang
int nilai_tahun = dt.get(dt.YEAR);
int nilai_bulan = dt.get(dt.MONTH);
int nilai_hari = dt.get(dt.DAY_OF_MONTH);
int nilai_jam = dt.get(dt.HOUR_OF_DAY);
int nilai_menit = dt.get(dt.MINUTE);
int nilai_detik = dt.get(dt.SECOND);
//jika nilai jam lebih kecil dari 10 (hanya 1 digit)
if (nilai_bulan <= 9) {
nol_bulan = "0";
}
if (nilai_hari <= 9) {
nol_hari = "0";
}
if (nilai_jam <= 9) {
nol_jam = "0";
}
if (nilai_menit <= 9) {
nol_menit = "0";
}
if (nilai_detik <= 9) {
nol_detik = "0";
}
String bulan = nol_bulan + Integer.toString(nilai_bulan + 1);
String hari = nol_hari + Integer.toString(nilai_hari);
String jam = nol_jam + Integer.toString(nilai_jam);
String menit = nol_menit + Integer.toString(nilai_menit);
String detik = nol_detik + Integer.toString(nilai_detik);
switch(nilai_bulan){
case 1:
namaBulan = "Januari"; break;
case 2:
namaBulan = "Februari"; break;
case 3:
namaBulan = "Maret"; break;
case 4:
namaBulan = "April"; break;
case 5:
namaBulan = "Mei"; break;
case 6:
namaBulan = "Juni";break;
case 7:
namaBulan = "Juli"; break;
case 8:
namaBulan = "Agustus"; break;
case 9:
namaBulan = "September"; break;
case 10:
namaBulan = "Oktober"; break;
case 11:
namaBulan = "November"; break;
case 12:
namaBulan = "Desember"; break;
default:System.out.println("Tidak ada angka bulan untuk dikonversi");
}
tanggal.setText(hari+ " " + namaBulan + " " + nilai_tahun);
Njam.setText(jam+ " : " + menit+ " : " +detik);
//txtTglAnda.setDate(Date.valueOf(nilai_tahun + "-" + bulan + "-" + hari));
}
};
new javax.swing.Timer(1000, taskPerformer).start();
}
Tidak ada komentar:
Posting Komentar