Pertemuan 4 OOP2 Elearning-1
Pertemuan 4 OOP2 Elearning-1
Materi :
Project NetBeans Komponen GUI
- Table dan ComboBox
- Fungsi : - Calendar dan Date
❑ Calendar dan Date
➢ Konsep date java
No Konstruktor dan Deskripsi
1 Date()
Konstruktor ini menginisialisasi objek dengan tanggal dan
waktu.
2 Date(long millisec)
Konstruktor ini menerima argumen yang sama dengan jumlah
milidetik yang telah berlalu sejak tengah malam, 1 Januari
1970
Konstruktor
▪ Date() : Membuat objek tanggal yang mewakili tanggal dan waktu saat ini.
▪ Date(long milliseconds) : Membuat objek tanggal untuk milidetik yang diberikan
sejak 1 Januari 1970, 00:00:00 GMT.
▪ Date(int year, int month, int date)
▪ Date(int year, int month, int date, int hrs, int min)
▪ Date(int year, int month, int date, int hrs, int min, int sec)
▪ Date(String s)
▪ Note : The last 4 constructors of the Date class are Deprecated.
import java.text.Format;
Contoh 1 :
import java.text.SimpleDateFormat;
import java.util.Date;
LATIHAN 1
import java.util.Calendar;
public class Demo {
public static void main(String[] args) throws Exception {
// displaying current date and time
Calendar cal = Calendar.getInstance();
SimpleDateFormat simpleformat = new SimpleDateFormat("dd/MMMM/yyyy
hh:mm:s");
System.out.println("Today's date and time =
"+simpleformat.format(cal.getTime()));
// displaying date
Format f = new SimpleDateFormat("MM/dd/yy");
String strDate = f.format(new Date());
System.out.println("Current Date = "+strDate);
// current time
f = new SimpleDateFormat("HH.mm.ss Z");
String strTime = f.format(new Date());
System.out.println("Current Time = "+strTime);
// displaying hour
f = new SimpleDateFormat("H");
String strHour = f.format(new Date());
System.out.println("Current Hour = "+strHour);
// displaying minutes
f = new SimpleDateFormat("mm");
String strMinute = f.format(new Date());
System.out.println("Current Minutes = "+strMinute);
// displaying seconds
f = new SimpleDateFormat("ss");
String strSeconds = f.format(new Date());
System.out.println("Current Seconds = "+strSeconds);
}
}
❑ Date Methods
▪ getYear
public int getYear()
Returns the year after 1900.
▪ setYear
public void setYear(int year)
Sets the year.
Parameters:
year - the year value
▪ getMonth
public int getMonth()
Returns the month. This method assigns months with the values 0-11, with
January beginning at value 0.
▪ setMonth
public void setMonth(int month)
Sets the month.
Parameters:
month - the month value (0-11)
▪ getDate
public int getDate()
Returns the day of the month. This method assigns days with the values of
1 to 31.
▪ setDate
public void setDate(int date)
Sets the date.
Parameters:
date - the day value
▪ getDay
public int getDay()
Returns the day of the week. This method assigns days of the week with
the values 0-6, with 0 being Sunday.
▪ setDay
public void setDay(int day)
Sets the day of the week.
Parameters:
day - the value of the day if the week
▪ getHours
public int getHours()
Returns the hour. This method assigns the value of the hours of the day to
range from 0 to 23, with midnight equal to 0.
▪ setHours
public void setHours(int hours)
Sets the hours.
Parameters:
hours - the hour value
▪ getMinutes
public int getMinutes()
Returns the minute. This method assigns the minutes of an hour to be any
value from 0 to 59.
▪ setMinutes
public void setMinutes(int minutes)
Sets the minutes.
Parameters:
minutes - the value of the minutes
▪ getSeconds
public int getSeconds()
Returns the second. This method assigns the seconds of a minute to values
of 0-59.
▪ setSeconds
public void setSeconds(int seconds)
Sets the seconds.
Parameters:
seconds - the second value
❑ Implementasi Calender : LATIHAN 2
Kalender Sederhana
public class SimpleCalendar {
public static void main(String[] args) throws Exception {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy MMM dd HH:mm:ss");
Calendar calendar = new GregorianCalendar(2013,1,28,13,24,56);
}
Adding a Date Picker to an Application UI
import java.util.Locale;
LATIHAN 4 import javafx.application.Application;
import javafx.geometry.HPos;
import javafx.scene.Scene;
import javafx.scene.control.DatePicker;
import javafx.scene.control.Label;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
GridPane.setHalignment(checkInlabel, HPos.LEFT);
gridPane.add(checkInDatePicker, 0, 1);
vbox.getChildren().add(gridPane);
}
}
TUGAS :
Mencari Selisih antara 2 Tanggal (input tgl_awal dan tgl_akhir)
Sumber :
https://www.tutorialspoint.com/java/util/dictionary_keys.htm
https://www.geeksforgeeks.org/java-util-dictionary-
class-java/
https://www.geeksforgeeks.org/hashtable-in-java/
https://docs.oracle.com/javase/8/javafx/user-interface-tutorial/date-
picker.htm