GregorianCalendar cal = new GregorianCalendar(1977,7,11); GregorianCalendar now = new GregorianCalendar(); int alder = now.get(Calendar.YEAR) - cal.get(Calendar.YEAR);
public class Alder { public static void main(String[] args) { System.out.println(alder(2003,Calendar.DECEMBER, 1)); System.out.println(alder(2003,Calendar.DECEMBER, 2)); System.out.println(alder(2003,Calendar.DECEMBER, 3)); } private static int alder(int y, int m, int d) { Calendar cal = new GregorianCalendar(1977,7,11); Calendar now = new GregorianCalendar(); return now.get(Calendar.YEAR) - cal.get(Calendar.YEAR); } }
public class Alder { public static void main(String[] args) { System.out.println(alder(1993,Calendar.DECEMBER, 1)); System.out.println(alder(1993,Calendar.DECEMBER, 2)); System.out.println(alder(1993,Calendar.DECEMBER, 3)); } private static int alder(int y, int m, int d) { Calendar cal = new GregorianCalendar(y, m, d); Calendar now = new GregorianCalendar(); return now.get(Calendar.YEAR) - cal.get(Calendar.YEAR); } }
public class Alder { public static void main(String[] args) { System.out.println(alder(1993,Calendar.DECEMBER, 2)); System.out.println(alder(1993,Calendar.DECEMBER, 3)); System.out.println(alder(1993,Calendar.DECEMBER, 4)); } private static int alder(int y, int m, int d) { Calendar cal = new GregorianCalendar(y, m, d); Calendar now = new GregorianCalendar(); int alder = now.get(Calendar.YEAR) - cal.get(Calendar.YEAR); if((cal.get(Calendar.MONTH) > now.get(Calendar.MONTH)) || (cal.get(Calendar.MONTH) == now.get(Calendar.MONTH) && cal.get(Calendar.DAY_OF_MONTH) > now.get(Calendar.DAY_OF_MONTH))) { alder--; } return alder; } }
men man er nødt til at korrigere efter dag/måned.
Synes godt om
Ny brugerNybegynder
Din løsning...
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.