Android:字符串中的日期格式 [英] Android : date format in a String
问题描述
我有一个问题,因为这些日期的格式排序日期。
我获取日期:
最终日历c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
我建立一个带有这些值的字符串。
$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ 。)
.append(mDay).append())。toString();
问题是,如果月份是例如2月,那么mMonth值是2.所以月份像十月(10)在我的名单之前。
我需要的是这个月份和日子是像MM和dd一样形成的。但是我不知道该怎么做。
编辑:
我使用如上所述的DateFormat解决了这个问题。
我替换了这个:
dateRappDB =(new StringBuilder()
.append(mYear).append(。)
.append(mMonth + 1)。 append(。)
.append(mDay).append())。toString();
此处:
Date date = new Date(mYear - 1900,mMonth,mDay);
dateFacDB = DateFormat.format(yyyy.MM.dd,date).toString();
它的工作原理。
感谢大家的帮助:)
这里是一个将Date转换为String的简单方法:
SimpleDateFormat simpleDate = new SimpleDateFormat(dd / MM / yyyy);
String strDt = simpleDate.format(dt);
I have a problem to sort date because of the format of these dates.
I obtain the date :
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
And I build a String with these values.
dateRappDB = (new StringBuilder()
.append(mYear).append(".")
.append(mMonth + 1).append(".")
.append(mDay).append(" ")).toString();
The problem is that if the month is for example February, mMonth value is 2. So dates with months like October (10) comes before in my list.
What I need is that month and day are formated like MM and dd. But I don't know how to do it in my case.
EDIT :
I solved the problem by using a DateFormat like said above.
I replaced this :
dateRappDB = (new StringBuilder()
.append(mYear).append(".")
.append(mMonth + 1).append(".")
.append(mDay).append(" ")).toString();
By this :
Date date = new Date(mYear - 1900, mMonth, mDay);
dateFacDB = DateFormat.format("yyyy.MM.dd", date).toString();
And it works. Thanks to all of you for your help :)
here is a simple way to convert Date to String :
SimpleDateFormat simpleDate = new SimpleDateFormat("dd/MM/yyyy");
String strDt = simpleDate.format(dt);
这篇关于Android:字符串中的日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!