[Day22] Java 22 [9/7]

 

1. java.time : https://java119.tistory.com/52

๊ณผ๊ฑฐ์— ์‚ฌ์šฉํ•˜๋˜ Date, Calendar ํด๋ž˜์Šค์— ๋ฌธ์ œ๊ฐ€ ๋„ˆ๋ฌด ๋งŽ์•„์„œ ์‚ฌ์šฉ์ด ์ง€์–‘๋˜๊ณ  ์žˆ๊ณ ,

์ƒˆ๋กญ๊ฒŒ ๋งŒ๋“ค์–ด์ง„ java.time ํŒจํ‚ค์ง€์— LocalTime ,LocalDate, LocalDateTime ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์ถ”์„ธ์ด๋‹ค.

 

- Date ํด๋ž˜์Šค      - deprecated ๋ฉ”์„œ๋“œ

- Calendar ํด๋ž˜์Šค 

- ์ถ”์ƒ ํด๋ž˜์Šค  : new ๊ฐ์ฒด ์ƒ์„ฑ X, getInstance() ๋ฉ”์„œ๋“œ๋กœ ๊ฐ์ฒด(์ธ์Šคํ„ด์Šค) ์–ป์–ด์™€์„œ ์‚ฌ์šฉํ•œ๋‹ค. 

  ใ„ด ํƒœ๊ตญ  : BuddhistCalendar ๊ฐ์ฒด  ๋ฐ˜ํ™˜

  ใ„ด ๊ทธ ์™ธ : GregorianCalendar ๊ฐ์ฒด ๋ฐ˜ํ™˜

 

java.time ํŒจํ‚ค์ง€ ๋ฐ ๊ทธ ํ•˜์œ„ ํŒจํ‚ค์ง€ (jdk 1.8)

- ๋‹ค์–‘ํ•œ ๋‚ ์งœ,์‹œ๊ฐ„,ํ˜•์‹ํ™” ํด๋ž˜์Šค ์ถ”๊ฐ€.

- 5๊ฐ€์ง€ ํ•˜์œ„ ํŒจํ‚ค์ง€

ใ„ดjava.time : ๋‚ ์งœ,์‹œ๊ฐ„  ํ•ต์‹ฌ ํด๋ž˜์Šค ์ œ๊ณต

ใ„ดj.t.chrono : ํ‘œ์ค€(ISO)์ด ์•„๋‹Œ ๋‹ฌ๋ ฅ ์‹œ์Šคํ…œ์„ ์œ„ํ•œ ํด๋ž˜์Šค๋“ค ์ œ๊ณต

ใ„ดj.t.format : ๋‚ ์งœ,์‹œ๊ฐ„  ํ˜•์‹ํ™”,ํŒŒ์‹ฑ ํด๋ž˜์Šค ์ œ๊ณต

ใ„ดj.t.temporal : ๋‚ ์งœ,์‹œ๊ฐ„์˜   ํ•„๋“œ(field)์™€ ๋‹จ์œ„(unit) ํด๋ž˜์Šค ์ œ๊ณต.

ใ„ดj.t.zone    : ์‹œ๊ฐ„๋Œ€(time-zone) ํด๋ž˜์Šค ์ œ๊ณต

 

1) j.tํŒจํ‚ค์ง€์˜ ํ•ต์‹ฌ ํด๋ž˜์Šค

ใ„ด ๋‚ ์งœ  - LocalDate ํด๋ž˜์Šค

ใ„ด ์‹œ๊ฐ„  - LocalTime ํด๋ž˜์Šค 

ใ„ด ๋‚ ์งœ + ์‹œ๊ฐ„  - LocalDateTime ํด๋ž˜์Šค

ใ„ด  ๋‚ ์งœ + ์‹œ๊ฐ„ + ์‹œ๊ฐ„๋Œ€ - ZonedDateTime ํด๋ž˜์Šค

 

2)

Period ํด๋ž˜์Šค   =    ๋‚ ์งœ - ๋‚ ์งœ ์ฐจ์ด(๊ฐ„๊ฒฉ)

Duration ํด๋ž˜์Šค =  ์‹œ๊ฐ„ -์‹œ๊ฐ„ ์ฐจ์ด(๊ฐ„๊ฒฉ)        

 

