android - 如何在 Android 中获取当前日期?

标签 android date simpledateformat android-date

我写了以下代码

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/

相关文章:

java - 日期差异包括时区偏移,怎么了?

java - 在不获取 "Suspicious sign in prevented"的情况下发送电子邮件的替代方法

android - Xamarin.Android 检测模拟器

mysql - 日期格式 .NET 到 mysql

ruby-on-rails-3 - Rails3/jQuery UI Datepicker - 月份和日期在保存时反转

Java SimpleDateFormat 转换为错误的日期

android - 减小 TimePicker 大小

android - RecyclerView 中的 GridLayoutManager 3 列

java 解析异常 : Unparseable date

java - SimpleDateFormat 小时字符串在某些*日期*上相差一。如何?为什么?