Skip to content

Instantly share code, notes, and snippets.

@Cellane
Created March 8, 2011 12:52
Show Gist options
  • Save Cellane/860239 to your computer and use it in GitHub Desktop.
Save Cellane/860239 to your computer and use it in GitHub Desktop.
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class Cviceni3 {
Calendar birth = Calendar.getInstance (); // contains date of birth
Calendar today = Calendar.getInstance (); // contains today's date
Calendar thisYear = Calendar.getInstance (); // contains date of birthday on this year
Date date;
public Cviceni3 (Calendar calendar) {
setDate (new Date (calendar.getTimeInMillis ()));
initializeCalendars ();
}
public Cviceni3 (Date date) {
setDate (date);
initializeCalendars ();
}
public Cviceni3 (String string) {
DateFormat format = new SimpleDateFormat ("d. M. yyyy");
try {
setDate (format.parse (string));
initializeCalendars ();
} catch (ParseException e) {
System.out.println ("Spatny format data!");
}
}
private void initializeCalendars () {
birth.setTime (date);
thisYear.setTime (date);
thisYear.set (Calendar.YEAR, today.get (Calendar.YEAR));
}
private void setDate (Date date) {
this.date = date;
}
public int getWeekDay () {
return (birth.get (Calendar.DAY_OF_WEEK));
}
public int getAge () {
int year1, year2, age;
int month1, month2;
int day1, day2;
year1 = today.get (Calendar.YEAR);
year2 = birth.get (Calendar.YEAR);
age = year1 - year2;
month1 = today.get (Calendar.MONTH);
month2 = birth.get (Calendar.MONTH);
if (month2 > month1) {
age--;
} else if (month1 == month2) {
day1 = today.get (Calendar.DAY_OF_MONTH);
day2 = birth.get (Calendar.DAY_OF_MONTH);
if (day2 > day1) {
age--;
}
}
return (age);
}
public int getThisYearWeekDay () {
return (thisYear.get (Calendar.DAY_OF_WEEK));
}
}
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.util.Calendar;
import java.util.Date;
import static junit.framework.Assert.assertEquals;
public class Cviceni3Test {
private Calendar calendar = Calendar.getInstance ();
private Cviceni3 instance1, instance2, instance3;
private Date date = new Date (615636000000l);
private String string = "5. 7. 1989";
@Before
public void setUp () throws Exception {
calendar.set (Calendar.YEAR, 1989);
calendar.set (Calendar.MONTH, Calendar.JULY);
calendar.set (Calendar.DAY_OF_MONTH, 5);
instance1 = new Cviceni3 (calendar);
instance2 = new Cviceni3 (date);
instance3 = new Cviceni3 (string);
}
@After
public void tearDown () throws Exception {
instance1 = null;
instance2 = null;
instance3 = null;
}
@Test
public void testConstructors () throws Exception {
assertEquals (4, instance1.getWeekDay ());
assertEquals (4, instance2.getWeekDay ());
assertEquals (4, instance3.getWeekDay ());
}
@Test
public void testGetWeekDay () throws Exception {
assertEquals (4, instance3.getWeekDay ());
}
@Test
public void testAge () throws Exception {
assertEquals (21, instance3.getAge ());
}
@Test
public void testGetThisYearWeekDay () throws Exception {
assertEquals (3, instance3.getThisYearWeekDay ());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment