프로그래밍/Java
자바 String타입을 Date타입으로 변환(string to date)
허니팁
2019. 4. 28. 16:53
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
반응형