프로그래밍/Java 공부

데이터 포맷, 텍스트 포맷, 동적가변배열, 배열 복사

개발계발게발 2021. 5. 31. 17:22
반응형

데이터 포맷

Date, Calendar

import java.util.Calendar;
import java.util.Date;
import java.util.Scanner;

public class DateFormat01 {
	public static void main(String[] args) {
		Date date = new Date();
		System.out.println(date);	//	Mon May 31 09:43:24 KST 2021
		
		Calendar cal = Calendar.getInstance();
		System.out.println(cal);
		System.out.println(cal.get(Calendar.YEAR));	//	2021
		System.out.println(cal.get(Calendar.MONTH) + 1); //	5 시작월이 0월
		System.out.println(cal.get(Calendar.DATE));	//	31
		
		System.out.println(cal.get(Calendar.HOUR));	//	9
		System.out.println(cal.get(Calendar.MINUTE));	//43
		System.out.println(cal.get(Calendar.SECOND));	//24	//	static 멤버변수
		
		System.out.println(cal.get(Calendar.DAY_OF_WEEK));	//	2
		//	1 ~ 7 일요일부터 시작 1 - 일요일 2 - 월요일 3 - 화요일
		 
		Scanner sc = new Scanner(System.in);
		
		System.out.print("출생 년도를 입력하세요 : ");
		
		int year = cal.get(Calendar.YEAR)-sc.nextInt();
		
		System.out.printf("당신의 나이는 %d살 입니다.\n",year);
		
		System.out.println(cal.get(Calendar.AM_PM));	// 0 or 1
		//24시간
		System.out.println(cal.get(Calendar.HOUR_OF_DAY));	// 10
		
		System.out.println(cal.getActualMaximum(Calendar.DATE)); //이달의 마지막날 31
		
		//이달의 몇번째 요일
	
		System.out.println(cal.get(Calendar.DAY_OF_WEEK_IN_MONTH));		
		System.out.println(cal.getTimeInMillis());	// 1622423207251		
		sc.close();
	}
}

 

Calendar 현재 시간 출력

 

import java.util.Calendar;

public class DateFormat02 {
	public static void main(String[] args) {
		//현재 시간 출력
		Calendar cal =Calendar.getInstance();
		// 객체 생성 new + 생성자
		
		System.out.print(cal.get(Calendar.YEAR) + "년 ");
		System.out.print(cal.get(Calendar.MONTH) + 1 + "월 " );
		System.out.print(cal.get(Calendar.DATE) + "일 " );
		switch(cal.get(Calendar.DAY_OF_WEEK)) {
		case 1:
			System.out.print("일요일 ");
			break;
		case 2:
			System.out.print("월요일 ");
			break;
		case 3:
			System.out.print("화요일 ");
			break;
		case 4:
			System.out.print("수요일 ");
			break;
		case 5:
			System.out.print("목요일 ");
			break;
		case 6:
			System.out.print("금요일 ");
			break;
		case 7:
			System.out.print("토요일 ");
			break;
		}
		int amPm=cal.get(Calendar.AM_PM);
		if(amPm==0){
			System.out.print("오전 ");			
		}
		else {
				System.out.print("오후 ");
		}
		System.out.print(cal.get(Calendar.HOUR)+"시 ");
		System.out.print(cal.get(Calendar.MINUTE)+"분 ");
		System.out.print(cal.get(Calendar.SECOND)+"초입니다.");
		
		String amPmstr = amPm == 0 ? "오전" : "오후";		
		System.out.println(amPmstr);
	}
}

 

 Date, SimpleDateFormat

 

import java.text.SimpleDateFormat;
import java.util.Date;

