Program menampilkan waktu sekarang

pertama kita buat program class untuk memanggil waktu yang akan kita panggil pada class kedua..sebut saja ini adalah Waktu.java berikut source codenya :

 

import java.util.Calendar;

public class Waktu {

private Calendar calendar = Calendar.getInstance();

public String getHari() {

if (calendar.get(Calendar.DAY_OF_WEEK) == 1) {
return “Minggu”;
} else if (calendar.get(Calendar.DAY_OF_WEEK) == 2) {
return “Senin”;
} else if (calendar.get(Calendar.DAY_OF_WEEK) == 3) {
return “Selasa”;
} else if (calendar.get(Calendar.DAY_OF_WEEK) == 4) {
return “Rabu”;
} else if (calendar.get(Calendar.DAY_OF_WEEK) == 5) {
return “Kamis”;
} else if (calendar.get(Calendar.DAY_OF_WEEK) == 6) {
return “Jumat”;
} else {
return “Sabtu”;
}
}

public String getBulan() {

if (calendar.get(Calendar.MONTH) == 0) {
return “Januari”;
} else if (calendar.get(Calendar.MONTH) == 1) {
return “Februari”;
} else if (calendar.get(Calendar.MONTH) == 2) {
return “Maret”;
} else if (calendar.get(Calendar.MONTH) == 3) {
return “April”;
} else if (calendar.get(Calendar.MONTH) == 4) {
return “Mei”;
} else if (calendar.get(Calendar.MONTH) == 5) {
return “Juni”;
} else if (calendar.get(Calendar.MONTH) == 6) {
return “Juli”;
} else if (calendar.get(Calendar.MONTH) == 7) {
return “Agustus”;
} else if (calendar.get(Calendar.MONTH) == 8) {
return “September”;
} else if (calendar.get(Calendar.MONTH) == 9) {
return “Oktober”;
} else if (calendar.get(Calendar.MONTH) == 10) {
return “November”;
} else {
return “Desember”;
}
}

public String getWaktu() {

String hari;
String tanggal;
String bulan;
String tahun;
hari = getHari();
tanggal = String.valueOf(calendar.get(Calendar.DAY_OF_MONTH));
bulan = getBulan();
tahun = String.valueOf(calendar.get(Calendar.YEAR));
return (hari + “, ” + tanggal + ” ” + bulan + ” ” + tahun);

}
}

==============================================================================

kemudian kita buat satu class lagi yaitu test waktu untuk menampilkan waktu sekarang…kita sebut testwaktu.java berikut source code nya :

 

public class TestWaktu {

public static void main(String[] args) {
Waktu waktu = new Waktu();
System.out.println(“Hari dan tanggal sekarang : ” + waktu.getWaktu());
}
}

 

Mudah kan…….

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: