nme.kr

차이

문서의 선택한 두 판 사이의 차이를 보여줍니다.

차이 보기로 링크

양쪽 이전 판 이전 판
다음 판
이전 판
프로그램:java:ㄴhomework:book_source:ch12 [2022/01/05 11:19]
clayeryan@gmail.com [Scanner 클래스]
프로그램: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:
-====== Chaptor12 기본 API ======+====== Ch.12 기본 API ======
  
 ===== java.lang 패키지 ===== ===== java.lang 패키지 =====
줄 887: 줄 887:
  }  }
  System.out.println("총 입력된 회원수 :"+cnt);  System.out.println("총 입력된 회원수 :"+cnt);
 +
 + }
 +
 +}
 +
 +</code>
 +==== Date 클래스 ====
 +
 +<code java>
 +package chapter12;
 +
 +import java.text.SimpleDateFormat;
 +import java.util.Calendar;
 +import java.util.Date;
 +
 +public class DateEx {
 +
 + public static void main(String[] args) {
 +
 + Date now = new Date();
 + System.out.println(now);
 + SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss E a");
 + System.out.println(sf.format(now));
 +
 + Calendar cal = Calendar.getInstance();
 + Date d = new Date(cal.getTimeInMillis());
  
  }  }
줄 894: 줄 920:
 </code> </code>
 ==== Calendar 클래스 ==== ==== Calendar 클래스 ====
 +
 +**Calendar 객체를 Date로 변환**
 +  Calendar cal = Calendar.getInstance();
 +  Date d = new Date(cal.getTimeInMillis());
 +
 +Date 객체를 Calendar로 변환
 +  Date d = new Date();
 +  Calendar cal = Calendar.getInstance();
 +  cal.setTime(d);
 +  
 +  Calendar cal = Calendar.getInstance();
 +  
 +<code java>
 +package chapter12;
 +
 +import java.util.Calendar;
 +
 +public class CalendarEx {
 +
 + public static void main(String[] args) {
 +
 + Calendar today = Calendar.getInstance();
 +
 + System.out.println("올해 년도 :"+today.get(Calendar.YEAR));
 + System.out.println("이번달 :" + (today.get(Calendar.MONTH)+1));
 + System.out.println("년도기준 몇째주 :"+today.get(Calendar.WEEK_OF_YEAR));
 + System.out.println("월기준 몇째주 :"+today.get(Calendar.WEEK_OF_MONTH));
 + System.out.println("일자 :" + today.get(Calendar.DATE));
 + System.out.println("일자 :" + today.get(Calendar.DAY_OF_MONTH));
 + System.out.println("년도기준날짜 :"+today.get(Calendar.DAY_OF_YEAR));
 + System.out.println("요일 (일)~ 토)):"+today.get(Calendar.DAY_OF_WEEK));
 + System.out.println("월기준몇째요일 :" + today.get(Calendar.DAY_OF_WEEK_IN_MONTH));
 +
 + }
 +
 +}
 +</code>
 +
 +<code java>
 +package chapter12;
 +
 +import java.util.Calendar;
 +
 +public class CalendarEx2 {
 +
 + public static void main(String[] args) {
 +
 + Calendar today = Calendar.getInstance();
 +
 + System.out.println("오전 (0) 오후 (1) :" + today.get(Calendar.AM_PM));
 + System.out.println("시간 (0~11) :" + today.get(Calendar.HOUR));
 + System.out.println("시간 (0~23) :" + today.get(Calendar.HOUR_OF_DAY));
 + System.out.println("분 (0~59) :" + today.get(Calendar.MINUTE));
 + System.out.println("초 (0~59) :" + today.get(Calendar.SECOND));
 + System.out.println("밀리초 (0~999) :" + today.get(Calendar.MILLISECOND));
 + System.out.println("Timezone(12~12):" + today.get(Calendar.ZONE_OFFSET) / (60 * 60 * 1000));
 + System.out.println("이번달의 마지막일자 :" + today.getActualMaximum(Calendar.DATE));
 +
 + Calendar cal = Calendar.getInstance();
 + cal.set(2020, (6-1), 12);
 + System.out.println("날짜 :" + 
 + cal.get(Calendar.YEAR) + "년 " + 
 + (cal.get(Calendar.MONTH) + 1) + "월 " + 
 + cal.get(Calendar.DATE) + "일");
 +
 + }
 +
 +}
 +
 +</code>
 +
 +**Date 클래스 사용**
 +<code java>
 +package chapter12;
 +
 +import java.util.Date;
 +
 +public class CalendarEx3 {
 +
 + public static void main(String[] args) {
 +
 +
 + // 현재일
 + int sYear = 2020;
 + int sMonth = 6;
 + int sDay = 12;
 +
 + // 이전일
 + int eYear = 2020;
 + int eMonth = 6;
 + int eDay = 1;
 +
 + Date sd = new Date();
 + Date ed = new Date();
 +
 + sd.setYear(sYear);
 + sd.setMonth(sMonth-1);
 + sd.setDate(sDay);
 +
 + ed.setYear(eYear);
 + ed.setMonth(eMonth-1);
 + ed.setDate(eDay);
 +
 + long temp = (sd.getTime() - ed.getTime()) / (1000L*60L*60L*24L);
 + int diff = (int)temp;
 +
 + System.out.println(diff + "일 경과");
 +
 + }
 +
 +}
 +
 +</code>
 +
 +**Calendar 클래스 사용**
 +<code java>
 +package chapter12;
 +
 +import java.util.Calendar;
 +
 +public class CalendarEx4 {
 +
 + public static void main(String[] args) {
 +
 +
 + // 현재일
 + int sYear = 2020;
 + int sMonth = 6;
 + int sDay = 12;
 +
 + // 이전일
 + int eYear = 2020;
 + int eMonth = 6;
 + int eDay = 1;
 +
 + Calendar sCal = Calendar.getInstance();
 + Calendar eCal = Calendar.getInstance();
 + sCal.set(sYear, sMonth+1, sDay);
 + eCal.set(eYear, eMonth+1, eDay);
 +
 + long diffSec = (sCal.getTimeInMillis() - eCal.getTimeInMillis()) / 1000;
 +
 + long diffDay = diffSec / (24*60*60);
 +
 + System.out.println(diffDay + "일 경과");
 +
 + }
 +
 +}
 +
 +</code>
 +
 +**GregorianCalendar 클래스 사용**
 +
 +<code java>
 +package chapter12;
 +
 +import java.util.Calendar;
 +import java.util.GregorianCalendar;
 +
 +public class CalendarEx5 {
 +
 + public static void main(String[] args) {
 +
 +
 + // 현재일
 + int sYear = 2020;
 + int sMonth = 6;
 + int sDay = 12;
 +
 + // 이전일
 + int eYear = 2020;
 + int eMonth = 6;
 + int eDay = 1;
 +
 +// Calendar sCal = Calendar.getInstance();
 +// Calendar eCal = Calendar.getInstance();
 +// sCal.set(sYear, sMonth+1, sDay);
 +// eCal.set(eYear, eMonth+1, eDay);
 + Calendar sCal = new GregorianCalendar(sYear, sMonth+1, sDay);
 + Calendar eCal = new GregorianCalendar(eYear, eMonth+1, eDay);
 +
 + long diffSec = (sCal.getTimeInMillis() - eCal.getTimeInMillis()) / 1000;
 +
 + long diffDay = diffSec / (24*60*60);
 +
 + System.out.println(diffDay + "일 경과");
 +
 + }
 +
 +}
 +
 +</code>
  
 ==== 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, 1, 3, "X");
 + for (String n : arr) System.out.print(n + ",");
 +
 + }
 +
 +}
 +
 +</code>
 ===== 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("#,###.00");
 +
 + for (int i=0; i<scores.length; i++) {
 + System.out.println(df.format(scores[i]));
 + }
 +
 +
 + }
 +
 +}
 +
 +</code>
 +
 +<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("000,000");
 +
 + System.out.println(df1.format(5500));
 + System.out.println(df2.format(5500));
 +
 + }
 +
 +}
 +
 +</code>
 +
  
 ==== 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("yyyy MM dd HH:mm:ss E a");
 + System.out.println(sf.format(now));
 +
 + }
 +
 +}
 +
 +</code>
 +
 +<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("yyyy-MM-dd");
 + String strDate = "2020-06-13";
 +
 + Date d = null;
 + try {
 + d = sdf.parse(strDate);
 + } catch (ParseException e) {
 + e.printStackTrace();
 + }
 + System.out.println(d);
 +
 + SimpleDateFormat sf2 = new SimpleDateFormat("yyyy-MM-dd E요일");
 + System.out.println(sf2.format(d));
 +
 + }
 +
 +}
 +
 +</code>
 +
 +<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, 5, 13); //2020 년 6 월 13 일 - 월은+1
 + Date day = cal.getTime() ;
 + SimpleDateFormat sdf1 , sdf2 , sdf3 , sdf4 ;
 + sdf1 = new SimpleDateFormat ("yyyy-MM-dd" );
 + sdf2 = new SimpleDateFormat ("yy-MM-dd E요일 " ) ;
 + sdf3 = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss.SSS " ) ;
 + sdf4 = new SimpleDateFormat ("yyyy-MM-dd hh:mm:ss a" ) ;
 + System.out.println(sdf1.format(day));
 + System.out.println(sdf2.format(day));
 + System.out.println(sdf3.format(day)) ;
 + System.out.println(sdf4.format(day));
 +
 + }
 +
 +}
 +
 +</code>
 ===== 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로 시작하는  알파벳 소문자 0개 이상 규칙
 + Pattern p = Pattern.compile("b[a-z]*");
 + Matcher m;
 +
 + // 문자열 bat 확인
 + m = p.matcher("bat");
 + System.out.println("bat = "+m.matches());
 +
 + // 문자열 cat 확인
 + m = p.matcher("cat");
 + System.out.println("cat = "+m.matches());
 +
 + // 문자열 bed 확인
 + m = p.matcher("bed");
 + System.out.println("bed = "+m.matches());
 +
 + }
 +
 +}
 +
 +</code>
 +
 +|1. Pattern 클래스의 static 메서드 copile에 정규표현식을 매개변수로 넣고 객체 생성 Pattern p = Pattern.copile("b[a-z]*");|
 +|2. Matcher 클래스를 이용해 생성한 패턴 책체의 matcher() 메서드의 매개변수로 비교할 대상 문자열을 넣어 Matcher 객체 생성|
 +|
 +  Matcher m;
 +  m= p.matcher("bat");  
 +|
 +|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 = {"." ,"[a-z]?","[0-9]+","0[1-9]*","^[0-9]",
 + "[^0-9]","[a-z]*","[a-z]+","02|010" ,"\\s", "\\S" ,"\\d" ,"\\w","\\W"};
 + String[] datas = {"bat","021231234","12345","011","bed","02","A","9","a","*"};
 +
 + 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>
 +
 +<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 = "휴대폰번호:010-1111-1111, 집전화번호:02-1234-5678,"
 + + "이메일주소:[email protected] 계좌번호:123-12-123456";
 + String telpattern = "(0\\d{1,2})-(\\d{3,4})-(\\d{4})";
 + String emailpattern = "(\\w+)@(\\w+).(\\w+)";
 + String accountpattern = "(\\d{3})-(\\d{2})-(\\d{6})";
 +
 + Pattern p = Pattern.compile(telpattern);
 + Matcher m = p.matcher(source);
 +
 + System.out.println("전화번호 : ");
 + while(m.find()) { // 지정된 패턴 맞는 문자열을 검색
 + System.out.println(m.group() + " : "
 + + m.group(1) + "," + m.group(2) + "," + m.group(3));
 + }
 +
 + p = Pattern.compile(emailpattern);
 + m = p.matcher(source);
 + System.out.println("이메일 : ");
 + while(m.find()) { // 지정된 패턴 맞는 문자열을 검색
 + System.out.println(m.group() + " : "
 + + m.group(1) + "," + m.group(2) + "," + m.group(3));
 + }
 +
 + p = Pattern.compile(accountpattern);
 + m = p.matcher(source);
 + System.out.println("계좌번호 : ");
 + while(m.find()) { // 지정된 패턴 맞는 문자열을 검색
 + System.out.println(m.group() + " : "
 + + m.group(1) + "," + m.group(2) + "," + m.group(3));
 + }
 +
 + }
 +
 +}
 +
 +</code>