public class DateFormat03 {
	public static void main(String[] args) {
		//Date
		Date now = new Date();
		System.out.println(now); //Mon May 31 10:41:22 KST 2021
		
		//now.getMonth();
		SimpleDateFormat sdf =
				new SimpleDateFormat("yyyy년 MM월 dd일 E요일 a hh시 mm분 ss초");
		//E 요일 a 오전/오후
		//D 월 구분 없이 일
		//K 시간 0~11 k 1~23
		//W 월의 몇 번쨰 주 w년의 몇번째 주
		//S 밀리세컨 s초
		
		System.out.println(sdf.format(now));
		//2021년 05월 31일 월요일 오전 11시 01분 31초
		
		String str = sdf.format(now).toString();
		System.out.println(str);
		//2021년 05월 31일 월요일 오전 11시 01분 31초

	}
}

 

 

System.currentTimeMillis();

 

public class DateFormat05 {
	public static void main(String[] args) {
		//System.currentTimeMillis();
		
		long millis = System.currentTimeMillis();
		System.out.println(millis);//1622427826464
		// 1970년부터 지금까지 흐른 ms시간
		// 1000 * 60 * 60 * 24
		
		Date date = new Date(130, 3, 29);	//연도1900+ 월1+ 일0+
		System.out.println(date);//Mon Apr 29 00:00:00 KST 2030
		//Mon Apr 29 00:00:00 KST 2030
		
		System.out.println(date.getTime());//1903618800000
		
		date = new Date();//오늘
		System.out.println(date);//Mon May 31 11:36:21 KST 2021
		
		Date xMas = new Date(121, 11, 25);
		System.out.println(xMas);
		
		long howLong = xMas.getTime() - date.getTime();		
		System.out.println(howLong);	//17929296406
		
		long result = howLong / (1000 * 60 * 60 * 24);
		
		System.out.println("올 크리스마스까지 " + result);
		//올 크리스마스까지 207
        
		int day = xMas.getDay();
		System.out.println(day); //6
		
		switch(day) {
		case 0:
			System.out.print("일요일 ");
			break;
		case 1:
			System.out.print("월요일 ");
			break;
		case 2:
			System.out.print("화요일 ");
			break;
		case 3:
			System.out.print("수요일 ");
			break;
		case 4:
			System.out.print("목요일 ");
			break;
		case 5:
			System.out.print("금요일 ");
			break;
		default:
			System.out.print("토요일 ");
			break;
		}		
		long millis2 = System.currentTimeMillis();		
        
		System.out.println("소요 시간 " + (millis2 - millis));
	}
}

 

MessageFormat

 

import java.text.MessageFormat;

public class TextFormat01 {
	public static void main(String[] args) {
		
		System.out.printf("%d", 3);	//3
		
		String name = "홍길동";
		String id = "hong5000";
		String tel = "010-1234-5678";
		
		System.out.println("이름 : " + name + " 아이디 : " + id + " tel : " + tel);
		//이름 : 홍길동 아이디 : hong5000 tel : 010-1234-5678
		
		String text = "이름 : {0} ,아이디 : {1} ,tel : {2}";
		 
		String result = MessageFormat.format(text, name, id, tel);
		System.out.println(result);
		//이름 : 홍길동 ,아이디 : hong5000 ,tel : 010-1234-5678
		
		String[] arr = {"홍길동", "hong5000", "010-1234-5678"};
		result = MessageFormat.format(text, arr);
		System.out.println(result);
		//이름 : 홍길동 ,아이디 : hong5000 ,tel : 010-1234-5678
	}
}

 

동적가변배열

동적 가변배열이란 배열의 길이를 동적으로, 필요시에 생성하는 것
동적가변배열이 아닌것은 처음부터 배열의 길이를 초기화 해서 만들어 사용
동적가변배열은 필요시에 필요한 만큼 만들어서 사용

 

import java.util.Arrays;

public class DynamicArray {
	public static void main(String[] args) {
		
		int[] arr = new int[3];
		
		int[][] arr01 = new int[3][3];
		
		char[][] arr02 = new char[10][];
		
		for (int i = 0; i < arr02.length; i++) {
			arr02[i] = new char[i+1];	// 1 10 방만들기
			for (int j = 0; j < arr02[i].length; j++) {
				arr02[i][j] = (char)(65 + i + j);
			}
		}
		for (char[] cs : arr02) {
			System.out.println(Arrays.toString(cs));
		}	
	}
}

 

