※ java 8 이전
-Date 객체 사용
-Calendar 클래스의 getInstance() 메소드 사용
-System 클래스의 currentTimeMillis() 메소드 사용
-원하는 포맷의 문자열로 변환 SimpleDateFormat 사용
※ Java 8 이후
-java.time.LocalDate (날짜)
-java.time.LocalTime (시간)
-java.time.LocalDateTime (날짜+시간)
원하는 포맷의 문자열로 변환 DateTimeFormatter 사용
package practice0509Test;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
public class testsdg {
public static void main(String[] args) {
LocalDate currentDate = LocalDate.now();
LocalTime currentTime = LocalTime.now();
DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyMMdd");
String formattedDate = currentDate.format(dateFormatter);
DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("HHmmss");
String formattedTime = currentTime.format(timeFormatter);
String formattedDateTime = formattedDate + formattedTime;
System.out.println(formattedDateTime);
System.out.println("----------------------");
LocalDateTime now = LocalDateTime.now();
System.out.println(now);
String formatedNow = now.format(DateTimeFormatter.ofPattern("yyyy년 MM월 dd일 HH시 mm분"));
System.out.println(formatedNow);
System.out.println("----------------------");
Date nows = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yy년MM월dd일/ HH시mm분ss초");
String formattedDates = sdf.format(nows);
System.out.println(formattedDates);
}
}
https://dev-coco.tistory.com/31 이사이트 설명 잘해놓음
'문제풀다가 알게 된 것들 > JAVA' 카테고리의 다른 글
Collections.reverse ==> arraylist 역순 (0) | 2023.07.09 |
---|