[Java] 12์ผ์ฐจ : ์๋ฐ์ ๊ธฐ๋ณธ API ํด๋์ค 6
Arrays
๋ฐฐ์ด ์กฐ์ ๊ธฐ๋ฅ์ ๊ฐ์ง๊ณ ์์ผ๋ฉฐ, ๋ชจ๋ ๋ฉ์๋๊ฐ static์ผ๋ก ์ ์๋์ด ๋ฐ๋ก ์ฌ์ฉ ๊ฐ๋ฅํ๋ค. ๋จ์ํ ๋ฐฐ์ด ๋ณต์ฌ๋ System,arraycopy() ๋ฉ์๋๋ฅผ ์ฌ์ฉํ ์ ์์ผ๋, Arrays๋ ๋ฐฐ์ด ๋ณต์ฌ ๋ฐ ์ถ๊ฐ์ ์ผ๋ก ํญ๋ชฉ ์ ๋ ฌ, ํญ๋ชฉ ๊ฒ์, ํญ๋ชฉ ๋น๊ต์ ๊ฐ์ ๊ธฐ๋ฅ์ ์ ๊ณตํ๋ค.
package ch07.unit08;
import java.util.Arrays;
import java.util.Collections;
public class Ex01_Arrays {
public static void main(String[] args) {
String s;
String[] ss = new String[] {"์์ธ","๋ถ์ฐ","๋๊ตฌ","์ธ์ ","๊ด์ฃผ","๋์ ","์ธ์ฐ","์ธ์ข
"};
//Arrays : ๋ฐฐ์ด์ ๊ด๋ จ๋ ๋ฉ์๋ ์ ๊ณต
s = Arrays.toString(ss); //
// toString : ๋ฐฐ์ด์ ๊ฐ ์์๋ฅผ "[๊ฐ1, ๊ฐ2...]" ํ์์ ๋ฌธ์์ด๋ก ๋ณํ
System.out.println(s);
// ์ค๋ฆ์ฐจ์ ์ ๋ ฌ
Arrays.sort(ss);
s = Arrays.toString(ss); // [๊ด์ฃผ, ๋๊ตฌ, ๋์ , ๋ถ์ฐ, ์์ธ, ์ธ์ข
, ์ธ์ฐ, ์ธ์ ]
System.out.println(s);
// ๋ด๋ฆผ์ฐจ์
Arrays.sort(ss, Collections.reverseOrder());;
s = Arrays.toString(ss); // [์ธ์ , ์ธ์ฐ, ์ธ์ข
, ์์ธ, ๋ถ์ฐ, ๋์ , ๋๊ตฌ, ๊ด์ฃผ]
System.out.println(s);
//Object : ๋ชจ๋ ํด๋์ค์ ์ต์์ ํด๋์ค๋ก
// ๋ฌธ์์ด, ์ ์, ์ค์ ๋ฑ ๋ชจ๋ ์๋ฃํ ๊ฐ์ ๊ฐ์ง ์ ์๋ค.
// Object [] oo = {"์๋ฐ",70,"์คํ๋ง",80}; // ๋ฐฐ์ด์ ์๋ก ๋ค๋ฅธ ์๋ฃํ์ด ์กด์ฌ
// Arrays.sort(oo); // ๋ฐ์ค
// ๋ฐฐ์ด์ ์๋ก ๋ค๋ฅธ ์๋ฃํ์ด ์กด์ฌํ๋ฏ๋ก
}
}
package ch07.unit08;
import java.util.Arrays;
public class Ex02 {
public static void main(String[] args) {
String[] ss = new String[] {"์์ธ","๋ถ์ฐ","๋๊ตฌ","์ธ์ ","๊ด์ฃผ","๋์ ","์ธ์ฐ","์ธ์ข
"};
int idx;
// ์ด์ง๊ฒ์ = ์ ๋ ฌ๋์ด ์์ง ์์ผ๋ฉด ๊ฒ์ ๋ถ๊ฐ
// idx = Arrays.binarySearch(ss, "์์ธ");
// System.out.println(idx);
// 1) ์ ๋ ฌ ๋จผ์ ํ๊ณ
Arrays.sort(ss);
System.out.println(Arrays.toString(ss));
// 2) ๊ฒ์
idx = Arrays.binarySearch(ss, "์์ธ"); // 4 : ์ธ๋ฑ์ค ๋ฐํ
System.out.println(idx);
idx = Arrays.binarySearch(ss, "๊ฒฝ๊ธฐ"); // -1 : ์์
System.out.println(idx);
}
}
package ch07.unit08;
import java.util.Arrays;
public class Ex03 {
public static void main(String[] args) {
String[] ss = new String[] {"์์ธ","๋ถ์ฐ","๋๊ตฌ","์ธ์ ","๊ด์ฃผ","๋์ ","์ธ์ฐ","์ธ์ข
"};
// ๋ฐฐ์ด๋ณต์ฌ-1
String[] ss2 = Arrays.copyOf(ss, ss.length);
System.out.println(ss == ss2); // false
System.out.println(Arrays.toString(ss2));
// ๋ฐฐ์ด๋ณต์ฌ -2
// ๋ณต์ฌ๋์, form, to-1
String[] ss3 = Arrays.copyOfRange(ss, 1, 4);
System.out.println(Arrays.toString(ss3)); // [๋ถ์ฐ, ๋๊ตฌ, ์ธ์ ]
// ๋ฐฐ์ด ๋ณต์ฌ-3 // arraycopy()๋ Arrays.copyOf() ๋ณด๋ค ์ฑ๋ฅ ์ข์.
String[] ss4 = new String[ss.length];
System.arraycopy(ss, 0, ss4, 0, ss.length);
System.out.println(Arrays.toString(ss4));
}
}
package ch07.unit08;
import java.util.Arrays;
public class Ex04 {
public static void main(String[] args) {
int []a = {1,2,3};
int []b = {1,2,3};
int []c = {1,2,3,4,5};
// ์ฃผ์ ๋น๊ต
System.out.println(a == b); // false
System.out.println(a == c); // false
System.out.println(a.equals(b)); // false
// ๋ฐฐ์ด์ ๊ฐ๋น๊ต
System.out.println(Arrays.equals(a, b)); // true
System.out.println(Arrays.equals(a, c)); // false
// 2์ฐจ์ ๋ฐฐ์ด
int [][] aa = {{1,2,3}, {4,5,6}};
int [][] bb = {{1,2,3}, {4,5,6}};
// Arrays.equals() : 2์ฐจ์ ๋ฐฐ์ด์์๋ 1์ฐจ์์ด ๊ฐ์ง ์ฃผ์๋น๊ต(์์๋น๊ต)
System.out.println(Arrays.equals(aa, bb)); // false
// Arrays.deepEquals() : 2์ฐจ์ ๋ฐฐ์ด์์ 2์ฐจ์์ ๊ฐ๋น๊ต(๊น์๋น๊ต)
System.out.println(Arrays.deepEquals(aa, bb)); // true
}
}
LocalDate
๋ก์ปฌ ๋ ์ง ํด๋์ค๋ก ๋ ์ง ์ ๋ณด๋ง ํ์ ํ ๋ ์ฌ์ฉํ๋ค.
package ch07.unit09;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.temporal.ChronoUnit;
// LocalDate ์ฐ๊ธฐ
public class Ex01_LocalDate {
public static void main(String[] args) {
// ์์คํ
๋ ์ง
LocalDate date1 = LocalDate.now();
System.out.println("ํ์ฌ ๋ ์ง : " + date1);
// String s = date1; // ์๋จ
String s = date1.toString();
System.out.println(s);
// ๋ ์ง ์ค์
LocalDate date2 = LocalDate.of(2024, 5, 8); // m์ -1ํ ํ์ ์์
System.out.println(date2);
// ์์คํ
์๊ฐ
LocalTime time1 = LocalTime.now();
s = time1.toString();
System.out.println(s); // 14:37:15.849691300
// ์๊ฐ ์ค์
LocalTime time2 = LocalTime.of(17, 50, 0, 0);
System.out.println(time2); // 17:50
// ์์คํ
๋ ์ง ๋ฐ ์๊ฐ
LocalDateTime dt = LocalDateTime.now();
System.out.println(dt);
// 2024-02-29T14:38:19.085077600
String[] ss = dt.toString().split("T"); // dt๋ ๋ฌธ์์ด์ด ์๋๋ฏ๋ก toString์ผ๋ก ๋ฐ๊พผ ํ ๋ถ๋ฆฌ
System.out.println(ss[0] + " " + ss[1]); // 2024-02-29 14:40:46.453227200
// ๋ ์ง ๋ฐ ์๊ฐ์ ์ค์
LocalDateTime dt2 = LocalDateTime.of(2024, 5, 8, 11,10,0,0); // 0์ด๋ฉด ์ด, ๋ฐ๋ฆฌ์ด ์๋์ด
System.out.println(dt2); // 2024-05-08T11:10
// ํน์ ์์ ์ ํ์ ์คํ
ํ ์ป๊ธฐ
Instant i1 = Instant.now();
try {
Thread.sleep(10); // ์ ์ ๋ฉ์ถ๊ธฐ
} catch (Exception e) {
}
Instant i2 = Instant.now();
if(i1.isBefore(i2)) {
System.out.println("i1์ด ๋น ๋ฆ
๋๋ค.");
} else if(i1.isAfter(i2)) {
System.out.println("i1์ด ๋๋ฆฝ๋๋ค.");
} else {
System.out.println("๋์ผ");
} // ๋ ํ์์ ์ฐจ์ด ๋๋
ธ์ธ์ปจ๋ ๊ณ์ฐ
System.out.println("์ฐจ์ด : " + i1.until(i2, ChronoUnit.NANOS));
}
}
LocalTime
๋ก์ปฌ ์๊ฐ ํด๋์ค๋ก ์๊ฐ ์ ๋ณด๋ง ํ์ ํ ๋ ์ฌ์ฉ ํ๋ค.
package ch07.unit09;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
public class Ex02 {
public static void main(String[] args) {
LocalDateTime now = LocalDateTime.now();
int y = now.getYear();
int m = now.getMonthValue();
int d = now.getDayOfMonth();
DayOfWeek week = now.getDayOfWeek();
String w = week.toString(); // ์์ด๋ก ๋์ด
int h = now.getHour();
int mi = now.getMinute();
int s = now.getSecond();
int n = now.getNano();
System.out.printf("%d-%02d-%d \n", y,m,d); // ํ์๋ฆฌ๋ง ์์๋ ์์ 0๋ถ์ด๊ธฐ
System.out.printf("%d-%02d-%d %02d:%02d:%02d.%d %s\n", y,m,d,h,mi,s,n, w); // ํ์๋ฆฌ๋ง ์์๋ ์์ 0๋ถ์ด๊ธฐ
LocalDate date = now.toLocalDate(); // ํ์ฌ ๋ ์ง. ์ค๋
๋ฉ์๋ isLeapYear๋ LocalDate์ ์์.
System.out.println(date);
if(date.isLeapYear()) {
System.out.println("์ค๋
์ ํด์
๋๋ค.");
} else {
System.out.println("์ค๋
์ด ์๋๋๋ค.");
}
LocalDate date2 = LocalDate.of(2024, 5, 5);
// ๋
๋๊ฐ ๋ค๋ฅด๋ฉด ๋
๋์ฐจ์ด, ๋
๋๊ฐ ๋ค๋ฅด๋ฉด ์์ฐจ์ด, ์์ด ๊ฐ์ผ๋ฉด ์ผ์ฐจ์ด
int diff = LocalDate.now().compareTo(date2); // 2-5 = -3
System.out.println(diff); //
}
}
package ch07.unit09;
import java.time.LocalDateTime;
public class Ex03 {
public static void main(String[] args) {
LocalDateTime now = LocalDateTime.now();
System.out.println(now); //
// ๋ ์ง ๊ณ์ฐ
LocalDateTime dt = now
.plusYears(1) // 1๋
๋ํ๊ณ
.minusMonths(3) // 3๊ฐ์ ๋นผ๊ธฐ
.plusDays(3) //
.plusHours(2);
System.out.println(dt);
}
}
DateTimeFormatter
java.util.DateFormat ํด๋์ค๋ฅผ ๋์ฒดํ ์ฉ๋๋ก ๋ง๋ค์ด์ก๋ค. ํ์ ํธํ์ฑ ๋๋ฌธ์ java.util.DateFormat ํ์ํ ๊ฒฝ์ฐ์๋ formatter.toFormat() ๋ฉ์๋๋ฅผ ํธ์ถํ๋ฉด ๋๋ค. ์ถ๋ ฅ ํ์์ ์ง์ ์ง์ ํ๊ธฐ ์ํด์๋ ofPattern() ์ ์ ๋ฉ์๋๋ฅผ ์ฌ์ฉํ๋ค.
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd")
/**
* ๋ฌธ์์ด์ LocalDate ๊ฐ์ฒด๋ก ๋ณํํ๋ ๋ฉ์๋
* @param date ๋ณํํ ๋ ์ง(String)
* @return ๋ณํ ํ ๋ ์ง(LocalDate)
*/
public LocalDate toLocalDate(String date) {
LocalDate result = null;
if( ! isValidDate(date)) {
throw new IllegalArgumentException("๋ ์ง ํ์ ์ค๋ฅ : " + date);
}
try {
date = date.replaceAll("\\-|\\/|\\.", "");
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyMMdd");
result = LocalDate.parse(date, dtf);
} catch (Exception e) {
throw new IllegalArgumentException("๋ ์ง ํ์ ์ค๋ฅ : " + date);
}
return result;
}