[Android] 문자열 날짜를 로컬 날짜로 변경하기 - SimpleDateFormat

원준·2023년 7월 27일

Android Studio

목록 보기
35/40

SimpleDateFormat ?

  • 저장된 날짜가 문자열인 경우가 많은데, 해당 날짜를 로컬을 기준으로 바꿔서 보여주게 해주는 방법이다!

아니 사용은 ?

SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); // 기존 데이터 모양
SimpleDateFormat df = new SimpleDateFormat("yyyy년MM월dd일 HH:mm"); // 바꿀 날짜 모양
sf.setTimeZone(TimeZone.getTimeZone("UTC"));
df.setTimeZone(TimeZone.getDefault()); // 로컬을 들고옴

Date date = sf.parse(post.getCreatedAt()); // 자바가 이해할 수 있게 시간으로 변경
	// 변환할 날짜를 들고온다.
String localTime = df.format(date); //변환한 날짜를 다시 문자열로 변환해 최종적으로 바뀐 로컬로 사용가능
profile
공부해보자

0개의 댓글