Date
클래스 -> Calendar
클래스 -> java.time
패키지
날짜와 시간을 다룰 목적으로 제공되어온 클래스(Date, Calendar)
Calendar
: 추상클래스, 직접 객체 생성 불가능,
메서드를 통해 완전히 구현된 클래스의 인스턴스를 얻어야 함 -> GregorianCalendar
, BuddhistCalendar
-> getInstance()
: 시스템의 국가와 지역설정을 확인해서 태국인 경우에 BuddhistCalendar
의 인스턴스를 반환하고,그 외에는 GregorianCalendar
의 인스턴스를 반환
Calendar cal = Calendar.getlnstance();
...
Date d = new Date (cal.getTimelnMillis()); // Date (long date)
Date d = new Date();
...
Calendar cal = Calendar.getlnstance();
cal.setTime(d);
getInstance()
Calendar today = Calendar.getlnstance();
set()
void set (int field, int value)
void set(int year, int month, int date)
void set(int year, int month, int date, int hourOfDay, int minute)
void set (int year, int month, int date, int hourOfDay, int minute, int second)
int get(int field)
Calendar.YEAR
Calendar.MONTH
: 월(0~11)Calendar.WEEK_OF_YEAR
: 이 달의 몇 째 주인지Calendar.DATE
Calendar.HOUR
: 0~11Calendar.HOUR_OF_DAY
: 0~23boolean isLeapYear(int year)
:매개변수 year가 윤년이면 true를 그렇지 않으면 false를 반환한다.int dayDiff(int y1, int ml, int d1, int y2, int m2, int 62)
: 두 날짜간의 차이를 일단위로 반환한다.int getDayOfWeek (int year, int month, int day)
: 지정한 날짜의 요일을 반환한다.(1~7, 1이 일요일) String convertDayToDate (int day)
: 일단위의 값을 년월일의 형태의 문자열로 변환하여 반환한다. int convertDateToDay (int year, int month, int day)
: 년월일을 입력받아서 일단위로 변환한다.
java.text
패키지
숫자, 날짜, 텍스트 데이 터를 일정한 형식에 맞게 표현할 수 있는 방법
데이터 -> 형식화, 형식화된 데이터 -> 본래 데이터 둘 다 가능
DecimalFormat
인스턴스 생성format()
호출double number = 1234567.89;
DecimalFormat df = new DecimalFormat("#.#E0");
String result = df.format(number);
날짜를 출력하는 방식
날짜 계산 : Date
, Calendar
format(Date d)
를 호출ex)
Date today = new Date();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dcT);
// 오늘 날짜를 yyyy-MM-dd형태로 변환하여 반환한다.
String result = df.format(today);
parse(String source)
: 문자열source -> 날짜Date인스턴스로 변환import java.util.*;
import java.text.*;
class DateFormatEx3 {
public static void main(String[] args) {
DateFormat df = new SimpleDateFormat ("yyyy년 MM월 dd일");
DateFormat df2 = new SimpleDateFormat("yyyy/MM/dd");
try {
Date d = df.parse ("2015년 11월 23일"); // ☑️
System.out.println(df2.format(d);
} catch(Exception e) {} } // main
}
SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat yy년 MMM dd일 E요일");
SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS”;
SimpleDateFormat("yyyy-MM-dd hh:mm:ss a”);
특정 범위에 속하는 값을 문자열로 변환
ex) 점수 -> 등급
: D
: C
: B
: A
import java.text.*;
class ChoiceFormatEx1 {
public static void main (String[] args) {
double [] limits = {60, 70, 80, 90); // 범위의 경계값
//1imits, grades간의 순서와 개수를 맞추어야 한다.
String[l grades = ("D", "C", "B", "A"); // 치환할 문자열
int [] scores = { 100, 95, 88, 70, 52, 60, 701;
ChoiceFormat form = new ChoiceFormat (limits, grades); //☑️
for (int i=0;i<scores.length;i++) {
System.out.printIn (scores [il+":"+form.format (scores [i]));
}
}
}
limit#value
String pattern = "60#D|70#이 80<B|90#A";
int[] scores = { 91, 90, 80, 88, 70, 52, 60};
91 : A
90 : A
80 : C
88 : B
70 : C
52 : D
60 : D
- 데이터를 정해진 양식에 맞게 출력
- 하나의 데이터를 다양한 양식으로 출력
- 지정된 양식에서 필요한 데이터만 추출(== SimpleDateFormat의 parse)
import java.text.*
class MessageFormatEx2 {
public static void main (String[] args) {
String tableName = "CUST INFO";
String msg = "INSERT. INTO "+ tableName+" VALUES (''(0)'', ''(1)'`, (2)``, ''(3)'');";
Object [] [] arguments = {
{"이자바", " 02-123-1234", "27", "07-09"),
{"김프로", " 032-333-1234", "33", "10-07"},
}:
for (int i=0; i < arguments.length; i++){
String result = MessageFormat. format(msg, arguments[i]); //☑️
System.out.printIn (result);
}
}
}
데이터 추출
import java.text.*;
class MessageFormatEx3 {
public static void main(String[] args) throws Exception {
String[] data = {
"INSERT INTO CUST_INFO VALUES ('이자바','02-123-1234', 27, '07-09');",
"INSERT INTO CUST一INFO VALUES ('김프로','032-333-1234', 33, '10-07');"
String pattern = "INSERT INTO CUST_INFO VALUES ({0},{1},{2},{3});";
MessageFormat mf = new MessageFormat(pattern); // ☑️
for(int i=0; i < data.length;i++) {
Object[] objs = mf.parse(data[i]); // ☑️
for(int j=0; j < objs.length; j++) {
System.out.print(objs[j] +",");
}
System.out.println();
}
}
import java.util.*;
import java.text.*;
import java.io.*;
class MessageFormatEx4 {
public static void main(String[] args) throws Exception {
String tableName = "CUST_INFO";
String fileName = "data4.txt";
String msg = "INSERT INTO "+ tableName+ ” VALUES ({0},{1},{2},{3});";
Scanner s = new Scanner(new File(fileName));
String pattern = "{0},{1},{2},{3}";
MessageFormat mf = new MessageFormat(pattern); //☑️
while(s.hasNextLine()) {
String line = s.nextLine();
Object[] objs = mf.parse(line); //☑️
System.out.println(MessageFormat.format(msg, objs));
}
}
s.close();
} // main
LocalDate
클래스LocalTime
클래스LocalDateTime
클래스ZonedDateTime
클래스Period
클래스Duration
클래스TemporalAdjusters
클래스now()
: 현재 날짜, 시간
of()
: 날짜/시간 지정
LocalDate date = LocalDate.nowf) ; // 2015-11-23
LocalTime time = LocalTime.now(); // 21:54:01.875
LocalDateTime dateTime = LocalDateTime.now();//2015-11-23TI1:54:01.875
ZonedDateTime dateTimelnKr = ZonedDateTime.now();
// 2015-ll-23T21:54:01.875+09:00[Asia/Seoul]
get()
/getXXX()
: 날짜와 시간에서 특정 필드의 값만을 얻을 때plus() 또는 minus()
: 특정 날짜와 시간에서 지정된 단위의 값을 더하거나 뻘 때with()
, plus()
, minus()
isAfter()
, isBefore()
, isEqual()
에포크 타임(EPOCH TIME, 1970-01-01 00:00:00 UTC)부터 경과된 시간 을 나노초 단위로 표현한 것
3.2 LocalDate, LocalTime
3.3 Instant
3.4 LocalDateTime, ZonedDateTime
3.5 TemporalAdiusters
3.6 Period와 Duration
3.7 파싱과 포맷