안드로이드 시스템은 date format과 time을 제공하고있는데요.

 

문제는 국가별로 date format이 조금씩 달라서 글로벌 서비스를 하는경우에는 조금은 고민을 해봐야합니다.

 

어떻게 적절하게 국가별로 date와 time을 보여줄지요.

 

예로들면 우리나라는 2월24일을 2.24 이런format으로 표현한다면

 

독일은 24.02. 프랑스는 24/02 등 조금씩 선호하는 방식이 다른편입니다.

 

국가별로 분기해서 작성하자니 만만지않을것같아서 찾아보니 웬걸? 안드로이드에서는 편이상 이런 기능들까지 다 제공해주고있습니다.

 

 

위의 그림은 epoch time으로 한국, 미국, 독일, 프랑스의 date format을 나타내본것이고 

time format도 출력해보았습니다. time format의 경우 현재 사용자가 12시간 format을 사용하고있는지 24시간 format을 사용하고있는지에 따라 출력되게끔 구현하였습니다.

 

 

 

MainActivity.java

main에서만 다 구성하였습니다.

 

getBestDateTimePattern - 현지 Locale에 맞는 date를 출력해줍니다. 기호에 따라 시간과 분도 format에 출력할 수 있습니다. 여기서 좋고 편한것은, MM과 DD의 위치를 신경쓰실 필요가없습니다.

Locale에 맞도록 적절하게 순서를 다시 바꿔서 return해줍니다.

 

getProperTimeAsClockPreference - 현재의 context 정보에 맞게끔 time format을 return하게 됩니다.

12시간, 24시간제 인지에 따라 변환해주고 또한, 국가에 따라 조금씩 다른 format을 주기까지합니다.

    public String getBestDateTimePattern (Locale locale) {
        //return  DateFormat.getBestDateTimePattern(locale, "MM dd hh:mm");
        return  DateFormat.getBestDateTimePattern(locale, "MM dd");
    }
    /*
    12 / 24 For distinguish
    static DateFormat	getTimeFormat(Context context)
    Returns a DateFormat object that can format the time according to the context's locale and the user's 12-/24-hour clock preference.
    */
    public java.text.DateFormat getProperTimeAsClockPreference(Context context) {
        return  DateFormat.getTimeFormat(context);
    }

 

 

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

        Button button = findViewById(R.id.button);
        final TextView tv = findViewById(R.id.textView);

        button.setOnClickListener(new View.OnClickListener () {
            @Override
            public void onClick(View view) {

                SimpleDateFormat dateFormat = null;
                long time = System.currentTimeMillis();

                tv.append("\n time:::" + time);

                String bestDateFormat = getBestDateTimePattern(Locale.KOREA);
                dateFormat = new SimpleDateFormat(bestDateFormat);
                String convertedKoreaDate = dateFormat.format(new Date(time));

                tv.append("\n" + convertedKoreaDate);

                bestDateFormat = getBestDateTimePattern(Locale.US);
                dateFormat = new SimpleDateFormat(bestDateFormat);
                String convertedDate = dateFormat.format(new Date(time));

                tv.append("\n" + convertedDate);


                bestDateFormat = getBestDateTimePattern(Locale.GERMAN);
                dateFormat = new SimpleDateFormat(bestDateFormat);
                convertedDate = dateFormat.format(new Date(time));

                tv.append("\n" + convertedDate);

                bestDateFormat = getBestDateTimePattern(Locale.FRANCE);
                dateFormat = new SimpleDateFormat(bestDateFormat);
                convertedDate = dateFormat.format(new Date(time));

                tv.append("\n" + convertedDate);


                long stampAsCal;
                java.text.DateFormat formatDateTime;
                formatDateTime = getProperTimeAsClockPreference(getApplicationContext());
                String _time = formatDateTime.format(time);

                tv.append("\n converted time:" + _time);
                //kr 24 case: 1:11
                //kr 12 case: 오전 1:11

                //ge 24 case: 1:11
                //ge 12 case: 1:11 vorm

                // remove space
                String convertedConvertedKoreaDate = convertedKoreaDate.replace(" ", "");

                // remove . end of the date
                if (convertedConvertedKoreaDate.length() > 0 && convertedConvertedKoreaDate.charAt(convertedConvertedKoreaDate.length()-1) == '.') {
                    convertedConvertedKoreaDate = convertedConvertedKoreaDate.substring(0, convertedConvertedKoreaDate.length()-1);
                    Log.d("jinss", "gotta");
                }

                convertedConvertedKoreaDate = convertedConvertedKoreaDate + " " + _time;

                tv.append("\n final converted time:" + convertedConvertedKoreaDate);
            }
        });


    }