import java.util.Arrays;

public class DynamicArray02 {
	public static void main(String[] args) {
		int[][] dArray = new int[10][];

		for (int i = 0; i < dArray.length; i++) {
			dArray[i] = new int[(int)(Math.random() * 5 + 1)];
		}
		for (int[] is : dArray) {
			System.out.println(Arrays.toString(is));
		}
		for (int i = 0; i < dArray.length; i++) {
			for (int j = 0; j < dArray[i].length; j++) {
				System.out.println(dArray[i][j]);				
			}
		}
	}
}

 

ArrayCopy

 

import java.util.Arrays;

public class ArrayCopy01 {
	public static void main(String[] args) {
		
		int[] arr01 = {10, 20, 30};
		int[] arr02 = new int[3];
		/*
		arr02 = arr01;
		
		arr01[0] = 100;
		
		System.out.println(Arrays.toString(arr01));	//[100, 20, 30]
		System.out.println(Arrays.toString(arr02));	//[100, 20, 30]
		*/
		for (int i = 0; i < arr01.length; i++) {
			arr02[i] = arr01[i];			
		}
		arr01[0] = 1000;
		System.out.println(Arrays.toString(arr01));	//[1000, 20, 30]
		System.out.println(Arrays.toString(arr02));	//[10, 20, 30]
		
		System.arraycopy(arr01, 0, arr02, 0, arr01.length);
		/*
		 	src 원본
		 	srcPos 원본 어느 위치 부터
		 	dest	값을 담을 배열
		 	destPost	어느 위치에 저장
		 	length	어느 길이만큼		  
		 */
		System.out.println(Arrays.toString(arr01));	//[1000, 20, 30]
		System.out.println(Arrays.toString(arr02));	//[1000, 20, 30]
		
		int[] arr03 = new int[10];
		//[0,0,0,0,0,0,0,0,0,0]
		System.arraycopy(arr01, 0, arr03, 1, arr01.length);

		System.out.println(Arrays.toString(arr03));	//[0,1000, 20, 30,0,0,0,0,0,0]
		
		int[] arr04 = arr01.clone();
		System.out.println(Arrays.toString(arr04));	//[1000, 20, 30]
		
	}
}

 

가위바위보 게임

 

import java.util.Arrays;
import java.util.Scanner;

public class Test01 {
	public static void main(String[] args) {
		
		//배열에 결과 저장
		//저장 : 승 비김 패  승률
		//배열에 승률 저장
		
		Scanner sc = new Scanner(System.in);
		System.out.println("몇판 하시겠습니까?");
		int input = sc.nextInt();
		
		int[] result = new int[3];
		char[] result2 = new char[input];
		
		for(int i = 0; i < input; i++) {
			int input2;
			do {
			System.out.println("가위(1)/ 바위(2) / 보(3)를 선택하세요.");
			
			input2 = sc.nextInt();
			}while(input2 < 1 || input2 > 3);
			
			int com = (int) (Math.random() *3 + 1);
			System.out.println("당신   : "+(input2 ==1 ? "가위" : input2 == 2? "바위" :"보"));;
			System.out.println("컴퓨터 : "+(com ==1 ? "가위" : com == 2? "바위" :"보"));;
			
			if(input2 == com) { 
				System.out.println("비겼습니다.");
				result2[i] = '무';
				result[2]++;
			}else if((input2 - com == 1) || (input2 - com == -2)) {
				System.out.println("이겼습니다.");		
				result2[i] = '승';
				result[0]++;
			}else {
				System.out.println("졌습니다.");
				result2[i] = '패';
				result[1]++;
			}		
		}
		double re = (double)result[0]/(result[0]+result[1])*100;
		System.out.println("결과 : "+Arrays.toString(result2));
		System.out.println("승 : " + result[0] +"\n패 : " + result[1] + "\n무 : " + result[2]);
		System.out.printf("승률 : %.2f%%",re);
		
		sc.close();
	}
}

 

반응형