[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

 

  • ๋„ค์ด๋ฒ„ ๋ธ”๋Ÿฌ๊ทธ ๊ณต์œ ํ•˜๊ธฐ
  • ๋„ค์ด๋ฒ„ ๋ฐด๋“œ์— ๊ณต์œ ํ•˜๊ธฐ
  • ํŽ˜์ด์Šค๋ถ ๊ณต์œ ํ•˜๊ธฐ
  • ์นด์นด์˜ค์Šคํ† ๋ฆฌ ๊ณต์œ ํ•˜๊ธฐ