我写了以下代码
Date d = new Date();
CharSequence s = DateFormat.format("MMMM d, yyyy ", d.getTime());
我想要字符串格式的当前日期,比如
28-Dec-2011
以便我可以将其设置为 TextView
。
最佳答案
您可以使用 SimpleDateFormat
用于将日期格式化为所需格式的类。
只需查看此链接即可了解您的示例。
例如:
String dateStr = "04/05/2010";
SimpleDateFormat curFormater = new SimpleDateFormat("dd/MM/yyyy");
Date dateObj = curFormater.parse(dateStr);
SimpleDateFormat postFormater = new SimpleDateFormat("MMMM dd, yyyy");
String newDateStr = postFormater.format(dateObj);
更新:
详细的例子是here ,我建议你通过这个例子来理解 SimpleDateFormat 类的概念。
最终解决方案:
Date c = Calendar.getInstance().getTime();
System.out.println("Current time => " + c);
SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yyyy", Locale.getDefault());
String formattedDate = df.format(c);
关于android - 如何在 Android 中获取当前日期?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8654990/