-1

Possible Duplicate:
How can I calculate a time span in Java and format the output?

Is there a simple function to format time range as a string? At the moment I can generate an output like that:

"81 days"

final Calendar today = Calendar.getInstance();
int today_day = today.get(Calendar.DAY_OF_YEAR);

final Calendar startCal = Calendar.getInstance();
startCal.add(Calendar.Day_OF_YEAR,81);  
int start_day = startCal.get(Calendar.DAY_OF_YEAR);

 int dif= start_day-today_day;
 if (dif >1){
   str = "in "+dif+" days";
 }

but I would prefere it to be

" 2 month and 20 days"

Community
  • 1
  • 1
Anthea
  • 3,741
  • 5
  • 40
  • 64

1 Answers1

-1

I recommend you to use Date Object:

Date startDay = new Date(); //today
Date targetDay = new Date();
targetDay.setDate(startDay.getDate() + 81);
Date difference = new Date(startDay.getTime() - targetDay.getTime());
// you may format as you want i.e difference.getMonth()
String differenceDateString = difference.toString(); 

Calendar's getTime method returns Date object too.

Mustafa Genç
  • 2,569
  • 19
  • 34