Language/JAVA

[JAVA] 유틸리티 클래스 (Utility Class)

gangintheremark 2023. 7. 24. 14:51
728x90

String 클래스

String은 한 번 생성하면 변경되지 않는다. 메서드 이용 시 새로운 문자열로 반환한다.

메서드 예시 설명
💡 length s.length() 문자열의 길이 반환
💡 toUpperCase s.toUpperCase() 문자열을 대문자로 변환
💡 toLowerCase s.toLowerCase() 문자열을 소문자로 변환
💡 contains s.contains(s1) 해당 문자열이 포함된다면 true
💡 indexOf s.indexOf(s1) 해당 문자열과 처음으로 일치하는 위치. 포함하지 않는다면 -1 반환
💡 lastIndexOf s.lastIndexOf(s1) 해당 문자열과 마지막에 일치하는 위치
💡 startsWith s.startsWith(s1) 해당 문자열로 시작하면 true
💡 endsWith s.endsWith(s1) 해당 문자열로 끝나면 true
💡 replace s.replace(s1, s2) s1을 s2로 변환
💡 replaceAll s.replaceAll("[^A-Z]", "") A-Z가 아닌 것은 전무 replace
💡 substring s.substring(n) index기준 n번째 부터 시작 (이전 내용삭제)
s.substring(a) 문자열 a부터 시작 (이전 내용삭제)
s.substring(a, b) 시작위치 부터 끝 위치 직전까지
💡 trim s.trim() 앞뒤 공백 모두 제거
💡 concat s1.concat(s2) s1, s2 결합
💡 equals s1.equals(s2) 문자열 내용이 같으면 true
💡 equalsIgnoreCase s1.equalsIgnoreCase(s2) 대소문자 구분없이 같으면 true
💡 charAt s.charAt(n) index기준 n번째 문자 반환

StringBuilder 클래스

StringBuilderStringBuffer 동적으로 크기가 변경된다.

메서드 예시 설명
💡 length sb.length() 문자열의 길이 반환
💡 append sb.append(s) 버퍼에 문자열을 추가
💡 insert sb.insert(n, s) index기준 n번째에 s 삽입
💡 delete sb.delete(0, 5) 0~5 까지 삭제
💡 reverse sb.reverse() 문자열 거꾸로 뒤집기
💡 toString sb.toString String 문자열로 변환

Random 클래스

임의의 랜덤값을 발생시키는 유틸리티 클래스이다.

메서드 설명
💡 nextInt 임의의 정수값
💡 nextInt(n) 0부터 n-1 까지의 임의의 정수값
💡 nextBoolean 임의의 Boolean 값
💡 nextFloat 0.0과 1.0 사이의 임의의 float 값
💡 nextDouble 0.0과 1.0 사이의 임의의 double 값

날짜 데이터 & SimpleDateFormat 클래스

Date 클래스를 이용한 날짜 데이터 값을 특정 포맷으로 출력할 때 사용되는 유틸리티 클래스이다.

형식문자 기능
y 년도 표시
m 월 표시
d 일 표시
H 시간(0~23) 표시
h 시간(0~12) 표시
m 분 표시
s 초 표시
a AM/PM 표시
import java.util.Date;
import java.text.SimpleDateFormat;

public class DateTest {
public static void main(String[] args) throws Exception {

        // 날짜 데이터
        // java.text.SimpleDateFormat 클래스와 병행해서 사용한다.
        Date d = new Date();
        System.out.println(d);
        // Fri Jul 21 15:23:12 KST 2023 =====> 특정포맷을 가진 문자열로 변경

        // 💡 Date 타입 --> String
        // SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
        // SimpleDateFormat sdf = new SimpleDateFormat("yyyy년MM월dd일");
        // SimpleDateFormat sdf = new SimpleDateFormat("yyyy년MM월dd일, HH:mm:ss");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
        String result = sdf.format(d);
        System.out.println(result);


        // 💡 String --> Date 
        String s = "2023년05월13일";
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy년MM월dd일");
        Date d2 = sdf2.parse(s);
        System.out.println(d2);

        Calendar cal = Calendar.getInstance();
        cal.setTime(d2);
    }
}

Calendar 클래스

날짜 데이터인 Calendar 클래스는 추상클래스 이기 때문에 인스턴스화 new 불가

import java.util.Calendar;

public class CalendarTest {
        // 날짜 데이터
         Calendar cal = Calendar.getInstance();

         int year = cal.get(Calendar.YEAR);
         int month = cal.get(Calendar.MONTH)+1; // 1월:0  12월:11
         int day = cal.get(Calendar.DAY_OF_MONTH);
         int hour = cal.get(Calendar.HOUR_OF_DAY);
         int minute = cal.get(Calendar.MINUTE);
         int second = cal.get(Calendar.SECOND);

         System.out.println("년:" + year);
         System.out.println("월:" + month);
         System.out.println("일:" + day);
         System.out.println("시:" + hour);
         System.out.println("분:" + minute);
         System.out.println("초:" + second);

         // 특정 날짜 설정

         cal2.set(2004, 1, 4);  // 0 -based ~ 11
         System.out.println("년:" + cal2.get(Calendar.YEAR));
         System.out.println("월:" + (cal2.get(Calendar.MONTH)+1));
         System.out.println("일:" + cal2.get(Calendar.DAY_OF_MONTH));
    }
}
728x90