且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2022-10-23 17:26:30

你可以使用 SimpleDateFormat



只需查看此链接,您就可以获得想法的样例。



例如:

  String dateStr =04/05/2010; 

SimpleDateFormat curFormater = new SimpleDateFormat(dd / MM / yyyy);
日期dateObj = curFormater.parse(dateStr);
SimpleDateFormat postFormater = new SimpleDateFormat(MMMM dd,yyyy);

String newDateStr = postFormater.format(dateObj);



更新:



详细示例是 here 和这里,我建议您通过这个例子,了解SimpleDateFormat类的概念。



最终解决方案:



 日历c = Calendar.getInstance() ; 
System.out.println(当前时间=>+ c.getTime());

SimpleDateFormat df = new SimpleDateFormat(dd-MMM-yyyy);
String formattedDate = df.format(c.getTime());


I wrote the following code

Date d = new Date();
CharSequence s  = DateFormat.format("MMMM d, yyyy ", d.getTime());

But is asking me parameter, I want current date in string format,

like

28-Dec-2011

so that I can set over TextView,

explain a bit, if you think something is necessary, I am new to Android Development.

You can use the SimpleDateFormat class for formatting date in your desired format.

Just check this link where you get idea for your example.

For example:

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); 

Update:

Detailed example is here and here, I would suggest you to go through this example and understand the concept of SimpleDateFormat class.

Final Solution:

Calendar c = Calendar.getInstance();
System.out.println("Current time => " + c.getTime());

SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yyyy");
String formattedDate = df.format(c.getTime());