3)

Temporal ์ธํ„ฐํŽ˜์ด์Šค

- j.t ํ•ต์‹ฌํด๋ž˜์Šค implements Temporal, TemporalAccessor, TemporalAdjuster

- ๋‚ ์งœ,์‹œ๊ฐ„์„ ํ‘œํ˜„ํ•˜๋Š ํด๋ž˜์Šค๋“ค์€ ๋ชจ๋‘  Temporal ๊ตฌํ˜„ 

TemporalAmount    ์ธํ„ฐํŽ˜์ด์Šค

- Period, Duration implements TemporalAmount

 

4)

TemporalUnit ์ธํ„ฐํŽ˜์ด์Šค

- ๋‚ ์งœ์™€ ์‹œ๊ฐ„์˜ [ ๋‹จ์œ„๋ฅผ ์ •์˜]ํ•ด ๋†“์€ ๊ฒƒ์ด   TemporalUnit์ธํ„ฐํŽ˜์ด์Šค

- class  ChronoUnit   implements TemporalUnit

TemporalField์ธํ„ฐํŽ˜์ด์Šค

- ๋…„, ์›”,์ผ ๋“ฑ์˜ ๋‚ ์งœ์™€ ์‹œ๊ฐ„์˜ [ํ•„๋“œ๋ฅผ ์ •์˜]ํ•ด ๋†“์€ ์ธํ„ฐํŽ˜์ด์Šค

- class  ChronoField   implements TemporalField

 

5)

- get() - ๋‚ ์งœ, ์‹œ๊ฐ„์—์„œ ํŠน์ • ํ•„๋“œ ๊ฐ’์„ ์–ป์–ด์˜ฌ๋•Œ

-plus(), minus()   - ํŠน์ • ๋‚ ์งœ, ์‹œ๊ฐ„        

 

6) LocalDate, LocalTime  

 

// new ์—ฐ์‚ฐ์ž๋กœ ๊ฐ์ฒด ์ƒ์„ฑ ๋ชปํ•œ๋‹ค. 
//  LocalDate d = new LocalDate() X
// - now() , of() static ๋ฉ”์„œ๋“œ
LocalDate d =  LocalDate.now();
// System.out.println(  d );  // 2022-09-07
		
int year = d.getYear();
System.out.println( year );
		
year = d.get( ChronoField.YEAR );
System.out.println( year );
		
//Calendar c = Calendar.getInstance();
//c.get( Calendar.Year );   int field
				
int month = d.getMonthValue();
System.out.println( month );
	
// ์—ด๊ฑฐํ˜•(Enum) X
// Month month = d.getMonth();
// System.out.println( month ); // SEPTEMBER
		
int date = d.getDayOfMonth();
System.out.println( date );

 

2. ํ˜•์‹ํ™” ํด๋ž˜์Šค (formatting)

1) DecimalFormat (์ˆซ์ž -> ํ˜•์‹ํ™” ํด๋ž˜์Šค)

- ์ˆซ์ž๋ฅผ ๋‚ด๊ฐ€ ์›ํ•˜๋Š” ํ˜•์‹์œผ๋กœ 

 

0    10์ง„์ˆ˜             

#   10์ง„์ˆ˜

.     ์†Œ์ˆ˜์ 

-   ์Œ์ˆ˜๋ถ€ํ˜ธ

,    ๋‹จ์œ„ ๊ตฌ๋ถ„์ž

E   ์ง€์ˆ˜๊ธฐํ˜ธ

;    ํŒจํ„ด๊ตฌ๋ถ„์ž

%

\u00A4          ํ†ตํ™”๊ธฐํ˜ธ

'   escape๋ฌธ์ž

 

์›ํ™” + 1,234 ํ˜•์‹ ์ถœ๋ ฅ

String smoney = String.format("\\%,d", money);
System.out.println( smoney );

String pattern = "\u00A4 #,###";  // p 541  ํ‘œ 10-1  ํŒจํ„ด์‚ฌ์šฉ๋˜๋Š” ๊ธฐํ˜ธ
DecimalFormat df = new DecimalFormat(pattern );
String fmoney =  df.format(money);
System.out.println( fmoney ); // ์›ํ™” 1,234

