0

i want to use standard class called DateFormat which has subclass SimpleDateFormat TO write a method called convert which returns a String in the form dd.mm.yy: when passed a GregorianCalendar with a specific date

public String convert (Calendar gc) { ... } 

For example, when myGC is a GregorianCalendar variable representing the 25th of December 2006, String s = convert(myGC); should set s to the string "25.12.06".

and i'm having trouble to write a convert method on this

Kirk Woll
  • 73,473
  • 21
  • 178
  • 189
user761497
  • 67
  • 5
  • 12

2 Answers2

0

Why not just use a pattern like this one "dd.MM.yy" in your SimpleDateFormat ?

DateFormat dateFormatter = new SimpleDateFormat("dd.MM.yy");
String myDate = dateFormatter.format(cal.getTime());
dplante
  • 2,417
  • 3
  • 21
  • 27
planetjones
  • 12,109
  • 4
  • 48
  • 51
0
public String convert(Calendar c) {
  return new SimpleDateFormat("dd.MM.yy").format(c.getTime());
}

Eventually you'll want to store that SimpleDateFormat as a member (for example) if performance becomes a concern.

maerics
  • 143,080
  • 41
  • 260
  • 285