ํฐ์คํ ๋ฆฌ ๋ทฐ
๐ ๋ ์ง์ ์๊ฐ
: ์๋ฐ์์ ๋ ์ง์ ์๊ฐ๊ณผ ๊ด๋ จ๋ ํด๋์ค์ ํจํค์ง์๋ 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๋ฑ ๋ ์ง์ ์๊ฐ์ ํํํ๊ธฐ ์ํ ํด๋์ค๋ค์ ๋ชจ๋ Temporal, TemporalAccessor, TemporalAdjuster ์ธํฐํ์ด์ค๋ฅผ ๊ตฌํํจ!
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");
'์ธ์ด > ์๋ฐ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Effective Java] 2. ์์ฑ์์ ๋งค๊ฐ๋ณ์๊ฐ ๋ง๋ค๋ฉด ๋น๋๋ฅผ ๊ณ ๋ คํ๋ผ (0) | 2023.09.22 |
---|---|
๋คํธ์ํน (0) | 2023.08.17 |
์คํธ๋ฆผ(stream) (0) | 2023.08.06 |
๋๋ค(lambda) (0) | 2023.08.06 |
์ฐ๋ ๋ (0) | 2023.07.30 |