ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

๐Ÿ“Œ ๋‚ ์งœ์™€ ์‹œ๊ฐ„

: ์ž๋ฐ”์—์„œ ๋‚ ์งœ์™€ ์‹œ๊ฐ„๊ณผ ๊ด€๋ จ๋œ ํด๋ž˜์Šค์™€ ํŒจํ‚ค์ง€์—๋Š” Date, Calendar, java.time์ด ์žˆ์Œ! ์š”์ฆ˜์€ ์ตœ๊ทผ ์ถ”๊ฐ€๋œ java.time ํŒจํ‚ค์ง€๋ฅผ ์‚ฌ์šฉ but ์•„์ง Date์™€ Calendar์„ ์‚ฌ์šฉํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์ด ๋งŽ์œผ๋ฏ€๋กœ ๋ฐฐ์›Œ ๋†“๊ธด ํ•ด์•ผ ํ•จ!

 

โœ”๏ธ Calendar

https://docs.oracle.com/javase/8/docs/api/java/util/Calendar.html

 

Calendar (Java Platform SE 8 )

Adds or subtracts (up/down) a single unit of time on the given time field without changing larger fields. For example, to roll the current date up by one day, you can achieve it by calling: roll(Calendar.DATE, true). When rolling on the year or Calendar.YE

docs.oracle.com

: Calendar์€ ์ถ”์ƒ ํด๋ž˜์Šค, ๊ทธ๋ž˜์„œ getInstance() ๋ฉ”์„œ๋“œ๋กœ ์ธ์Šคํ„ด์Šค๋ฅผ ์–ป์–ด์•ผ ํ•จ

: Calendar์˜ ๊ตฌํ˜„ ํด๋ž˜์Šค๋กœ๋Š” GregorianCalendar์™€ BuddhistCalendar๊ฐ€ ์žˆ๊ณ  ํƒœ๊ตญ ์™ธ์˜ ๊ตญ๊ฐ€์—์„œ๋Š” GregorianCalendar๋ฅผ ์‚ฌ์šฉ

Calendar cal = Calendar.getInstance(); // new GregorianCalendar() ์‚ฌ์šฉX (DIP)
Calendar today = Calendar.getInstance();
today.get(Calendar.YEAR); // 2023

๐Ÿ”Ž get()ํ•จ์ˆ˜ ์•ˆ์— ๋„ฃ์„ ์ˆ˜ ์žˆ๋Š” ํ•„๋“œ

: (Calendar.) YEAR, MONTH, DATE( = DAY_OF_MONTH), AM_PM, ...

 

โœ”๏ธ Date

Calendar cal = Calendar.getInstance();
Date d = new Date(cal.getTimeInMillis());
Date d = new Date();
Calendar cal = Calendar.getInstance();
cal.getTime(d);

 

 

๐Ÿ“Œ ํ˜•์‹ํ™”

: ์ˆซ์ž, ๋‚ ์งœ, ํ…์ŠคํŠธ ๋ฐ์ดํ„ฐ๋ฅผ ์ผ์ •ํ•œ ํ˜•์‹์— ๋งž๊ฒŒ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์„ ๊ฐ์ฒด์ง€ํ–ฅ์ ์œผ๋กœ ์„ค๊ณ„ํ•˜์—ฌ ํ‘œ์ค€ํ™”

https://docs.oracle.com/javase/8/docs/api/java/text/package-summary.html

 

java.text (Java Platform SE 8 )

Interface Summary  Interface Description AttributedCharacterIterator An AttributedCharacterIterator allows iteration through both text and related attribute information. CharacterIterator This interface defines a protocol for bidirectional iteration over

docs.oracle.com

 

 

โœ”๏ธ DecimalFormat

: ์ˆซ์ž๋ฅผ ํ˜•์‹ํ™”

https://docs.oracle.com/javase/8/docs/api/java/text/DecimalFormat.html

 

DecimalFormat (Java Platform SE 8 )

DecimalFormat is a concrete subclass of NumberFormat that formats decimal numbers. It has a variety of features designed to make it possible to parse and format numbers in any locale, including support for Western, Arabic, and Indic digits. It also support

