4

I want to get the current date day/month/year min:sec in Android so I have used the follow code

String data="";
Calendar c = Calendar.getInstance(); 
data=c.getTime().toGMTString();

But Eclipse notify me that the method .toGMTString(); is deprecated.

How could I get the current date as formatted String avoiding the use of this deprecated method?

Ram kiran Pachigolla
  • 20,897
  • 15
  • 57
  • 78
AndreaF
  • 11,975
  • 27
  • 102
  • 168

2 Answers2

7

From the Android documentation:

This method is deprecated.
use DateFormat

Since the GMT string is represented as 22 Jun 1999 13:02:00 GMT, we can use SimpleDateFormat (subclass of the abstract DateFormat) like so:

SimpleDateFormat df = new SimpleDateFormat("dd MMM yyyy HH:mm:ss");
String asGmt = df.format(c.getTime()) + " GMT";

Might want to double-check that format, but this'll get you started. Have a look at this IDEOne code.

Cat
  • 66,919
  • 24
  • 133
  • 141
5

The method toGMTString() from the type Date is deprecated.

you can check this for different types of date formations.

In your case use

SimpleDateFormat dfDate  = new SimpleDateFormat("dd/MMM/yyyy HH:mm:ss");
String data="";
Calendar c = Calendar.getInstance(); 
data=dfDate.format(c.getTime());
System.out.println(data);//==========> 17/Oct/2012 08:36:52

If you want to print month number instead of month name

use

SimpleDateFormat dfDate  = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
String data="";
Calendar c = Calendar.getInstance(); 
data=dfDate.format(c.getTime());
System.out.println(data);//==========> 17/10/2012 08:36:52
Ram kiran Pachigolla
  • 20,897
  • 15
  • 57
  • 78