SimpleDateFormat 및 Calendar 객체를 사용하여 날짜를 수정하려고합니다. 그런 다음 수정 된 날짜를 내 데이터베이스에 씁니다. 문제는 내 메서드가 컴파일되지 않는다는 것입니다. 그것은 말합니다 : 호환되지 않는 유형 문자열은 날짜로 변환 할 수 없습니다. 내가 놓친 게 무엇입니까? 이 내 방법Java : 날짜 조작
public Date expiredItem() {
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Calendar expiredDate = Calendar.getInstance();
expiredDate.add(Calendar.DATE, Integer.parseInt(timeFld.getText()));
//timeFld is JTextField.getText() casted into int
// The confusing thing is, below Dialog works as i expected but not on my
// method return.
JOptionPane.showMessageDialog(null, "Expiration Date: " +
dateFormat.format(expiredDate.getTime()));
return dateFormat.format(expiredDate.getTime()); //Erroneous code
}
방금 반환 문이 String임을 알았지 만 내 메서드는 Date를 반환합니다. 하지만 어떻게하면 내 메서드가 formattedDate를 반환하도록 고칠 수 있습니까? –
'java.util.Date' 타입의 객체를 얻고 싶다면'expiredDate.getTime());'Simple을 반환하십시오. 여기에서 포맷 할 필요가 없습니다.또는 문자열을 원하면 형식을 지정하고 메서드의 반환 유형을 변경합니다. –
메서드의 반환 유형을 문자열로 변경 하시겠습니까? –