: String 변수명 = new String("문자열");
ex)
: Date 변수명 = new Date();
package pack_Date;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
public class Main {
public static void main(String[] args) {
Date date = new Date();
System.out.println(date);
String txt = date.toLocaleString();
System.out.println("txt => " + txt);
System.out.println("\n날짜만 표기");
System.out.println(txt.substring(0, 13));
System.out.println("\n시간만 표기");
System.out.println(txt.substring(13)); //13부터 마지막까지~
LocalDate localDate = LocalDate.now();
int year = localDate.getYear();
int month = localDate.getMonthValue();
int day = localDate.getDayOfMonth();
System.out.println();
String dateRes = year + "-" + month + "-" + day;
System.out.println(dateRes);
System.out.println("\n\n--------------------\n\n");
LocalDateTime myDateObj = LocalDateTime.now();
System.out.println("Before formatting : " + myDateObj); //format => 형식
//형식 적용 시작
DateTimeFormatter myFormatObj
= DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss a");
//or "dd/MM/yyyy HH:mm:ss a"
//a => 오전/오후
String formattedDate = myDateObj.format(myFormatObj);
System.out.println("After formatting : " + formattedDate);
}
}
출력 결과)
Number Format : NumberFormat 변수명 = NumberFormat.getInstance();
메인클래스 Main)
package pack_Form;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("세전급여를 입력해 주세요(단위. 원) : ");
int money = scanner.nextInt();
scanner.close();
Money obj = new Money(money);
String salary = obj.getSalary();
System.out.print("실지급액 : " + salary + "원");
}
}
커스텀클래스 Money)
package pack_Form;
public class Money {
private int money;
public Money(int money) {
this.money = money;
}
public String getSalary() {
double moneyParam = money - (money * 0.033);
Format objFormat = new Format(moneyParam);
String salary = objFormat.mtd_setForm();
return salary;
}
}
커스텀클래스 Format)
package pack_Form;
import java.text.NumberFormat;
public class Format {
private double money;
public Format(double money) {
this.money = money;
}
public String mtd_setForm() {
NumberFormat nf = NumberFormat.getInstance();
String salary = nf.format(this.money);
return salary;
}
}
package pack_String;
import java.util.Scanner;
public class StringBuiltIn {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("주민등록번호 입력 : ");
String num = scanner.next();
num = num.trim(); //입력값 앞뒤 공백 제거
////////// 성별 구분 코드 //////////
String genderCode = num.substring(7, 8);
String gender = "";
if (Integer.parseInt(genderCode) % 2 == 0) {
//Integer.parseInt => String 타입의 숫자를 int 타입으로 변환!!
gender = "Female";
} else {
gender = "Male";
}
////////// 나이 산출 코드 //////////
String yearPrefix = "";
switch (genderCode) {
case "1", "2":
yearPrefix = "19";
break;
case "3", "4":
yearPrefix = "20";
break;
}
String birthYear = yearPrefix + num.substring(0, 2);
// LocalDate obj = LocalDate.now(); //많이 씀!
int age = 2021 - Integer.parseInt(birthYear) + 1;
System.out.println("나이 : " + age);
System.out.println("성별 : " + gender);
scanner.close();
}
}
package pack_RandomQuiz;
public class Main {
public static void main(String[] args) {
System.out.println("1부터 10사이의 난수 3개 출력");
double[] rndNum = new double[3];
int i = 0;
while (true) { //무한 순환
rndNum[i] = Math.floor(10*Math.random()+1);
i++;
if(i == 3) { //i가 3일 때 아래 내용 진행!
if(! (rndNum[0] == rndNum[1]
|| rndNum[1] == rndNum[2]
|| rndNum[2] == rndNum[0]) ){
System.out.print((int)rndNum[0] + " ");
System.out.print((int)rndNum[1] + " ");
System.out.println((int)rndNum[2]);
break;
} else {
i = 0;
}
}
}
}
}