Calendar
// 에러, 추상클래스는 인스턴스 생성 X
Calendar cal = new Calendar();
// getInstance() 메서드는 Calendar 클래스를 구현한 클래스의 인스턴스 반환
Calendar cal = Calendar.getInstance();
Date와 Calendar간의 변환
// Calendar를 Date로 변환
Calendar cal = Calendar.getInstance();
...
Date d = new Date(cal.getTimeMillis());
// Date를 Calendar로 변환
Date d = new Date();
...
Calendar cal = Calendar.getInstance();
cal.setTime(d);
/*
0 : 빈 자리를 0으로 채움
# : 빈 자리 출력 X
. : 소수점
, : 천 단위 구분자
% : 백분율
*/
double number = 1234567.89;
DecimalFormat df = new DecimalFormat("#.#E0");
String result = df.format(number);
/*
y : 연도
M : 월
W : 월의 몇 번째 주
d : 월의 몇 번째 일
E : 요일
a : 오전/오후
H : 시간 (0 ~ 23)
h : 시간 (1 ~ 12)
m : 분
s : 초
z : Time zone
*/
Date today = new Date();
SimpleDateFormat df = new SimpleDateFormat("yyy-MM-dd");
// 오늘 날짜를 yyyy-mm-dd 형식으로 변환하여 반환
String result = df.format(today);
import java.text.*;
class ChoiceFormatEx1 {
public static void main(String[] args) {
double[] limits = {60, 70, 80, 90};
String[] grades = {"D", "C", "B", "A"};
int[] scores = {100, 95, 88, 70, 52, 60, 75};
// 점수 -> 학점 변환
ChoiceFormat form = new ChoiceFormat(limits, grades);
for (int i = 0; i < scores.length; i++) {
System.out.println(scores[i] + ":" + form.format(scores[i]));
}
}
}