String 1,234,567 -> int 1234567

String smoney = "1,234,567";  
int money ; //  = 1234567;
		
String pattern = "#,###";
DecimalFormat df = new DecimalFormat(pattern);
try {
	Number n = df.parse( smoney );
	// Number -> int ํ˜•๋ณ€ํ™˜
	money =  n.intValue();
	// n.longValue();
	// n.xxxValue();
	System.out.println(  money );
} catch (ParseException e) { 
	e.printStackTrace();
}

String 1,234,567.89 -> double 1234567,89

String smoney = "1,234,567.89";
double  money = 1234567.89;
	
pattern = "#,###.##";
df = new DecimalFormat(pattern);
try {
	Number n = df.parse(smoney);
	money = n.doubleValue();
	// ์ถœ๋ ฅ
} catch (ParseException e) { 
	e.printStackTrace();
}

#0010 ํ˜•์‹์œผ๋กœ ์ถœ๋ ฅ

// 1๋ฒˆ
String sno =  String.format("#%04d", no);
System.out.println( sno );

// 2๋ฒˆ
String pattern = "'#'0000";
DecimalFormat df = new DecimalFormat(pattern);
String sno = df.format(no);
System.out.println( sno );

 

2) java.time fomatting

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

LocalDate localDate = LocalDate.now(); //For reference
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd LLLL yyyy");
String formattedString = localDate.format(formatter);

 

3) ChoiceFormat

- ํŠน์ • ๋ฒ”์œ„์— ์†ํ•˜๋Š” ๊ฐ’์„ ๋ฌธ์ž์—ด๋กœ ๋ฐ˜ํ™˜ํ•ด ์ค€๋‹ค

- ๋ถˆ์—ฐ์†์ ์ธ ๋ฒ”์œ„์˜ ๊ฐ’๋“ค์„ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐ ์žˆ์–ด์„œ if๋ฌธ, switch๋ฌธ์„ ๋Œ€์‹ ํ•ด์„œ ์‚ฌ์šฉ

// if, switch๋ฌธ ์‚ฌ์šฉํ•ด์„œ               ์ ์ˆ˜-> ์ˆ˜~๊ฐ€ ์ถœ๋ ฅ
int [] scores = { 100, 57, 95, 88, 77, 80, 0};
		
// p 549  '#' ๊ฒฝ๊ณ„๊ฐ’์„ ๋ฒ”์œ„์— ํฌํ•จ
//            '<'   ๊ฒฝ๊ณ„๊ฐ’์„ ํฌํ•จํ•˜์ง€ ํ•œ๋Š” ๊ฒฝ์šฐ
		
String newPattern ="0#๊ฐ€|60#์–‘|70#๋ฏธ|80#์šฐ|90#์ˆ˜";
ChoiceFormat cf = new ChoiceFormat(newPattern );
		
/*
double[] limits = { 0, 60,70, 80 , 90};  //๋‚ฎ์€๊ฐ’๋ถ€ํ„ฐ ..
String[] formats = {"๊ฐ€","์–‘", "๋ฏธ", "์šฐ", "์ˆ˜"};
ChoiceFormat cf = new ChoiceFormat(limits, formats);
*/
		
for (int i = 0; i < scores.length; i++) {
    System.out.printf("%d์ ์ˆ˜ - %s \n",scores[i],  cf.format(  scores[i] ));		   
}

 

4) MessageForamt ํ˜•์‹ํ™” ํด๋ž˜์Šค  (์ž์ฃผ ์•ˆ์”€)

- ๋ฐ์ดํ„ฐ๋ฅผ ์ •ํ•ด์ง„ ์–‘์‹์— ๋งž๊ฒŒ ์ถœ๋ ฅ

public static void main(String[] args) {
	String name = "ํ™ฉ์ˆ˜๋นˆ";
	int age = 20;
	boolean gender = false;

	/*
	// ์ถœ๋ ฅ ํ˜•์‹      "์ด๋ฆ„:ํ™ฉ์ˆ˜๋นˆ, ๋‚˜์ด:20์‚ด, ์„ฑ๋ณ„:์—ฌ์ž"
	String str = String.format("์ด๋ฆ„:%s, ๋‚˜์ด:%d์‚ด, ์„ฑ๋ณ„:%s", name, age, gender?"๋‚จ์ž":"์—ฌ์ž");
	System.out.println( str );
	*/

	/* ์ด๋ ‡๊ฒŒ ์‚ฌ์šฉ X     ์™œ ?   static ์„ ์–ธ๋œ format() ๋ฉ”์„œ๋“œ , ๊ตณ์ด ๊ฐ์ฒด ์ƒ์„ฑ X 
	String pattern = "";
	MessageFormat mf = new MessageFormat(pattern);
    mf.format(args, null, null)
	*/
	
	String pattern = "์ด๋ฆ„:{0}, ๋‚˜์ด:{1}์‚ด, ์„ฑ๋ณ„:{2}";            // Object...args          C#
	//String            .format("์ด๋ฆ„:%s, ๋‚˜์ด:%d์‚ด, ์„ฑ๋ณ„:%s", name, age, gender?"๋‚จ์ž":"์—ฌ์ž");
	String str = MessageFormat.format(pattern, name, age, gender?"๋‚จ์ž" :"์—ฌ์ž");
	System.out.println( str );
} // main
public static void main(String[] args) {
	Object [][]  arguments = {
			{"ํ™ฉ์ˆ˜๋นˆ", 20},
			{"์ด์žฌ๋ฏผ", 25},
			{"์ž„ํฌ์ง„", 21 }
	};
	
	// Oracle ํ…Œ์ด๋ธ” ์ถ”๊ฐ€     insert into ํ…Œ์ด๋ธ”๋ช… ( ์ปฌ๋Ÿผ๋ช…,..) values (  ๊ฐ’);
	String tableName = "member";
	String pattern = String.format("insert into %s ( name, age ) values (  ''{0}'', {1} );"
			,  tableName);
	// System.out.println(   sql  );
	// insert into member ( name, age ) values (  '{0}', {1} );
	
	for (int i = 0; i < arguments.length; i++) {
		  String sql =  MessageFormat.format(pattern, arguments[i]);
		  System.out.println( sql );
	}
} // main

MessageFormat.format();   static ๋ฉ”์„œ๋“œ๋„ ์žˆ๋Š”๋ฐ

*** mf.parse() ์ธ์Šคํ„ด์Šค ๋ฉ”์„œ๋“œ *** -> ๊ฐ์ฒด ์ƒ์„ฑ ํ•„์š”

String source = "์ด๋ฆ„:์กฐ๋ฏผ๊ฒฝ, ๋‚˜์ด:20์‚ด, ์„ฑ๋ณ„:์—ฌ์ž";

String pattern = "์ด๋ฆ„:{0}, ๋‚˜์ด:{1}์‚ด, ์„ฑ๋ณ„:{2}";
MessageFormat mf = new MessageFormat(pattern );
Object[] objs;

try {
	objs = mf.parse(source);
	for (int i = 0; i < objs.length; i++) {
		  System.out.println(  objs[i] );
	}
} catch (ParseException e) { 
	e.printStackTrace();
}
			
// ์กฐ๋ฏผ๊ฒฝ	
// 20
// ์—ฌ์ž
		
// [๋กœ์ง]
//             ,  split
// "์ด๋ฆ„"   "์กฐ๋ฏผ๊ฒฝ" [1]  ->        :  split
// "๋‚˜์ด"   "20์‚ด" [1] -> 20
// "์„ฑ๋ณ„"    "์—ฌ์ž"[1] -> false
		

String [] datas = source.split(",");
for (int i = 0; i < datas.length; i++) {
// System.out.println( datas[i] );
	String [] kv = datas[i].split(":");
	System.out.println(    kv[1]  );
}
  • ๋„ค์ด๋ฒ„ ๋ธ”๋Ÿฌ๊ทธ ๊ณต์œ ํ•˜๊ธฐ
  • ๋„ค์ด๋ฒ„ ๋ฐด๋“œ์— ๊ณต์œ ํ•˜๊ธฐ
  • ํŽ˜์ด์Šค๋ถ ๊ณต์œ ํ•˜๊ธฐ
  • ์นด์นด์˜ค์Šคํ† ๋ฆฌ ๊ณต์œ ํ•˜๊ธฐ