docs.oracle.com

 

๐Ÿ”Ž DecimalFormat ํŒจํ„ด์— ์‚ฌ์šฉ๋˜๋Š” ๋ช†๊ฐ€์ง€ ๊ธฐํ˜ธ

โ–ช๏ธ 0 : 10์ง„์ˆ˜

โ–ช๏ธ # : 10์ง„์ˆ˜

โ–ช๏ธ . : ์†Œ์ˆ˜์ 

โ–ช๏ธ , : ๋‹จ์œ„ ๊ตฌ๋ถ„์ž

 

DecimalFormat df = new DecimalFormat("#.#");
DecimalFormat df2 = new DecimalFormat("#.###E0");

df.format(1234567.89); // 1234567.9
df2.parse(1234567.89); // 1.235E6

 

โœ”๏ธ SimpleDateFormat

https://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html

 

SimpleDateFormat (Java Platform SE 8 )

Parses text from a string to produce a Date. The method attempts to parse text starting at the index given by pos. If parsing succeeds, then the index of pos is updated to the index after the last character used (parsing does not necessarily use all charac

docs.oracle.com

Date today = new Date();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
df.format(today); // 2023-08-12

โ• Date ์ธ์Šคํ„ด์Šค๋งŒ format() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— Calendar → Date๋กœ ๋ฐ”๊ฟ”์ค˜์•ผ ํ•จ!

 

DateFormat dfIn = new DateFormat("yyyy/MM/dd");
DateFormat dfOut = new DateFormat("yyyy-MM-dd");

Date d = dfIn.parse("2023/05/31");
dfOut.format(d); // 2023-05-31;

 

๐Ÿ”Ž SimpleDateFormat ํŒจํ„ด์— ์‚ฌ์šฉ๋˜๋Š” ๋ช†๊ฐ€์ง€ ๊ธฐํ˜ธ

โ–ช๏ธ y : ๋…„๋„

โ–ช๏ธ M : ์›” (ex. 10, 10์›”, OCT)

โ–ช๏ธ d : ์ผ (ex. 15)

โ–ช๏ธ E : ์š”์ผ (ex. ์›”)

โ–ช๏ธ H : ์‹œ๊ฐ„(0~23)

โ–ช๏ธ m : ๋ถ„(0~59)

โ–ช๏ธ s : ์ดˆ(0~59)

โ–ช๏ธ z : Time zone (ex. GMT+9:00)

 

โœ”๏ธ ChoiceFormat

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

String pattern = "60#D|70#C|80<B|90#A"
ChoiceFormat form = new ChoiceFormat(pattern);

form.format("80"); // C

โ• limit#value ๋˜๋Š” limit<value๋กœ ํŒจํ„ด์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Œ! #๋Š” ๊ฒฝ๊ณ„๊ฐ’์„ ๋ฒ”์œ„์— ํฌํ•จ, <๋Š” ํฌํ•จX

 

โœ”๏ธ MessageFormat

: ๋ฐ์ดํ„ฐ๋ฅผ ์ •ํ•ด์ง„ ์–‘์‹์— ๋งž๊ฒŒ ์ถœ๋ ฅํ•  ์ˆ˜ ์žˆ๋„๋ก ๋„์™€์คŒ. ๋‹ค์ˆ˜์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ™์€ ์–‘์‹์œผ๋กœ ์ถœ๋ ฅํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋ฉด ์ข‹์Œ!

โ• parse() ๋ฉ”์„œ๋“œ๋ฅผ ํ†ตํ•ด ์ง€์ •๋œ ์–‘์‹์—์„œ ํ•„์š”ํ•œ ๋ฐ์ดํ„ฐ๋งŒ์„ ์†์‰ฝ๊ฒŒ ์ถ”์ถœํ•  ์ˆ˜ ์žˆ์Œ

String tableName = 'users';
String msg = "INSERT INTO " + tableName 
			+ " VALUE (''{0}'',''{1}'',''{2}'',''{3}'');";
Objact[][] args = {
    { "์ •์˜์ง„", "010-0000-0000", "23", "05-31" }, 
    ...
};

for(Object obg: args) {
    String result = MessageFormat.format(msg, args);
}

 

 

๐Ÿ“Œ java.time ํŒจํ‚ค์ง€

https://docs.oracle.com/javase/8/docs/api/java/time/package-summary.html

 

java.time (Java Platform SE 8 )

Class Summary  Class Description Clock A clock providing access to the current instant, date and time using a time-zone. Duration A time-based amount of time, such as '34.5 seconds'. Instant An instantaneous point on the time-line. LocalDate A date withou

docs.oracle.com

โœ”๏ธ java.time๊ณผ ํ•˜์œ„ ํŒจํ‚ค์ง€์˜ ํด๋ž˜์Šค๋“ค์€ String ํด๋ž˜์Šค์ฒ˜๋Ÿผ ๋ถˆ๋ณ€ ๊ฐ์ฒด! (์ฆ‰, ๋‚ ์งœ๋‚˜ ์‹œ๊ฐ„์„ ๋ณ€๊ฒฝํ•˜๋ฉด ๊ธฐ์กด ๊ฐ์ฒด๋ฅผ ๋ณ€๊ฒฝํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ์ƒˆ๋กœ์šด ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜)

 

๐Ÿ’๐Ÿป‍โ™€๏ธ Calendar๋Š” ๊ฐ€๋ณ€ ๊ฐ์ฒด์ธ๋ฐ, ์™œ ๋ถˆ๋ณ€ ๊ฐ์ฒด๊ฐ€ ๋˜์—ˆ๋‚˜์š”?

๐Ÿ’ก ๋ฉ€ํ‹ฐ ์“ฐ๋ ˆ๋“œ ํ™˜๊ฒฝ์—์„œ ๋™์‹œ์— ์—ฌ๋Ÿฌ ์“ฐ๋ ˆ๋“œ๊ฐ€ ๊ฐ™์€ ๊ฐ์ฒด์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— Calendar์™€ ๊ฐ™์ด ๋ณ€๊ฒฝ๊ฐ€๋Šฅํ•œ ๊ฐ์ฒด๋Š” ๋ฉ€ํ‹ฐ ์“ฐ๋ ˆ๋“œ ํ™˜๊ฒฝ์—์„œ ์•ˆ์ „ํ•˜์ง€ ๋ชปํ•จ

 

โœ”๏ธ java.time ํŒจํ‚ค์ง€์˜ ํ•ต์‹ฌ ํด๋ž˜์Šค

 

1๏ธโƒฃ LocalDate์™€ LocalTime

LocalDate๋Š” ๋‚ ์งœ ํ‘œํ˜„ ํด๋ž˜์Šค์ด๊ณ , LocalTime๋Š” ์‹œ๊ฐ„ ํ‘œํ˜„ ํด๋ž˜์Šค

https://docs.oracle.com/javase/8/docs/api/java/time/LocalDate.html

 

LocalDate (Java Platform SE 8 )

Returns a copy of this date with the specified field set to a new value. This returns a LocalDate, based on this one, with the value for the specified field changed. This can be used to change any supported field, such as the year, month or day-of-month. I

docs.oracle.com

 

โœ”๏ธ ์ƒ์„ฑํ•˜๊ธฐ - now(), of(), parse()

LocalDate date = LocalDate.now(); // ํ˜„์žฌ ๋‚ ์งœ(ex. 2023-08-12)
LocalTime time = LocalTime.now(); // ํ˜„์žฌ ์‹œ๊ฐ„(ex. 21:54:01.875)

LocalDate date = LocalDate.of(2023, 08, 31);
LocalTime time = LocalTime.of(23, 59, 59);

โ• of() ๋ฉ”์„œ๋“œ๋Š” ๋‹ค์–‘ํ•œ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ๊ฐ€์ง€๊ณ  ์˜ค๋ฒ„๋กœ๋”ฉ๋˜์–ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๊ณต์‹๋ฌธ์„œ๋ฅผ ์ฐพ์•„๋ณด๊ธฐ!

โ•parse() ๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ•˜๋ฉด ๋ฌธ์ž์—ด → ๋‚ ์งœ/์‹œ๊ฐ„์œผ๋กœ ๋ณ€ํ™˜ ๊ฐ€๋Šฅ!

LocalDate birthDate = LocalDate.parse("2000-05-31");
LocalTime tile = LocalTime.parse("23:59:59");

 

โœ”๏ธ ํŠน์ • ํ•„๋“œ ๊ฐ’ ๊ฐ€์ ธ์˜ค๊ธฐ - get(), getXXX()

๐Ÿ”Ž LocalDate ํด๋ž˜์Šค

๋ฉ”์„œ๋“œ ์„ค๋ช…
int getYear() ๋…„๋„(ex. 2023)
int getMonthValue() ์›”(ex. 10)
Month getMonth() ์›”(ex. DECEMBER) getMonth().getValue() = 12
int getDayOfMonth() ์ผ(ex. 31)
int getDayOfYear() ๊ฐ™์€ ํ•ด์˜ 1์›” 1์ผ๋ถ€ํ„ฐ ๋ช‡๋ฒˆ์งธ ์ผ(ex. 365)
DayOfWeek getDayOfWeek() ์š”์ผ(ex. FRIDAY) getDayOfWeek().getValue() = 5
int lengthOfMonth() ๊ฐ™์€ ๋‹ฌ์˜ ์ด ์ผ์ˆ˜(ex. 31)
int lengthOfYear() ๊ฐ™์€ ํ•ด์˜ ์ด ์ผ์ˆ˜(ex. 365) ์œค๋…„์ด๋ฉด 366
boolean isLeapYear() ์œค๋…„์—ฌ๋ถ€ ํ™•์ธ

 

๐Ÿ”Ž LocalTime ํด๋ž˜์Šค

๋ฉ”์„œ๋“œ ์„ค๋ช…
int getHour() ์‹œ(ex. 23)
int getMinute() ๋ถ„(ex. 59)
int getSecond() ์ดˆ(ex. 59) 
int getNano() ๋‚˜๋…ธ์ดˆ(ex. 0)

 

โ• get(), getLong() ๋ฉ”์„œ๋“œ๋ฅผ ์“ฐ๊ณ  ์›ํ•˜๋Š” ํ•„๋“œ๋ฅผ ์ง์ ‘ ์ธ์ž๋กœ ๋„ฃ์–ด์ค„ ์ˆ˜ ์žˆ์Œ!

int get(TemporalField field)
long getLong(TemporalField field)

๐Ÿ’๐Ÿป‍โ™€๏ธ TemporalField?

๐Ÿ‘ฉ๐Ÿป‍๐Ÿซ ๋‚ ์งœ์™€ ์‹œ๊ฐ„์˜ ๋‹จ์œ„๋ฅผ ์ •์˜ํ•ด ๋†“์€ ๊ฒƒ์ด TemporalUnit ์ธํ„ฐํŽ˜์ด์Šค์ด๊ณ , ์ด ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•œ ๊ฒƒ์ด ์—ด๊ฑฐํ˜• ChronoUnit์ž„!

๊ทธ๋ฆฌ๊ณ  TemporalField๋Š” ๋…„, ์›”, ์ผ ๋“ฑ ๋‚ ์งœ์™€ ์‹œ๊ฐ„์˜ ํ•„๋“œ๋ฅผ ์ •์˜ํ•ด ๋†“์€ ๊ฒƒ์ด๋ฉฐ ์—ด๊ฑฐํ˜• ChronoField๊ฐ€ ์ด ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„!

TemporalField(ChronoField) ์„ค๋ช…
ERA ์‹œ๋Œ€
YEAR ๋…„
MONTH_OF_YEAR ์›”
DAY_OF_MONTH ์ผ
DAY_OF_WEEK ์š”์ผ (1:์›”, 2:ํ™” ...)
AMPM_OF_DAY ์˜ค์ „/์˜คํ›„
HOUR_OF_DAY ์‹œ๊ฐ„(0~23)
HOUR_OF_AMPM ์‹œ(0~11)
MINUTE_OF_HOUR ๋ถ„
SECOND_OF_MINUTE ์ดˆ

โ• ChronoField ์ƒ์ˆ˜ํ•„๋“œ์˜ ์ผ๋ถ€๋ถ„ ์ด๋ฏ€๋กœ ๊ณต์‹๋ฌธ์„œ๋ฅผ ์ฐพ์•„๋ณด๊ธฐ!

โ•์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ํ•„๋“œ๋Š” ํด๋ž˜์Šค๋งˆ๋‹ค ๋‹ค๋ฆ„! (ex. LocalDate๋Š” ๋‚ ์งœ๋ฅผ ํ‘œํ˜„ํ•˜๊ธฐ ์œ„ํ•œ ๊ฒƒ์ด๋ฏ€๋กœ MINUTE_OF_HOUR๊ฐ™์€ ํ•„๋“œ ์ง€์›X)

    → isSupported() ๋ฉ”์„œ๋“œ๋กœ ํ•ด๋‹น ํ•„๋“œ ์ง€์› ์—ฌ๋ถ€๋ฅผ ํ™•์ธ ํ•  ์ˆ˜ ์žˆ์Œ!

LocalDate today = LocalDate.now()
today.isSupported(ChronoField.MINUTE_OF_HOUR); // false

 

โœ”๏ธ ํŠน์ • ํ•„๋“œ ๊ฐ’ ๋ณ€๊ฒฝํ•˜๊ธฐ -with(), plus(), minus()

โ–ช๏ธ with() - ์›ํ•˜๋Š” ํ•„๋“œ๋ฅผ ์ง์ ‘ ๋ณ€๊ฒฝ

LocalDate today = LocalDate.now();
today.withYear(2000); // 2000๋…„๋„๋กœ ๋ณ€๊ฒฝ

โ–ช๏ธ plus(), minus() - ํŠน์ • ํ•„๋“œ์— ๊ฐ’์„ ๋”ํ•˜๊ฑฐ๋‚˜ ๋นผ๊ธฐ

today.plus(23, ChronoUnit.YEARS);
today.plusYears(23);

โ• LocalTime์˜ truncatedTo() ๋ฉ”์„œ๋“œ๋Š” ์ง€์ •๋œ ๊ฒƒ๋ณด๋‹ค ์ž‘์€ ๋‹จ์œ„์˜ ํ•„๋“œ๋ฅผ 0์œผ๋กœ ๋งŒ๋“ฆ

LocalTime time = LocalTime.of(12, 34, 56);
time.truncatedTo(ChronoUnit.HOURS); // 12:00:00

 

โœ”๏ธ ๋‚ ์งœ์™€ ์‹œ๊ฐ„ ๋น„๊ตํ•˜๊ธฐ -isAfter(), isBefore(), isEqual()

boolean isAfter(ChronoLocalDate other)
boolean isBefore(ChronoLocalDate other)
boolean isEqual(ChronoLocalDate other)

โ• LocalTime๋„ compareTo()๊ฐ€ ์ ์ ˆํ•˜๊ฒŒ ์˜ค๋ฒ„๋ผ์ด๋”ฉ๋˜์–ด ์žˆ์–ด์„œ compareTo()๋ฅผ ํ†ตํ•ด ๋น„๊ต ๊ฐ€๋Šฅ!

 

2๏ธโƒฃ Instant

: Date์™€ ์œ ์‚ฌํ•œ ํด๋ž˜์Šค๋กœ ์• ํฌํฌ ํƒ€์ž„(1970-01-01 00:00:00 UTC)๋ถ€ํ„ฐ ๊ฒฝ๊ณผ๋œ ์‹œ๊ฐ„์„ ๋‚˜๋…ธ์ดˆ ๋‹จ์œ„๋กœ ํ‘œํ˜„ 

: ๊ณ„์‚ฐ์— ์œ ๋ฆฌ

Instant now = Instant.now();
Instant now2 = Instant.ofEpochSecond(now.getEpochSecond());
Instant now3 = Instant.ofEpochSecond(now.getEpochSecond(), now.getNono());

long epochSec = now.getEpochSecond();
int nano = now.getNano();
long milli = now.toEpochMilli();

โ• Instant๋Š” ํ•ญ์ƒ UTC(+00:00)์„ ๊ธฐ์ค€์œผ๋กœ ํ•˜๊ธฐ ๋•Œ๋ฌธ์—, LocalTime๊ณผ ์ฐจ์ด๊ฐ€ ์žˆ์„ ์ˆ˜ ์žˆ์Œ!

โ• Instant๋Š” Date๋ฅผ ๋Œ€์ฒดํ•˜๊ธฐ ์œ„ํ•œ ๊ฒƒ์ด๋ฏ€๋กœ Date์™€ ๋ณ€ํ™˜ ๊ฐ€๋Šฅ!

static Date from(Instant instant)
Instant toInstant()

 

3๏ธโƒฃ LocalDateTime๊ณผ ZonedDateTime

: LocalDateTime์€ ์‹œ๊ฐ„(LocalDate) + ๋‚ ์งœ(LocalTime)๋ฅผ ํ•ฉ์นœ ๊ฒƒ์ด๊ณ  ZonedDateTime์€ ๊ฑฐ๊ธฐ์— ์‹œ๊ฐ„๋Œ€๋ฅผ ์ถ”๊ฐ€ํ•œ ๊ฒƒ!

LocalDateTime dateTime = LocalDateTime.now(); // 2023-08-12T21:54:01.875
ZonedDateTime dateTimeInKr = ZonedDateTime.now(); // 2023-08-12T21:54:01.875+9:00[Asia/Seoul]
LocalDateTime dateTime = LocalDateTime.of(date, time);
ZonedDateTime zDateTime = ZonedDateTime.of(dateTime, ZoneId.of("Asia/Seoul"));

 

โœ”๏ธ LocalDateTime์„ LocalDate/LocalTime์œผ๋กœ ๋ณ€ํ™˜ํ•˜๊ธฐ

LocalDateTime dt = LocalDateTime.of(2023, 8, 31, 12, 34, 56);
LocalDate date = dt.toLocalDate();
LocalTime time = dt.toLocalTime();

 

โ• LocalDate, LocalTime, LocalDateTime, ZonedDateTime๋“ฑ ๋‚ ์งœ์™€ ์‹œ๊ฐ„์„ ํ‘œํ˜„ํ•˜๊ธฐ ์œ„ํ•œ ํด๋ž˜์Šค๋“ค์€ ๋ชจ๋‘ TemporalTemporalAccessorTemporalAdjuster ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•จ!

 

4๏ธโƒฃ Period์™€ Duration

: Period๋Š” ๋‚ ์งœ์˜ ์ฐจ์ด๋ฅผ, Duration์€ ์‹œ๊ฐ„์˜ ์ฐจ์ด๋ฅผ ๊ณ„์‚ฐํ•˜๊ธฐ ์œ„ํ•œ ๊ฒƒ

 

โœ”๏ธ between() - Period์™€ Duration ์ƒ์„ฑํ•˜๊ธฐ

LocalDate date1 = LocalDate(2023, 1, 1);
LocalDate date2 = LocalDate(2023, 12, 31);
Period pe = Period.between(date1, date2); // date1์ด ๋‚˜์ค‘์ด๋ฉด ์Œ์ˆ˜ ๋ฐ˜ํ™˜!

LocalTime time1 = LocalTime(00, 00, 00);
LocalTime time2 = LocalTime(12, 34, 31);
Duration du = Duration.between(time1, time2);

 

โœ”๏ธ get() - ํ•„๋“œ๊ฐ’ ์–ป๊ธฐ

int year = pe.get(ChronoUnit.YEARS);
long sec = du.get(ChronoUnit.SECONDS);

: LocalDate๋Š” Years, Months, Days๋กœ ๊ฐ’์„ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ๊ณ , LocalTime์€ Seconds, Nanos(Years, Months, DaysโŒ)๋กœ ๊ฐ’์„ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ์Œ!

 

๐Ÿ’ก Duration → LocalTime์œผ๋กœ ๋ณ€ํ™˜ํ•ด์„œ ํ•ด๊ฒฐ ๊ฐ€๋Šฅ!

LocalTime tmpTime = LocalTime.of(0, 0).plusSeconds(du.getSeconds());

int hour = tmpTime.getHour();
int min = tmpTime.getMinute();
int sec = tmpTime.getSecond();

 

โœ”๏ธ util() - ๋””๋ฐ์ด ๊ตฌํ•˜๊ธฐ

: between()๊ณผ ๊ฐ™์€ ์ผ์„ ํ•˜์ง€๋งŒ between()์€  static ๋ฉ”์„œ๋“œ์ด๊ณ , util()์€ ์ธ์Šคํ„ด์Šค ๋ฉ”์„œ๋“œ์ž„!

long dday = todat.util(birthDay, ChronoUnit.DAYS);

 

โœ”๏ธ of(), with()

: Period์™€ Duration์—๋„ of()์™€ with() ํ•จ์ˆ˜๊ฐ€ ์กด์žฌ!

 

โœ”๏ธ ์‚ฌ์น™์—ฐ์‚ฐ, ๋น„๊ต์—ฐ์‚ฐ, ๊ธฐํƒ€ ๋ฉ”์„œ๋“œ

โ–ช๏ธ plus(), minus(), multipliedBy(), dividedBy()

โ• Period๋ฅผ ์œ„ํ•œ ๋‚˜๋ˆ—์…ˆ ๋ฉ”์„œ๋“œ๋Š” ์—†์Œ!

โ–ช๏ธ isNegative(), isZero(), abs()

โ• Period๋ฅผ ์œ„ํ•œ abs()๊ฐ€ ์—†์Œ!

๐Ÿ’ก ํ•ด๊ฒฐ์ฑ…

if(pe.isNegative()) {
    pe = pe.negated();
}

โ–ช๏ธ normalized()

: Period์—๋งŒ ์กด์žฌํ•˜๋Š” ๋ฉ”์„œ๋“œ, ์›”(month)๊ฐ€ 12๋ฅผ ๋„˜์ง€ ์•Š๊ฒŒ ๋ฐ”๊ฟ”์คŒ!

pe = Period.of(1, 13, 32).normalized(); // 2๋…„ 1๊ฐœ์›” 32์ผ๋กœ ๋ฐ”๋€œ

โ• ์ผ(day)์€ ๋ฐ”๋€Œ์ง€ ์•Š์Œ

 

โœ”๏ธ ๋‹ค๋ฅธ ๋‹จ์œ„๋กœ ๋ณ€ํ™˜ - toTotalMonth(), toDays(), toHours(), toMinutes()

: Period์™€ Duration์„ ๋‹ค๋ฅธ ๋‹จ์œ„์˜ ๊ฐ’์œผ๋กœ ๋ณ€ํ™˜

โ• LocalDate์—๋Š” toEpochDay()๋ผ๋Š” ๋ฉ”์„œ๋“œ๋„ ์กด์žฌ! '1970-01-01'๋ถ€ํ„ฐ ๋‚ ์งœ๋ฅผ ์„ธ์–ด์„œ ๋ฐ˜ํ™˜!

 

 

๐Ÿ“Œ java.time ํŒŒ์‹ฑ๊ณผ ํฌ๋งท

: java.time.format ํŒจํ‚ค์ง€์— ํ˜•์‹ํ™”์™€ ๊ด€๋ จ๋œ ํด๋ž˜์Šค๊ฐ€ ์žˆ์Œ

https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html

 

DateTimeFormatter (Java Platform SE 8 )

Parses the text using this formatter, without resolving the result, intended for advanced use cases. Parsing is implemented as a two-phase operation. First, the text is parsed using the layout defined by the formatter, producing a Map of field to value, a

docs.oracle.com

: DateTimeFormatter๊ฐ€ ํ•ต์‹ฌ!

LocalDate date = LocalDate.of(2000, 5, 31);
String yyyymmdd = DateTimeFormatter.ISO_LOCAL_DATE.format(date); // 2000-05-31
String yyyymmdd = date.format(DateTimeFormatter.ISO_LOCAL_DATE); // 2000-05-31

๐Ÿ”Ž DateTimeFormatter์— ์ƒ์ˆ˜๋กœ ์ •์˜๋œ ๋ช‡๊ฐ€์ง€ ํ˜•์‹

DateTimeFormatter ๋ณด๊ธฐ
ISO_DATE_TIME 2023-08-13T10:15:30+01:00[Europe/Paris]
ISO_LOCAL_DATE 2023-08-13
ISO_LOCAL_TIME 10:15:30
ISO_LOCAL_DATE_TIME 2023-08-13T10:15:30
BASIC_ISO_DATE 20230813
RFC_1123_DATE_TIME Sun, 13 Aug 2023 10:15:30 GMT

 

โœ”๏ธ ๋กœ์ผ€์ผ์— ์ข…์†๋œ ํ˜•์‹ํ™”

DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT);
String shortFormat = formatter.format(LocalDate.now()); // 23.08.13

