문서의 선택한 두 판 사이의 차이를 보여줍니다.
양쪽 이전 판 이전 판 다음 판 | 이전 판 | ||
프로그램:java:ㄴhomework:book_source:ch12 [2022/01/05 11:28] clayeryan@gmail.com [Calendar 클래스] |
프로그램:java:ㄴhomework:book_source:ch12 [2023/07/13 17:29] (현재) clayeryan@gmail.com ↷ 문서가 programmer:java:ㄴhomework:book_source:ch12에서 프로그램:java:ㄴhomework:book_source:ch12(으)로 이동되었습니다 |
||
---|---|---|---|
줄 1: | 줄 1: | ||
- | ====== | + | ====== |
===== java.lang 패키지 ===== | ===== java.lang 패키지 ===== | ||
줄 1116: | 줄 1116: | ||
==== Array 클래스 ==== | ==== Array 클래스 ==== | ||
+ | <code java> | ||
+ | package chapter12; | ||
+ | |||
+ | import java.util.Arrays; | ||
+ | |||
+ | public class ArraysEx { | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | |||
+ | String[] arr = {" | ||
+ | Arrays.fill(arr, | ||
+ | for (String n : arr) System.out.print(n + "," | ||
+ | System.out.println(); | ||
+ | Arrays.fill(arr, | ||
+ | for (String n : arr) System.out.print(n + "," | ||
+ | |||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | </ | ||
===== java.text 패키지 ===== | ===== java.text 패키지 ===== | ||
+ | |||
+ | ==== DecimalFormat 클래스 ==== | ||
+ | |||
+ | **숫자 포맷을 지정할 수 있는 클래스** | ||
+ | <code java> | ||
+ | package chapter12; | ||
+ | |||
+ | import java.text.DecimalFormat; | ||
+ | |||
+ | public class DecimalFormatEx { | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | |||
+ | double[] scores = {90.555, 80.6666, 70.77777, 60.666666, 50.5}; | ||
+ | |||
+ | DecimalFormat df = new DecimalFormat("#,### | ||
+ | |||
+ | for (int i=0; i< | ||
+ | System.out.println(df.format(scores[i])); | ||
+ | } | ||
+ | |||
+ | |||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | <code java> | ||
+ | package chapter12; | ||
+ | |||
+ | import java.text.DecimalFormat; | ||
+ | |||
+ | public class DecimalFormatEx2 { | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | |||
+ | DecimalFormat df1 = new DecimalFormat("#,### | ||
+ | DecimalFormat df2 = new DecimalFormat(" | ||
+ | |||
+ | System.out.println(df1.format(5500)); | ||
+ | System.out.println(df2.format(5500)); | ||
+ | |||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
==== Simple date format ==== | ==== Simple date format ==== | ||
+ | <code java> | ||
+ | package chapter12; | ||
+ | |||
+ | import java.text.SimpleDateFormat; | ||
+ | import java.util.Date; | ||
+ | |||
+ | public class SimpleDateFormatEx { | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | |||
+ | Date now = new Date(); | ||
+ | System.out.println(now); | ||
+ | SimpleDateFormat sf = new SimpleDateFormat(" | ||
+ | System.out.println(sf.format(now)); | ||
+ | |||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | <code java> | ||
+ | package chapter12; | ||
+ | |||
+ | import java.text.ParseException; | ||
+ | import java.text.SimpleDateFormat; | ||
+ | import java.util.Date; | ||
+ | |||
+ | public class SimpleDateFormatEx3 { | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | |||
+ | SimpleDateFormat sdf = new SimpleDateFormat(" | ||
+ | String strDate = " | ||
+ | |||
+ | Date d = null; | ||
+ | try { | ||
+ | d = sdf.parse(strDate); | ||
+ | } catch (ParseException e) { | ||
+ | e.printStackTrace(); | ||
+ | } | ||
+ | System.out.println(d); | ||
+ | |||
+ | SimpleDateFormat sf2 = new SimpleDateFormat(" | ||
+ | System.out.println(sf2.format(d)); | ||
+ | |||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | <code java> | ||
+ | package chapter12; | ||
+ | |||
+ | import java.text.SimpleDateFormat; | ||
+ | import java.util.Calendar; | ||
+ | import java.util.Date; | ||
+ | |||
+ | public class SimpleDateFormatEx2 { | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | |||
+ | // Calendar 와 Date 간의 변환은 다 음과 같이 한다 • | ||
+ | Calendar cal = Calendar.getInstance() ; | ||
+ | cal.set(2020, | ||
+ | Date day = cal.getTime() ; | ||
+ | SimpleDateFormat sdf1 , sdf2 , sdf3 , sdf4 ; | ||
+ | sdf1 = new SimpleDateFormat (" | ||
+ | sdf2 = new SimpleDateFormat (" | ||
+ | sdf3 = new SimpleDateFormat (" | ||
+ | sdf4 = new SimpleDateFormat (" | ||
+ | System.out.println(sdf1.format(day)); | ||
+ | System.out.println(sdf2.format(day)); | ||
+ | System.out.println(sdf3.format(day)) ; | ||
+ | System.out.println(sdf4.format(day)); | ||
+ | |||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | </ | ||
===== java.util.regex 패키지 ===== | ===== java.util.regex 패키지 ===== | ||
+ | |||
+ | **정규표현식을 사용하기 위한 패키지** | ||
+ | |||
+ | <code java> | ||
+ | package chapter12; | ||
+ | |||
+ | import java.util.regex.Matcher; | ||
+ | import java.util.regex.Pattern; | ||
+ | |||
+ | public class ReEx { | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | |||
+ | // 소문자 b로 시작하는 | ||
+ | Pattern p = Pattern.compile(" | ||
+ | Matcher m; | ||
+ | |||
+ | // 문자열 bat 확인 | ||
+ | m = p.matcher(" | ||
+ | System.out.println(" | ||
+ | |||
+ | // 문자열 cat 확인 | ||
+ | m = p.matcher(" | ||
+ | System.out.println(" | ||
+ | |||
+ | // 문자열 bed 확인 | ||
+ | m = p.matcher(" | ||
+ | System.out.println(" | ||
+ | |||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | |1. Pattern 클래스의 static 메서드 copile에 정규표현식을 매개변수로 넣고 객체 생성 Pattern p = Pattern.copile(" | ||
+ | |2. Matcher 클래스를 이용해 생성한 패턴 책체의 matcher() 메서드의 매개변수로 비교할 대상 문자열을 넣어 Matcher 객체 생성| | ||
+ | | | ||
+ | Matcher m; | ||
+ | m= p.matcher(" | ||
+ | | | ||
+ | |3. Matcher 객체에 amtchers() 메서드를 호출해 매칭이 되었는지(true) 안 되었는지(false) 판단 if (m.matchers()) {...}| | ||
+ | |||
+ | |||
+ | |||
==== 정규 표현식에 사용되는 문자 ==== | ==== 정규 표현식에 사용되는 문자 ==== | ||
==== 정규 표현식으로 문자열검증 ==== | ==== 정규 표현식으로 문자열검증 ==== | ||
+ | **Pattern 클래스의 static 메서드 matches() 메서드 사용** | ||
+ | <code java> | ||
+ | package chapter12; | ||
+ | import java.util.regex.Matcher; | ||
+ | import java.util.regex.Pattern; | ||
+ | public class ReEx2 { | ||
+ | public static void main(String[] args) { | ||
+ | |||
+ | String[] patterns = {" | ||
+ | " | ||
+ | String[] datas = {" | ||
+ | |||
+ | for(String d : datas) { | ||
+ | System.out.print(d+" | ||
+ | for(String p : patterns) { | ||
+ | Pattern pattern = Pattern.compile(p); | ||
+ | Matcher m = pattern.matcher(d); | ||
+ | if(m.matches()) { | ||
+ | System.out.print(p+", | ||
+ | } | ||
+ | } | ||
+ | System.out.println(); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | <code java> | ||
+ | package chapter12; | ||
+ | |||
+ | import java.util.regex.Matcher; | ||
+ | import java.util.regex.Pattern; | ||
+ | |||
+ | public class ReEx3 { | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | |||
+ | String source = " | ||
+ | + " | ||
+ | String telpattern = " | ||
+ | String emailpattern = " | ||
+ | String accountpattern = " | ||
+ | |||
+ | Pattern p = Pattern.compile(telpattern); | ||
+ | Matcher m = p.matcher(source); | ||
+ | |||
+ | System.out.println(" | ||
+ | while(m.find()) { // 지정된 패턴 맞는 문자열을 검색 | ||
+ | System.out.println(m.group() + " : " | ||
+ | + m.group(1) + "," | ||
+ | } | ||
+ | |||
+ | p = Pattern.compile(emailpattern); | ||
+ | m = p.matcher(source); | ||
+ | System.out.println(" | ||
+ | while(m.find()) { // 지정된 패턴 맞는 문자열을 검색 | ||
+ | System.out.println(m.group() + " : " | ||
+ | + m.group(1) + "," | ||
+ | } | ||
+ | |||
+ | p = Pattern.compile(accountpattern); | ||
+ | m = p.matcher(source); | ||
+ | System.out.println(" | ||
+ | while(m.find()) { // 지정된 패턴 맞는 문자열을 검색 | ||
+ | System.out.println(m.group() + " : " | ||
+ | + m.group(1) + "," | ||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | </ |