[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] );
}
์ต๊ทผ๋๊ธ