๐Ÿ”Ž ์—ด๊ฑฐํ˜• FormatStyle์— ์ •์˜๋œ ์ƒ์ˆ˜

FormatStyle ๋‚ ์งœ ์‹œ๊ฐ„
FULL 2023๋…„ 8์›” 13์ผ ์ผ์š”์ผ N/A
LONG 2023๋…„ 8์›” 13์ผ (์ผ) ์˜คํ›„ 9์‹œ 15๋ถ„ 13์ดˆ
MEDIUM 2023.8.13 ์˜คํ›„ 9:15:13
SHORT 23.8.13์ผ ์˜คํ›„ 9:15

 

โœ”๏ธ ์ถœ๋ ฅํ˜•์‹ ์ง์ ‘ ์ •์˜ํ•˜๊ธฐ

: DateTimeFormatter์˜ ofPattern()์œผ๋กœ ์›ํ•˜๋Š” ์ถœ๋ ฅ์„ ์ง์ ‘ ์ž‘์„ฑํ•  ์ˆ˜๋„ ์žˆ์Œ

LocalDateTime dateTime = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy.mm.dd HH:mm");
dateTime.format(formatter); // 2023.08.13 01:26

โ• ํŒจํ„ด์— ์‚ฌ์šฉ๋˜๋Š” ๊ธฐํ˜ธ๋Š” ํ•„์š”ํ•  ๋•Œ ์ฐพ์•„๋ณด๊ธฐ!

 

