gpt4 book ai didi

java - 安卓 : date format in a String

转载 作者:IT老高 更新时间:2023-10-28 23:18:37 26 4
gpt4 key购买 nike

由于这些日期的格式,我无法对日期进行排序。

我得到日期:

final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);

我用这些值构建了一个字符串。

dateRappDB = (new StringBuilder()
.append(mYear).append(".")
.append(mMonth + 1).append(".")
.append(mDay).append(" ")).toString();

问题是,如果月份是例如二月,则 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();

而且它有效。感谢大家的帮助:)

最佳答案

这是一个将日期转换为字符串的简单方法:

SimpleDateFormat simpleDate = new SimpleDateFormat("dd/MM/yyyy");

String strDt = simpleDate.format(dt);

关于java - 安卓 : date format in a String,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7281633/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com