Contoh berikut ini menunjukan Bagaimana caranya menambahkan tanggal dan mengurangi tanggal sekarang dengan java calender, oke berikut contoh programnya
Nama file : java_Calender.java
import java.util.Calendar;
public class java_Calender {
public static void main(String[] args) {
//create Calendar instance
Calendar now = Calendar.getInstance();
System.out.println("Sekarang Tanggal : " + now.get(Calendar.DATE)
+ "-"
+ (now.get(Calendar.MONTH) + 1)
+ "-"
+ now.get(Calendar.YEAR));
//add year to current date using Calendar.add method
now.add(Calendar.YEAR,1);
System.out.println("Tanggal Setelah Setahun : " + now.get(Calendar.DATE)
+ "-"
+ (now.get(Calendar.MONTH) + 1)
+ "-"
+ now.get(Calendar.YEAR));
//substract year from current date
now =Calendar.getInstance();
now.add(Calendar.YEAR,-100);
System.out.println("Tanggal Sebelum 100 Tahun : " + now.get(Calendar.DATE)
+ "-"
+ (now.get(Calendar.MONTH) + 1)
+ "-"
+ now.get(Calendar.YEAR));
}
}
Output
run:
Sekarang Tanggal : 31-7-2016
Tanggal Setelah Setahun : 31-7-2017
Tanggal Sebelum 100 Tahun : 31-7-1916
BUILD SUCCESSFUL (total time: 0 seconds)
Program dibuat menggunakan netbeans
selamat mencoba semoga bermanfaat ...
No comments:
Post a Comment