How to obtain date separators in Android?
First of 开发者_StackOverflow中文版all, thanks to everybody here that I can get answers to almost all my questions after 39 days into Java/Android/Eclipse development life. But now I seem to get stuck at a very unexpected problem!
I need to format dates into only year+month and month+day formats. These are not standard formats, so I need to construct my own format patterns and use DateFormat.format() to get the string. I know I can use DateFormat.getDateFormatOrder() to determine the ordering of year, month, day. But I also need to know the date separator, like "/", "-", or ".". This is where I get stuck, I can't find a way to do it!
From Eclipse debugger, I can get to see that there is a "pattern" field inside a DateFormat object, but it's not exposed. And the name of the method SimpleDateFormat.getDateFormatSymbols() sounds like what I want, but it's not. So....
Please help, thank you.
I think I found something for you:
public static String getDateSeparator(){
String dateString = DateFormat.getInstance().format(new java.util.Date());
Matcher matcher = Pattern.compile("[^\\w]").matcher(dateString);
if (!matcher.find()) return null;
return matcher.group(0);
}
use this code to get the separator and then crate your own date format using the simpledateformatter: http://developer.android.com/reference/java/text/SimpleDateFormat.html
SimpleDateFormat
has toPattern()
method.
Do you really need to know the exact separator? I would parse until I find a non-number (or non-alphanumeric if you're using month names).
精彩评论