Andy
Andy

Reputation: 27

Android: Calendar not returning values?

I'm trying to run this little code to return a eight-digit integer to be used in a for-loop later on as a search function. Problem is that it doesn't return any values for searchDateToday. Am I missing something?

final Calendar cal = Calendar.getInstance();
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
DateToday = formatter.format(cal.getTime()); // YYYYMMDD form -
                                             // Example: 20111010
                                             // = October 10,
                                             // 2011

int searchDateToday = Integer.parseInt(DateToday);

Upvotes: 0

Views: 187

Answers (2)

Carnal
Carnal

Reputation: 22064

You forgot to use the Date object:

final Calendar cal = Calendar.getInstance();
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
DateToday = formatter.format(new Date(cal.getTime())); 

// YYYYMMDD form -// Example: 20111010 // = October 10, // 2011

int searchDateToday = Integer.parseInt(DateToday);

Upvotes: 2

RajaReddy PolamReddy
RajaReddy PolamReddy

Reputation: 22493

use this for getting today date

String currentDate = DateFormat.getDateInstance().format(new Date());
tv_date.setText(currentDate);   // tv_date is TextView

Upvotes: 0

Related Questions