반응형
Notice
Recent Posts
Recent Comments
Link
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | ||||
| 4 | 5 | 6 | 7 | 8 | 9 | 10 |
| 11 | 12 | 13 | 14 | 15 | 16 | 17 |
| 18 | 19 | 20 | 21 | 22 | 23 | 24 |
| 25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- 티스토리챌린지
- 오블완
- 혼공얄코
- 프로그래머스
- 둘만의 암호
- SQL Mapper
- 멀티태스킹
- java
- 다형성
- over()
- 자바의 정석
- 쿠키
- 오버로딩
- StringBuffer
- 둘만의 암호 자바
- 자바의정석
- StringBuilder
- 오버라이딩
- spring security 설정
- 프로그래머스 둘만의 암호
- CPU
- LocalDate
- 리눅스
- 멀티프로세싱
- 캡슐화
- spring security
- localtime
- hackerrank
- 입출력
- 백트래킹
Archives
- Today
- Total
쉽게 쉽게
[Java] LocalTime , LocalDate의 타입변환 본문
반응형
▤ 목차
1. LocalTime 변환
1. String to LocalTime
String 타입을 LocalTime 타입으로 변환
1. ofSecondOfDay 사용
// 1. ofSecondOfDay 사용
String time1 = "04:05";
String[] parts = n.split(":");
int minutes = Integer.parseInt(parts[0]);
int seconds = Integer.parseInt(parts[1]);
// 1. 총 초로 변환
int totalSeconds = (minutes * 60) + seconds;
// 2. ofSecondOfDay 메서드를 활용
LocalTime posTime = LocalTime.ofSecondOfDay(totalSeconds); // 00:04:05
2. DateTimeFormatter 사용
// 2. DateTimeFormatter 사용
String time2 = "14:30:55";
// 1. 사용할 날짜/시간 형식 정의
// H: 시 (0-23), m: 분, s: 초
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss");
// 2. 문자열을 LocalTime 객체로 파싱(Parse)
LocalTime localTime = LocalTime.parse(timeString, formatter);
2. LocalTime to String
LocalTime 타입을 String 타입으로 변환
1. ofSecondOfDay 사용
// 1. toSecondOfDay 사용
LocalTime localTime = LocalTime.of(4,5); // 04:05
// 1. 총 초로 변환
int totalSeconds = localTime.toSecondOfDay();
int minutes = totalSeconds / 60;
int seconds = totalSeconds % 60;
// 2. format() 메서드를 사용하여 String으로 변환
String n = String.format("%02d:%02d", minutes, seconds); // "04:05"
2. DateTimeFormatter 사용
// 2. DateTimeFormatter 사용
LocalTime specificTime = LocalTime.of(15, 45, 10); // 오후 3시 45분 10초
// 1. 원하는 시간 형식(패턴) 정의
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss");
// 2. format() 메서드를 사용하여 String으로 변환
String formattedTime = specificTime.format(formatter);
2. LocalDate 변환
1. Stirng to LocalDate
String 타입을 LocalDate 타입으로 변환
// DateTimeFormatter 사용
String today = "2025.09.29";
// 1. 원하는 시간 형식(패턴) 정의
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy.MM.dd");
// 2. 문자열을 LocalDate 객체로 파싱(Parse)
LocalDate now = LocalDate.parse(today, formatter); /* 2025.09.29 */
2. LocalDate to Stirng
LocalDate 타입을 String 타입으로 변환
// DateTimeFormatter 사용
LocalDate date = LocalDate.of(2025, 12, 17);
// 1. 원하는 시간 형식(패턴) 정의
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy년 MM월 dd일");
// 2. format() 메서드를 사용하여 String으로 변환
String formattedString = date.format(formatter); // "2025년 12월 17일"
| 잘못된 내용이 있다면 지적부탁드립니다. 방문해주셔서 감사합니다. |

반응형
'Java > Java' 카테고리의 다른 글
| [Java/알고리즘] 시간복잡도 계산 (0) | 2026.01.10 |
|---|---|
| [Java/알고리즘] 비트 연산자 사용법 (1) | 2026.01.02 |
| [Java] 자주 사용하는 타입(String, Int, Char) 변환 방법 (0) | 2025.12.02 |
| [Java] 배열, 컬렉션 정렬 방법 (0) | 2025.11.24 |
| [Java] 피보나치 수열 구현 (0) | 2025.11.13 |
