开发者

Android: Unique ID generator and Locale

I have a function as below to开发者_开发百科 generate unique ID for my application

public static String now(String dateFormat) {
    Calendar cal = Calendar.getInstance();
    SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
    return sdf.format(cal.getTime());
}

I call the function using following way

incident_id = IncidentIdGenerator.now("ddMMyyyyHHmmss");

problem occures when I change the Locale. For an example if I change the locale to Arab, unique ID is generated in some arab letters. Arab letters are not supported in the web service which I'm gonna call.

I must stick to Locale.US when creating unique ID. How can I acheive this?? I tried as below but it didn't work too.

Calendar cal = Calendar.getInstance(Locale.US);

Thanks for yoru time in advance.


Unless you are hellbent on using time and locale for generating unique ID in your application Here is an alternative

String uniqueId = UUID.randomUUID().toString();

Generate it and store this is a sharedPreference and use it whenever you want.


You also have to specify Locale.US with your SimpleDateFormat.

SimpleDateFormat sdf = new SimpleDateFormat(dateFormat, Locale.US);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