ライブラリ
よく利用される機能を切り出して、再利用しやすいようにまとめたもの。ライブラリの利用により、機能を1から作る必要がなくなり、効率的に開発を行うことができる。
一般的に、Javaのライブラリは「JARファイル(.jar)」で提供される。
jarファイルは、コンパイル時に生成される複数の「classファイル」をまとめたもの。
使い方
利用時には、ライブラリ内の汎用的なクラスを呼び出すだけで、そのクラス機能を利用することができる。
日付を扱うことができるクラスライブラリCalendarを使ってみる。
使う時はimportする。
import java.util.Calendar; //クラスをimport
public class Main {
public static void main(String[] args) throws Exception {
Calendar now = Calendar.getInstance() ;
int year = now.get(Calendar.YEAR);
int month = now.get(Calendar.MONTH);
int date = now.get(Calendar.DATE);
int hour = now.get(Calendar.HOUR);
int minute = now.get(Calendar.MINUTE);
int second = now.get(Calendar.SECOND);
System.out.print(year + "年");
System.out.print(month + "月");
System.out.print(date + "日");
System.out.print(hour + "時");
System.out.print(minute + "分");
System.out.println(second + "秒");
}
}
結果
2023年6月15日23時09分50秒