Android

[Android, Java] 마지막 날짜 가져오기

다음 예시는 “2023/01” 타입의 문자열 값을 전달 받았을 때 01월의 마지막 날짜를 yyyy-mm-dd 형식의 문자로 날짜를 리턴 받을 수 있다.

public static String getEndDateOfMonth(String sYearMonth){
Calendar cal = Calendar.getInstance();
String[] arr = sYearMonth.split(“/”);
cal.set( Integer.parseInt(arr[0]) ,Integer.parseInt(arr[1])-1 ,1);
cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
SimpleDateFormat sdf = new SimpleDateFormat(“yyyy-MM-dd”);
//Log.d(“TAG”, ” : ” + sdf.format(cal.getTime()));
return sdf.format(cal.getTime());
}

split()메소드를 이용하여 쪼갤 기준을 “/”로 잡아 배열에 담아요.

Leave a Reply

error: Content is protected !!