본문 바로가기

Java & JSP

[Java] 날짜 더하기, String<->Date 변환, Calender->String 변환

728x90
반응형

날짜 더하기


SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

Date date = format.parse("날짜입력");


Calender cal = Calender.getInstance();

cal.setTime(date);

cal.add(Calender.DATE, 일수);

cal.add(Calender.MONTH, 월수);


String <-> Date 변환

1. String To Date

String from = "2018-09.06 11:11:11";

SimpleDateFormat fm = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

Date to = fm.parse(from);


2. Date To String

Date from = new Date();

SimpleDateFormat fm = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

String to = fm.format(from);


Calender -> String 변환

// SimpleDateForamt, Calender 값 있을 경우

String strDate = fm.format(cal.getTime());


728x90
반응형