본문 바로가기

Android

SimpleDateFormat 사용하기



@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

editText3 = (TextView) findViewById(R.id.editText3);

String strToday = getDateToday();
editText3.setText(strToday);

}

protected String getDateToday(){

//현재 시간을 가지는 Date객체를 생성한다
Date currentDate = new Date();

//시간을 특정한 문자열로 만들기 위한 포맷을 생성한다.  

SimpleDateFormat sdfYear = new SimpleDateFormat("yyyy");

SimpleDateFormat sdfMon = new SimpleDateFormat("MM");
SimpleDateFormat sdfDay = new SimpleDateFormat("dd");

return sdfYear.format(currentDate)+"년 "+sdfMon.format(currentDate)+"월 "+sdfDay.format(currentDate)+"일";
}