현재 시간 구하기

1. 현재 시간 구하기

// format 객체로 원하는 형태로 형식 정의
SimpleDateFormat format1 = new SimpleDateFormat ( "yyyy-MM-dd HH:mm:ss");

// date 객체 또는 Calendar 객체 사용 가능.
Date dt = new Date();
// Calendar c = Calendar.getInstance();

String dt = format1.format(dt);

System.out.println(time1);

2. 년/월/일 따로 구하기

Calendar cal = Calendar.getInstance();

// 년
int y = cal.get(Calendar.YEAR);
// 월
int m = cal.get(Calendar.MONTH) + 1;
// 일
int d = cal.get(Calendar.DAY_OF_MONTH);
// 시간
int h = cal.get(Calendar.HOUR_OF_DAY);
// 분
int m = cal.get(Calendar.MINUTE);
// 초
int s = cal.get(Calendar.SECOND);
// 따로따로 구하여 연산 가능.

// Integer.toString(int y); : int -> String
// Integer.parseInt(String y); : String -> int

© 2024. Chiptune93 All rights reserved.