[Day23] Java 23 [9/8]
java.time ํ์ฉ
LocalDate d = LocalDate.now();
System.out.println( d ); // 2022-09-08
int year = d.getYear();
year = d.get( ChronoField.YEAR ); // -1900
System.out.println( year );
// ์. ์ผ [ ์, ๋ถ,์ด X ]
int month = d.getMonthValue(); // 9
month = d.get( ChronoField.MONTH_OF_YEAR ); // -1
// int month = d.getMonth(); // 9 Enum ์ด๊ฑฐํ Month
System.out.println( month );
int date = d.getDayOfMonth(); //
// date = d.getDayOfYear(); // 251 2022.1.1 ->9.8
date = d.get( ChronoField.DAY_OF_MONTH );
System.out.println( date );
// ์์ผ ์ถ๋ ฅ
//DayOfWeek dow = d.getDayOfWeek();
// System.out.println( dow ); // THURSDAY
//int n = dow.getValue(); //๋ชฉ์์ผ4 return ordinal() + 1
int dow = d.getDayOfWeek().getValue();
dow = d.get( ChronoField.DAY_OF_WEEK ); // 4
System.out.println( dow ); // 1์,2ํ,3์,[4๋ชฉ],5๊ธ,6 ํ ,7์ผ
์ํ๋ ๋ ์ง ๊ฐ์ฒด ์์ฑ
LocalDate sDay = LocalDate.now();
//System.out.println( sDay.lengthOfMonth() ); // ๋ง์ง๋ง ๋ ์ง
//sDay.isLeapYear() ์ค๋
/ํ๋
true,false
// Calendar c = new GregorianCalendar();
// GregorianCalendar c = new GregorianCalendar();
// c.isLeapYear() X -> O
// ๋
,์,์ผ -> ์ค์
// 1) sDay = sDay.of(2022, 8, 8);
// 2) LocalDate sDay = LocalDate.of(2022, 8, 8);
// sDay.with(field, newValue)
/*
sDay = sDay.withYear( 2022);
sDay = sDay.withMonth(8);
sDay = sDay.withDayOfMonth(8);
*/
sDay = sDay.with( ChronoField.YEAR , 2022);
sDay = sDay.with( ChronoField.MONTH_OF_YEAR , 8);
sDay = sDay.with( ChronoField.DAY_OF_MONTH , 8);
System.out.println( sDay );
ํน์ ์ผ์๋ก๋ถํฐ plus() minus()
// LocalDate ๊ธฐ๋
์ผ = sDay.plusDays(100);
LocalDate ๊ธฐ๋
์ผ = sDay.plus(100, ChronoUnit.DAYS);
System.out.println( ๊ธฐ๋
์ผ ); // 2022-11-16
LocalDate eDay = LocalDate.of(2023, 1, 18);
LocalDate d = eDay.minus(100, ChronoUnit.DAYS ); // ๊ฐ๋
์ฑ, ์ ์ง,๋ณด์ ์ฉ์ด
//LocalDate d = eDay.plus(-100, ChronoUnit.DAYS );
System.out.println( d ); // 2022-10-10
* temporalAmount : period, duration ํด๋์ค๋ฅผ ์ธ์๊ฐ์ผ๋ก ์ค์ผํจ
LocalDate
- get(), with(), plus(), minus() , now(), of()
- getXXX(), withXXX(), plusXXX(), minusXXX()
LocalTime
LocalTime t = LocalTime.now();
System.out.println( t ); // 11:40:23.589
// h
// int t.get( field );
// long t.getLong( filed );
int h = t.getHour();
//h = t.get( ChronoField.HOUR_OF_AMPM ); // 12( 0~ 11 )
h = t.get( ChronoField.HOUR_OF_DAY); // 24( 0~ 23 )
System.out.println( h );
// m
int m = t.getMinute();
m = t.get( ChronoField.MINUTE_OF_HOUR);
System.out.println( m );
// s
int s = t.getSecond();
s = t.get( ChronoField.SECOND_OF_MINUTE);
System.out.println( s );
// ms
int ms = t.getNano();
ms = t.get( ChronoField.NANO_OF_SECOND);
System.out.println( ms );
// p 556 ํ 10-4 get()
// p 556 ํ 10-5 TemporalField(ChronoField) ๋ชฉ๋ก
// 50๋ถ ๋ค์ ๋ง๋์.
t = t.plusMinutes(50);
System.out.println( t );
// 2์๊ฐ 30๋ถ ๋ค์ ๋ง๋์.
//t = t.plusHours(2);
// t = t.plusMinutes(30);
t = t.plusMinutes( 2*60 + 30);
// 30์ด ๋ค์ ๋ง๋์.
๋ ์ง/์๊ฐ ์ ์ญ
// System.out.println( Math.floor(3.14) );
LocalTime t = LocalTime.now();
System.out.println( t );
// truncate ๋์ ์๋ฅธ
// t.truncatedTo(unit);
// t = t.truncatedTo( ChronoUnit.HOURS ); // 12:00 ์๊ฐ ๋ฐ์ผ๋ก ๋ค ์งค๋ฆผ
t = t.truncatedTo( ChronoUnit.DAYS );
System.out.println( t );
๋ ์ง์ ์๊ฐ ๋น๊ต
boolean isAfter(), isBefore(), isEqual()
[ Instant ํด๋์ค ]
์ํฌํฌํ์( 1970-1-1 00:00:00 )๋ถํฐ ๊ฒฝ๊ณผ๋ ์๊ฐ์ ๋๋ ธ์ด ๋จ์๋ก ํํ
EPOCH TIME
[๋ฉ์๋]
Instant -> j.u.Date ํ ๋ณํ : Date from( Instant )
j.u.Date -> Instantํ ๋ณํ : Instant toInstant( Date )
p 561 LocalDateTime + Zone(ํ์์กด)
// ๋ ์ง + ์๊ฐ
LocalDateTime dt = LocalDateTime.now();
// LocalDateTime dt = LocalDateTime.of();
System.out.println( dt ); // 2022-09-08T12:26:10.550
/*
dt = dt.truncatedTo( ChronoUnit.DAYS );
System.out.println( dt ); // 2022-09-08T00:00
*/
// ๋
, ์,์ผ, ์,๋ถ,์ด,ms ๋ฑ๋ฑ ์ ๋ณด์ป์ด์ค๊ณ , ์ค์ ํ๊ณ , + , -
// dt.get() LocalDAte, LocalTime
//dt.getXXX()
// "2023.1.18 18:00:00"
LocalDateTime eDay = LocalDateTime.of(2023, 1, 18, 18, 0, 0);
System.out.println( eDay );
// dt.plus(null)
// dt.plusXXX(null)
// dt.minus(null)
// dt.minusXXX(null)
// dt.with( )
// dt.withXXX( )
// LocalDate , LocalTime -> LocalDateTime ๋ง๋ค ์ ์๋ค. p 561
// 1) LocalDateTime.of(date, time);
LocalDate d = LocalDate.now();
LocalTime t = LocalTime.now();
// 2) LocalDateTime dt2 = d.atTime(t);
// 3) LocalDateTime dt3 = t.atDate(d);
LocalDateTime dt4 = d.atStartOfDay(); // 00:00:00 ๋ก ์ค์
LocalDate -> atTime(t) -> LocalDateTime ๋ณํ
LocalTime -> atDate(d) -> LocalDateTime ๋ณํ
// LocalDateTime -> LocalDate, LocalTime ๋ณํ
LocalDateTime dt = LocalDateTime.now();
LocalDate d = dt.toLocalDate();
System.out.println( d );
LocalTime t = dt.toLocalTime() ;
System.out.println( t );
Temporal + Adjusters
์๊ฐ์ ์กฐ์ ์ฅ์น
์์ฃผ ์ฌ์ฉ๋๋ ๋ ์ง,์๊ฐ์ ์ฒ๋ฆฌํด์ฃผ๋ ๋ฉ์๋๋ฅผ ๋ฏธ๋ฆฌ
์ ์ํด ๋์ ํด๋์ค - Temporal + Adjusters
TemporalAdjusters ์ ๋ฉ์๋
- firstDayOfXXX๋ ,์ - ์ฒซ๋ฒ์งธ๋
- lastDayOfXXX - ๋ง์ง๋ง๋
- firstInMonth(DayOfWeek) - ์ด๋ฒ๋ฌ ์ฒซ์งธ๋
- lastInMonth() - ์ด๋ฒ๋ฌ ๋ง์ง๋ง๋
์ง์ ๊ตฌํ
public class DayAfterTomorrow implements TemporalAdjuster {
@Override
public Temporal adjustInto(Temporal temporal) {
return temporal.plus(2, ChronoUnit.DAYS); // 2์ผ +
}
}
public static void main(String[] args) {
//
LocalDate today = LocalDate.now();
//System.out.println( today );
// with() ๋ ์ง ์กฐ์ (์์ )
// today.plus(2, ChronoUnit.DAYS);
LocalDate date = today.with( new DayAfterTomorrow() );
// System.out.println(date);
p( today );
p( date );
// ์ด ๋ฌ์ ๋ง์ง๋ง ๋
p( today.with( lastDayOfMonth() ));
// Math.abs(10);
// abs(-10);
// p 566 ์์ 10-24 ๋จ์ ์ฝ๋ฉ ํด ๋ณด๊ธฐ.
} // main
private static void p(Object obj) {
System.out.println( obj );
}
* p ๋ฉ์๋ createํ๋ฉด ์๋์ถ๋ ฅ ๋ฉ์๋ ๋ง๋ค์ด์ง
* import
๊ณ์ฐํ๊ธฐ ์ํ ํด๋์ค
Period : ๋ ์ง - ๋ ์ง ( ๋ ์ง์ ์ฐจ์ด )
Duration : ์๊ฐ -์๊ฐ ( ์๊ฐ์ ์ฐจ์ด)
1) Period (between = static๋ฉ์๋, until = ์ธ์คํด์ค๋ฉ์๋)
// between()
LocalDate d1 = LocalDate.of( 2014, 1, 1);
LocalDate d2 = LocalDate.of( 2015, 12, 31);
// d1 - d2 ๋ ์ง ์ฐจ์ด(๊ฐ๊ฒฉ)
// Period p = new X
// ํฌํจํ๋๊ฑฐ๋ผ๋ฉด d2 = d2.plus(1, ChronoUnit.DAYS);
Period p = Period.between(d1, d2); // 14.1.1 <= < 15.12.31
// Period p = d1.until(d2); ๋์ผ ๊ธฐ๋ฅํ๋ ์ฝ๋ฉ.
System.out.println( p );
System.out.println( p.getYears() );
System.out.println( p.getMonths() );
System.out.println( p.getDays() );
System.out.println( p.toTotalMonths() ); // 23๊ฐ์
// P1Y11M30D
// 1*12 + 11 +30์ผ
// 23
/* ๊ฐ์์๋ก ๋ฐ๊พธ๊ธฐ (์ ์ดํด๋ฅผ ๋๊ธฐ ์ํจ)
Period p2 = Period.of(1, 2, 31); // ์ฐ,์,์ผ์ ๋งค๊ฐ๋ณ์๋ก ์ค, 1๋
2๊ฐ์ 31์ผ
p2 = p2.withYears(2); // 2๋
์ผ๋ก ์์
p2 = p2.plusYears(3);
System.out.println( p2 );
System.out.println( p2.toTotalMonths() ); // 62๊ฐ์
*/
2) Duration
LocalTime t1 = LocalTime.now();
LocalTime t2 = LocalTime.of(12, 34, 56);
// t1.until(t2, null)
Duration d = Duration.between(t1, t2);
System.out.println( d );
// PT-2H-8M-24.754S
System.out.println( d.getNano() );
System.out.println( d.getSeconds() );
// ์๊ฐ/๋ถ
// System.out.println( d.get(ChronoUnit.HOURS)); // Unsupported unit: Hours
long hour = Math.abs( d.getSeconds() ) / ( 60*60);
System.out.println( hour );
// ๋ถ
/*
System.out.println( d.toMinutes() );
System.out.println( d.toHours() );
System.out.println( d.toMinutes() - d.toHours()*60 );
*/
// "PT-2H-8M-24.754S"
System.out.println( Integer.parseInt( d.toString().split("-")[2].replaceAll("[^0-9]", "") )); // 2H
// Period p = Period.between( d1,d2 )
// p.getXXX();
// p.get();
// p.of();
// p.ofXXX();
// Duration d = Duration.between( t1, t2);
// d.getXXX();
// d.get( field );
// d.of();
// d.ofXXX()
DateTimeFormatter
LocalDate d = LocalDate.now();
System.out.println( d ); // 2022-09-08
String pattern = "yyyy๋
M์ d์ผ";
DateTimeFormatter dtf = DateTimeFormatter.ofPattern(pattern);
// ํจํด ๊ธฐํธ
String sd = dtf.format(d);
System.out.println( sd );
// String -> LocalDate ๋ณํ
String sd = "2023๋
07์ 29์ผ ํ ";
// LocalDate d =??
String pattern = "yyyy๋
MM์ dd์ผ E";
DateTimeFormatter dtf = DateTimeFormatter.ofPattern(pattern);
// LocalDate, LocalDateTime<- TemporalAccessor dtf.parse(sd); X
// LocalDate d2 = LocalDate.parse(sd, dtf);
LocalDateTime d2 = LocalDateTime.parse( sd, dtf );
System.out.println( d2 );
String sd = "2023๋
07์ 29์ผ ํ 12:34:55"; // ์๊ฐ X
String pattern = "yyyy๋
MM์ dd์ผ E HH:mm:ss";
DateTimeFormatter dtf = DateTimeFormatter.ofPattern(pattern);
// ๋ ์ง + ์๊ฐ
LocalDateTime d2 = LocalDateTime.parse( sd, dtf );
System.out.println( d2 );
// [ j.t ํต์ฌํด๋์ค ]
// Date, Calendar ํด๋์ค
// DateTimeFormatter dtf ํจํด์ง์ -> ํ์ฑ, ํฌ๋งท
LocalDate d = LocalDate.parse( "2022-12-31" ); // O
// ์ ? ๋ ์งํ์์ ๋ฌธ์์ด์ด ISO_LOCAL_DATE ์ ๊ฐ์ ํ์์ด๋ผ๋ฉด
// dtf๋ฅผ ์ฌ์ฉํ์ง ์๊ณ ๋ ํ์ฑ์ด ๊ฐ๋ฅํ๋ค.
// LocalDate d = LocalDate.parse( "2022/12/31" ); X
// LocalDate d = LocalDate.parse( "2022.12.31" ); X
// LocalDate d = LocalDate.parse( "2022๋
12์31์ผ" ); X
System.out.println( d );
LocalTime t = LocalTime.parse("12:10:23");
System.out.println( t );
LocalDateTime dt = LocalDateTime.parse("2022-12-31T12:10:23");
System.out.println( dt );
//ํ์ํ ํด๋์ค String format()/ parse(s)
// DF
// SDF
// CF
// MF
// j.t
// DTF
์ต๊ทผ๋๊ธ