한국, 미국, 독일, 프랑스에 대해서 시험적으로 테스트 해보았습니다.

또한 최종 date와 time까지 출력도 해보았습니다.

 

앱을 실행해놓고, 12시간 24시간 변경도 해보신다음 버튼을 눌러서 텍스트가 바뀌어서 나오는지도 한번 확인해보시면 되겠습니다.

 

Executor 프레임워크를 사용하면 다음과 같은 일들을 처리할 수있습니다.



 - 스레드에서 실행되기를 기다리는 task 수를 제어하는 작업자 스레드 pool과 큐를 설정

- 비정상적으로 종료되는 스레드를 초래하는 에러 확인

- 완료되는 스레드를 기다리고 스레드로부터 결과를 가져온다.

- 스레드의 일괄처리를 실행하고, 고정된 순서로 결과를 가져온다.

- 사용자가 더 빨리 결과를 확인할 수 있도록 알맞은 시간에 백그라운드 스레드를 시작한다.




Excutor 프레임워크의 기본 구셩요소는 Executor 인터페이스다.


이것의 주요목표는 태스크의 생성을 분리하여(Runnnalbe) 앞에서 나열한 응용프로그램 동작들을 가능하게 하는 것이다.



이 인터페이스는 단하나의 메서드를 포함한다.

 public interface Executor{

void execute(Runnalbe command);
}




Executor 로 제어할 수 있는 동작으로는 다음과 같다.


- 태스크 큐잉

- 태스크 실행순서

- 태스크 실행 유형(직렬 또는 동시)




Executor 예제

AsyncTask에서 사용된 직렬 Task 실행자를 구현. SerialExecutor는 생산자-소비자 패턴으로 구현


Thread는 Runnable Task를 생성하고, 큐에 Task를 배치하며, 소비자 스레드는 큐에서 Task 를 제거하고 처리한다.



private static class SerialExecutor implemtents Executor{
final ArrayDeque mTasks = new ArrayDeque();
Runnable mActive;

public synchronized void execute(final Runnable r){

mTasks.offer(new Runnable(){
public void run{
try{
r.run();
}
finally{
scheduleNext();
}
}
});
if( mActive == null){
scheduleNext();
}
protected sysnchronized void scheduleNext(){
if((mActive=mTasks.poll())!= null){
THREAD_POOL_EXECUTOR.excute(mActive);
}
}

}
}





실행자 실행동작특징은 다음과 같다.


- 태스크 큐잉

ArrayDeque - 데큐, 양방향으로 꺼낼 수 있는 큐.

             스레드에 의해 처리될떄까지 삽입된 Task들을 보유한다.


- 태스크 실행순서

모든 태스크는 mTask.offer()를 통해 데큐의 끝에 넣어진다. 따라서

결과는 삽입된 태스크들 fifo 순서다.



- 태스크 실행 유형

Task는 직렬로 실행되지만, 같은 스레드에서 실행될 필요없다.

Task가 실행완료할떄 마다 r.run()이 완료할떄마다, scheduleNext()가 호출된다.

이것은 큐에서 다음 Task를 가져와서 스레드가 Task그를 실행할 수 있는 스레드 풀안의 다른 Executor로 보낸다.



요컨대 SerialExecutor는 각각 다른 스레드에서 Task를 처리할 수 있으며 Task들의 순차적인 실행을 보장하는 실행환경을 

구성한다.


<순차실행이지, 동시실행은 아님>




+ Recent posts