Created
December 14, 2022 13:37
-
-
Save adityasuseno/fa9762ac7b5090bd937d0db2ad3f95b4 to your computer and use it in GitHub Desktop.
Java Program untuk menghitung jumlah hari dimana mobil ganjil dan mobil genap boleh keluar di kawasan ganjil-genap Jakarta
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Calendar; | |
class MobilGanjilGenap { | |
public static void main(String[] args) { | |
int tahun = 2022; /* Set Tahun Disini */ | |
Calendar c = Calendar.getInstance(); | |
c.set(tahun, Calendar.JANUARY, 1); /* set kalender mulai di tanggal 1 Januari */ | |
int genap = 0; /* Jumlah Hari Untuk Mobil Genap */ | |
int ganjil = 0; /* JUmlah Hari Untuk Mobil Ganjil */ | |
/* Ulang Sepanjang Tahun */ | |
while (c.get(Calendar.YEAR) == tahun) { | |
/* Jika hari Sabtu atau Minggu */ | |
if (c.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY || c.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) { | |
genap += 1; | |
ganjil += 1; | |
} | |
/* Jika Tanggal Genap (Habis Dibagi Dua) */ | |
else if (c.get(Calendar.DAY_OF_MONTH) % 2 == 0) { | |
genap += 1; | |
} | |
/* Selain itu tentunya adalah tanggal Ganjil */ | |
else { | |
ganjil += 1; | |
} | |
c.add(Calendar.DATE, 1); /* Next Day */ | |
} | |
System.out.println("Jumlah Hari Mobil Pelat GANJIL Boleh Keluar Selama Tahun " + tahun + " adalah " + ganjil + " Hari"); | |
System.out.println("Jumlah Hari Mobil Pelat GENAP Boleh Keluar Selama Tahun " + tahun + " adalah " + genap + " Hari"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment