마지막날 구하기 등등
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.Date;
public class LocalDateTimeExc {
public static void main(String[] args) {
final int[] TIME_UNIT = {3600, 60, 1};
final String[] TIME_NAME = {"시간", "분", "초"};
LocalDateTime ldt = LocalDateTime.now();
System.out.println(ldt.getDayOfMonth());
System.out.println(ldt.getMonth());
LocalTime lt = LocalTime.now();
System.out.println(lt);
LocalDate ld = LocalDate.now();
System.out.println(ld);
Date d1 = new Date();
long time = d1.getTime();
System.out.println("현재 밀리초 : "+time);
LocalDateTime ldt1 = LocalDateTime.of(2002, 3, 1, 4, 9, 22);
LocalDateTime ldt2 = LocalDateTime.of(2002, 3, 2, 1, 3, 2);
LocalDateTime ldt3 = LocalDateTime.now()
.with(LocalDateTime.of(
2002,3,22,22,33,22
));
System.out.println("getDayOfWeek() "+ldt1.getDayOfWeek());
System.out.println("getMonth() "+ldt1.getMonth());
LocalDate start = LocalDate.from(ldt1.withDayOfMonth(1));
System.out.println(ldt1.withDayOfMonth(start.lengthOfMonth()));
LocalDateTime ldt4 = LocalDate.now().atTime(6, 0);
ZonedDateTime zdt = ldt1.atZone(ZoneId.of("Asia/Seoul"));
long millis = zdt.toInstant().toEpochMilli();
System.out.println("밀리초 with ZoneID: "+millis);
System.out.println("ldt " + LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant().toEpochMilli());
System.out.println("ctm " + System.currentTimeMillis());
LocalDateTime date = LocalDateTime.of(1977, 1, 1, 0, 0);
long millisNoZoneId = date.toInstant(ZoneOffset.ofTotalSeconds(0)).toEpochMilli();
System.out.println("밀리초 without ZoneID: "+millisNoZoneId);
long difference = (ldt2.toInstant(ZoneOffset.ofTotalSeconds(0)).toEpochMilli() - ldt1.toInstant(ZoneOffset.ofTotalSeconds(0)).toEpochMilli())/1000 ;
System.out.println("두 시간 간의 차이 나는 초는 "+difference);
StringBuilder tmp = new StringBuilder();
for (int i = 0; i < TIME_UNIT.length ; i++) {
tmp.append(difference / TIME_UNIT[i]).append(TIME_NAME[i]).append(" ");
difference %= TIME_UNIT[i];
}
System.out.println("시 분 초로 변환 하면 "+ tmp.toString().trim());
// 해당 월의 마지막 날 구하기
LocalDate monthStart = LocalDate.of(2002, 4, 1);
LocalDate monthEnd = monthStart.plusDays(monthStart.lengthOfMonth() -1);
System.out.println("해당 월의 마지막 날 : "+monthEnd.getDayOfMonth());
LocalDate lastDate = LocalDate.now().with(TemporalAdjusters.lastDayOfMonth());
String lastDateString = lastDate.format(DateTimeFormatter.ISO_DATE);
System.out.println(lastDateString); // 2021-06-30
System.out.println(lastDate.getDayOfMonth());
LocalDate ld3 = LocalDate.of(2002, 4, 3);
LocalDate lastdDate1 = ld3.with(TemporalAdjusters.lastDayOfMonth());
System.out.println("날짜 지정후 마지막 날 구하기"+lastdDate1);
LocalDateTime ld4 = LocalDateTime.of(2002, 4, 3, 22,3,2);
LocalDateTime lastdDate2 = ld4.with(TemporalAdjusters.lastDayOfMonth());
System.out.println("날짜 지정후 마지막 날 구하기"+lastdDate2);
LocalDate ld1 = LocalDate.now();
LocalDate lastDay = LocalDate.ofEpochDay(ld1.lengthOfMonth() -1);
System.out.println(lastDay);
// =====================================
LocalDateTime now = LocalDateTime.now();
System.out.println("Now is " + now);
LocalDateTime now2 = LocalDateTime.of(LocalDate.now(), LocalTime.now());
System.out.println("Now is " + now2);
LocalDateTime y2k = LocalDateTime.parse("1999-12-31T23:59:59.999");
System.out.println("Y2K is " + y2k);
LocalDateTime dateOfBirth = LocalDateTime.of(1982, 7, 13, 14, 25, 0);
System.out.println("My date of birth is " + dateOfBirth);
LocalDateTime dateOfBirth2 = Year.of(1982).atMonth(7).atDay(13).atTime(14, 25);
System.out.println("My date of birth is " + dateOfBirth2);
LocalDateTime nowLdt = LocalDateTime.now(); // 현재시간
LocalDateTime threeYearsAfter = nowLdt.plusYears(3); // 3년 뒤 - now는 계속 현재시간
LocalDateTime twoDaysAgo = nowLdt.minusDays(2); // 2일 전
LocalDateTime twoDaysAndThreeHoursAgo = nowLdt.minusDays(2).minusHours(3); // 2일 3시간 전
}
}
'kotlin > java' 카테고리의 다른 글
Set, HashSet, equals, hashCode override 예제 (0) | 2023.01.06 |
---|---|
무작위로 4개의 char를 추출해서 정렬 (0) | 2023.01.06 |
Comparator 와 Comparable (0) | 2023.01.06 |
Arrays class (0) | 2023.01.06 |
Stack & Queue (0) | 2023.01.06 |