Date today = new Date();
System.out.println(today.getDate());
SimpleDateFormat date = new SimpleDateFormat("yyyy/MM/dd");
SimpleDateFormat time = new SimpleDateFormat("hh:mm:ss a");
System.out.println(date.format(today));
System.out.println(time.format(today));SimpleDateFormat : 날짜 보여주는 형식 지정. 월은 mm이 아니라 MM이라고 써야함. time에 a는 오전/오후 이다. Calender cal = new Calender(); -> 이거 안됨.Calender cal = Calender.getInstance();
//Calender cal = new GregorianCalendar();
System.out.println(cal);.getInstance()가 사용자의 접속한 시스템의 지역을 확인해서 달력 클래스들 중에서 하나를 반환해온다. (우리는 GregorianCalendar, 태국은 BuddhistCalendar)cal.get(Calendar.YEAR) : 년cal.get(Calendar.MONTH) + 1 : 월cal.get(Calendar.DAY_OF_MONTH) : 일cal.get(Calendar.DAY_OF_WEEK) : 요일 {"", "일", "월", "화", "수", "목", "금", "토"}. 맨 앞칸은 비어있다. cal.get(Calendar.HOUR) : 시(12시간제)cal.get(Calendar.HOUR_OF_DAY) : 시(24시간제)cal.get(Calendar.MINUTE) : 분cal.get(Calendar.SECOND) : 초cal.get(Calendar.MILLISECOND) : 밀리초cal.set(2023, 1, 1); : set 이용해서 특정 시간대로 설정. (년, 월, 일)long diff = (cal.getTimeInMillis() - cal2.getTimeInMillis()); : getTimeInMillis()는 밀리초라서 결과도 밀리초로 나온다.System.out.println((diff / 1000 / 60 / 60 / 24) + "일 지남"); 밀리초 > 초 > 분 > 시 > 일 순서로 나누기. Date d = new Date();
Calender c = Calendar.getInstance();
c.setTime(d);Date d = new Date(c.getTimeInMillis());LocalDate currDate = LocalDate.now(); : 지금 날짜LocalDate prevDate = LocalDate.of(2023, 9, 19) : 지정 날짜 (년, 월, 일)LocalDate currDate = LocalTime.now(); : 지금 시간LocalDate prevDate = LocalTime.of(2, 9, 19) : 지정 시간 (시, 분, 초)LocalDateTime currDateTime = LocalDateTime.now(); : 지금 날짜 시간LocalDate curDate = LocalDate.now();
LocalTime curTime = LocalTime.now();
LocalDateTime targetDateTime = LocalDateTime.of(curDate, curTime);-> 특정 시간대 구하기LocalDate dt1 = LocalDate.now();
LocalDate dt2 = LocalDate.of(2023, 9, 19);
Period pe = Period.between(dt1, dt2);
System.out.println(pe.get(ChronoUnit.YEARS));-> Period에서 필드값을 뽑을 때는 get(ChronuUnit.필드명) 써야한다. LocalTime t1 = LocalTime.now();
LocalTime t2 = LocalTime.of(20, 15, 17);
Duration du = Duration.between(t1, t2);Duration에서도 필드값 뽑을 때는 get(ChronoUnit.필드명) 써야함System.out.println(du.get(ChronoUnit.SECONDS));
System.out.println(du.get(ChronoUnit.NANOS));SECONDS랑 NANOS 밖에 없기 때문에 그냥 Duration을 LocalTime으로 변환한 후, LocalTime의 get 메서드 이용하면 편함. LocalTime localTime = LocalTime.of(0, 0).plusSeconds(du.getSeconds());