โœ”๏ธ ๋ฌธ์ž์—ด์„ ๋‚ ์งœ์™€ ์‹œ๊ฐ„์œผ๋กœ ํŒŒ์‹ฑํ•˜๊ธฐ

: static ๋ฉ”์„œ๋“œ์ธ parse()๋ฅผ ์ด์šฉํ•˜๋ฉด ๋ฌธ์ž์—ด → ๋‚ ์งœ ๋˜๋Š” ์‹œ๊ฐ„ ๋ณ€ํ™˜ ๊ฐ€๋Šฅ!

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy.mm.dd HH:mm");
LocalDateTime dateTime = LocalDateTime.parse("2023.08.13 01:29", pattern);

โ• ์ž์ฃผ ์‚ฌ์šฉ๋˜๋Š” ๊ธฐ๋ณธ์ ์ธ ํ˜•์‹์˜ ๋ฌธ์ž์—ด์€ ํ˜•์‹ํ™” ์ƒ์ˆ˜์—†์ด ํŒŒ์‹ฑ ๊ฐ€๋Šฅ!

LocalDate date = LocalDate.parse("2023-08-13");

 

๊ณต์ง€์‚ฌํ•ญ
์ตœ๊ทผ์— ์˜ฌ๋ผ์˜จ ๊ธ€
์ตœ๊ทผ์— ๋‹ฌ๋ฆฐ ๋Œ“๊ธ€
Total
Today
Yesterday
๋งํฌ
ยซ   2025/05   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
๊ธ€ ๋ณด๊ด€ํ•จ