Programing

캘린더와 함께 SimpleDateFormat을 어떻게 활용할 수 있습니까?

lottogame 2020. 11. 11. 07:50
반응형

캘린더와 함께 SimpleDateFormat을 어떻게 활용할 수 있습니까?


GregorianCalendar 인스턴스가 있고 필요한 출력을 얻으려면 SimpleDateFormat (또는 캘린더와 함께 사용할 수 있지만 필요한 #fromat () 기능을 제공하는 것)을 사용해야합니다. 영구적 인 해결책만큼 좋은 해결 방법을 제안하십시오.


이 시도:

Calendar cal = new GregorianCalendar();
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
dateFormat.setTimeZone(cal.getTimeZone());
System.out.println(dateFormat.format(cal.getTime()));

eQui의 대답에 단계가 없습니다.

Calendar cal = new GregorianCalendar();
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
#---- This uses the provided calendar for the output -----
dateFormat.setCalendar(cal); 
System.out.println(dateFormat.format(cal.getTime()));

Calendar.getTime ()은 SimpleDateFormat과 함께 사용할 수있는 날짜를 반환합니다.


calendar.getTime()결과 Date개체를 호출 하고 format메서드에 전달하기 만하면 됩니다.

참고 URL : https://stackoverflow.com/questions/5621825/how-can-i-utilize-simpledateformat-with-calendar

반응형