0% found this document useful (0 votes)
20 views

Pertemuan 4 OOP2 Elearning-1

Uploaded by

Edi Priatna
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Pertemuan 4 OOP2 Elearning-1

Uploaded by

Edi Priatna
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Tujuan Pembelajaran :

Mahasisiswa mampu memahami manajemen waktu dengan class


Calendar dan Date

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);

int year = calendar.get(Calendar.YEAR);


int month = calendar.get(Calendar.MONTH); // Jan = 0, dec = 11
int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
int weekOfYear = calendar.get(Calendar.WEEK_OF_YEAR);
int weekOfMonth= calendar.get(Calendar.WEEK_OF_MONTH);

int hour = calendar.get(Calendar.HOUR); // 12 hour clock


int hourOfDay = calendar.get(Calendar.HOUR_OF_DAY); // 24 hour clock
int minute = calendar.get(Calendar.MINUTE);
int second = calendar.get(Calendar.SECOND);
int millisecond= calendar.get(Calendar.MILLISECOND);
System.out.println(sdf.format(calendar.getTime()));

System.out.println("year \t\t: " + year);


System.out.println("month \t\t: " + month);
System.out.println("dayOfMonth \t: " + dayOfMonth);
System.out.println("dayOfWeek \t: " + dayOfWeek);
System.out.println("weekOfYear \t: " + weekOfYear);
System.out.println("weekOfMonth \t: " + weekOfMonth);

System.out.println("hour \t\t: " + hour);


System.out.println("hourOfDay \t: " + hourOfDay);
System.out.println("minute \t\t: " + minute);
System.out.println("second \t\t: " + second);
System.out.println("millisecond \t: " + millisecond);
}
}
GetCurrentDateTime.java
LATIHAN 3
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;

public class GetCurrentDateTime {

private static final DateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");


private static final DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");

public static void main(String[] args) {

Date date = new Date();


System.out.println(sdf.format(date));

Calendar cal = Calendar.getInstance();


System.out.println(sdf.format(cal.getTime()));

LocalDateTime now = LocalDateTime.now();


System.out.println(dtf.format(now));

LocalDate localDate = LocalDate.now();


System.out.println(DateTimeFormatter.ofPattern("yyy/MM/dd").format(localDate));

}
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;

public class AddDatePickertoUI extends Application {


private Stage stage;
private DatePicker checkInDatePicker;
public static void main(String[] args) {
Locale.setDefault(Locale.US);
launch(args);
}
@Override
public void start(Stage stage) {
this.stage = stage;
stage.setTitle("DatePickerSample ");
initUI();
stage.show();
}
private void initUI() {
VBox vbox = new VBox(20);
vbox.setStyle("-fx-padding: 10;");
Scene scene = new Scene(vbox, 400, 400);
stage.setScene(scene);

checkInDatePicker = new DatePicker();

GridPane gridPane = new GridPane();


gridPane.setHgap(10);
gridPane.setVgap(10);

Label checkInlabel = new Label("Check-In Date:");


gridPane.add(checkInlabel, 0, 0);

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

You might also like