728x90
반응형
자바 string타입을 date타입으로 변환하는 법에 대해 알아보겠습니다.
ex) yyyymmdd
String sDate = "20190428";
String eDate = "20190505";
String startDate = "";
String endDate = "";
SimpleDateFormat beforeFormat = new SimpleDateFormat("yyyymmdd");
//Date로 변경하기 위해 날짜 형식을 yyyy-mm-dd로 변경
SimpleDateFormat afterFormat = new SimpleDateFormat("yyyy-mm-dd");
Date tempDate = null;
Date tempDate2 = null;
//yyyymmdd로된 날짜 형식으로 java.util.Date객체를 만듬
tempDate = beforeFormat.parse(sDate);
tempDate2 = beforeFormat.parse(eDate);
//Date를 yyyy-mm-dd 형식으로 변경하여 String으로 반환
startDate = afterFormat.format(tempDate);
endDate = afterFormat.format(tempDate2);
System.out.println("startDate: " + startDate); // 2019-04-28
System.out.println("endDate: " + endDate); // 2019-05-05
728x90
반응형
'프로그래밍 > Java' 카테고리의 다른 글
자바 java.lang.UnsupportedClassVersionError: Bad version number in .class file 오류 해결하기 (2) | 2021.01.10 |
---|---|
윈도우 class파일 jar로 압축하기 (1) | 2020.08.30 |
java.net.ConnectException : Connection refused 에러 해결하기 (0) | 2019.02.02 |
Resource is out of sync with the file system 해결하기 (0) | 2018.11.03 |
eclipse 이클립스 디버깅에서 class파일이 실행되는 경우 (0) | 2018.11.03 |