Java - java.base module

원종서·2023년 2월 27일
0

java

목록 보기
5/9

2023-02-27

레코드

DTO 를 작성할 때 반복적으로 상용되는 코드를 줄이기 위해 자바14버전 부터 레코드가 도입됨.

  1. private 필드
  2. 필드 명으로 private 필드 참조 가능함수
  3. constructor
  4. hashCode
  5. equals
  6. toString

메서드 자동 생성.

public record Member(String id, String name , int age) {
 // private String id , name;
 // private int age;
 // String name() {return this.name; }
 // Member(String id, String name, int age) {...}
 // hashCode() {...};
 // equals() {...};
 // toString() {...};
}

System Class

자바 프로그램은 자바 가성 머신 위에서 실행하기 때문에 운영체제의 기능을 자바 코드로 구현하기 어렵다.
그래서 java.lang.System 을 이용하면 운영체제의 일부 기능일 이요할 수 있다.

// 프로그램 처리 시간을 측정하는 데 주로 사용되는 System.nanoTime();

public static void main(String[] args){
	long time1 = System.nanoTime();
    
    int sum = 0;
    
    for(int i = 1; i<= 1000000000 ;i++){
    	sum += 1;
    }
    
    long time2 = System.nanoTime();
    
    System.out.println(time2 -time1);
}

String class

StringBuilder

String은 내부 문자열을 수정할 수 없다.
기존 String 객체에 + 연산을 하면 ,
사용되던 String객체는 버려지고 새로운 String객체 + '새로운 문자열' 의 객체가 생성된다.

그러므로 잦은 문자열 변경 작업을 한다면 String 보다는 StringBuilder 를 사용하는 것이 좋다.

StringBuilder 는 내부 부퍼에 문자열을 저장해두고, 그 안에서 추가 수정 삭제를 하도록 설계됨.

	String data = new StringBuilder();
    
    data.append("DEF")  // DEF
    	.insert(0,"ABC") // ABCDEF
        .delete(3,4)   // ABCEF
        .toString();

StringTokenizer

문자열이 구분자로 연결되어 있을 경우, 구분자를 기준으로 문자열을 분리하기 용이


String data = "A/B/C"

StringTokenizer st = new StringTokenizer(data, "/");

while(st.hadMoreTokens()){
	String token = st.nextToken();
    
    System.out.print(token);  // ABC
}

Wrapper Class

primary type -> wrapper class == boxing
wrapper class -> primary type == unboxing

Math Class

Math Class Method
int v1 = Math.abs(-5); // 5
double v1 = Math.ceil(5.3); // 6.0
double v1 = Math.floor(5.2); // 5.0
int v1 = Math.max(5,9); // 9
int v1 = Math.min(5,9); // 5
double v1 = Math.random(); // 0.0 <= 1.0
int v1 = Math.round(5.0); // 5
## Date and Time Class
Data and Time Class
Date 날자 정보를 전달하기 위해 사용/td>
Calendar 다양한시간대 별로날짜와시간을얻을때 사용
LocalDateTime 날자와 시간을 조작할 때 사용
### Date ```java public static void main(String[] args){ Date now = new Date(); System.out.println(now.toString()); // Sun Nov 19:29:51 KST 2021... SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss"); String strNow2 = stf.format(now); System.out.println(strNow2); // 2021.11.28 19.29.51 } ``` ### Calendar ```java Calendar now = Calendar.getInstance(); int year = now.get(Calendar.YEAR); int month = now.get(Calendar.MONTH) + 1 ; int day = now.get(Calendar.DAY_OF_MONTH); int week = now.get(Calendar.DAY_OF_WEEK); int amPm = now.get(Calendar.AM_PM); int hour = now.get(Calendar.HOUR); int minute = now.get(Calendar.MINUTE); int second = now.get(Calendar.SECOND); ``` ### LocalDateTime 날짜와 시간을 조작하는데 용이한 클래스 ```java LocalDateTime now = LocalDateTime.now(); DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy.MM.dd a HH:mm:ss"); System.out.println(now.format(dtf)); // 2021.11.28 pm 21:13:37 LocalDateTime result2 = now.plusYears(1); System.out.println(result2.format(dtf)); // 2022.11.28 pm 21:13:37 LocalDateTime result3 = now.minusMonths(2); System.out.println(result3.format(dtf)); // 2021.09.28 pm 21:13:37 LocalDateTime result4 = now.plusDays(7); System.out.println(result4.format(dtf)); // 2021.12.05 pm 21:13:37 ```

날자와 시간 비교

isAter
isBefore
isEqual
until

public static void main(String[] args){
	DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy.MM.dd a HH:mm:ss");
  
  	LocalDateTime startDateTime = LocalDateTime.of(2021,1,1,0,0,0);
      
      LocalDateTime endDateTime = LocalDateTime.of(2021,12,31,0,0,0);
      
      if(startDateTime.isBefore(endDateTime) {
      	System.out.println("True"); // True
      }
      
      
      long remainYear = startDateTime.until(endDateTime, ChronoUnit.YEARS);
      long remainMonth = startDateTime.until(endDateTime, ChronoUnit.MONTHS);
      long remainDay = startDateTime.until(endDateTime, ChronoUnit.DAYS);
}

0개의 댓글