기억 저장소

클라우드 기반 인공지능 개발과 DevOps 실무

프론트엔드/안드로이드 스튜디오

안드로이드 스튜디오 : 두 날짜간의 차 빼기 / 더하기/Incompatible types. Found: 'long', required: 'java.lang.String'

하늘.98 2022. 3. 23. 10:04

 

 

prase 를 사용 하는 경우는 try 문안에 넣어줘야 한다. 

이렇게 했을경우 

Incompatible types. Found: 'long', required: 'java.lang.String' 이런 오류가 날 것이다 

잘못된 코드!!!! 

이럴 경우예는 long으로 

빼고나는 변수 값을 duration 을 String으로 했기 때문에 나는 오류일 것이다.

데이터를 빼고나서는 long 인 변수로 받아줘야된다!

try {

    Date rightNow = new Date();

    SimpleDateFormat formatter = new SimpleDateFormat("HH:mm");

    dateString = formatter.format(rightNow);
    txt2.setText(dateString);


    Date startDate = formatter.parse(getTime);
    Date endDate = formatter.parse(dateString);
    duration=endDate.getTime()-startDate.getTime();

   // txt3.setText(duration);



}catch (Exception e){

}

----------------------------------------------------------------------------------------------------------------

 

 

똑바로 된 코드

 

try {

    Date rightNow = new Date();

    SimpleDateFormat formatter = new SimpleDateFormat("HH:mm");

    dateString = formatter.format(rightNow);
    txt2.setText(dateString);


    Date startDate = formatter.parse(getTime);
    Date endDate = formatter.parse(dateString);
    long duration=endDate.getTime()-startDate.getTime();

   // txt3.setText(duration);



}catch (Exception e){

}