안드로이드 시스템은 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시간 변경도 해보신다음 버튼을 눌러서 텍스트가 바뀌어서 나오는지도 한번 확인해보시면 되겠습니다.

 

+ Recent posts