Dev.Back-End/JAVA
[JAVA] String <-> Date 형변환
Dev.GA
2018. 2. 13. 10:06
[JAVA] String to Date, Date to String 형변환
개발하면서 자꾸 왔다갔다 헷깔리는 녀석
시간데이터를 구할때 String과 Date의 형변환이다.
1 2 3 4 5 6 7 8 9 10 11 12 | /** Date Format **/ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); /** String to Date **/ Date date1 = sdf.parse("2018-01-31"); /** Date to String **/ Date now = new Date(); String date2 = sdf.format(now); System.out.println("date1 : " + date1); System.out.println("date2 : " + date2); | cs |
주의 할 점은 String으로 받은 값이 SimpleDateFormat의 format과 일치해야한다는 점이다.