HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

GregorianCalendar

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
gregoriancalendarstackoverflowprogramming

Problem

I have to create a class Data with dates and there should be some methods and constructor. Maybe some method can be done in another way, especially the method wichIsEarlier, because I made it static and I'm not really sure about it. Please take a look on this class and say what is good and what is bad. Is it better to use class Calendar than GregorianCalendar? Where should I throw an exception?

```
class Data {
GregorianCalendar gcalendar = new GregorianCalendar();

Data(GregorianCalendar x) {
gcalendar = x;
}

public boolean czyPrzestepny() {

boolean rr = false;
if (gcalendar.isLeapYear(gcalendar.get(Calendar.YEAR))) {
rr = true;
} else {
rr = false;
}
return rr;
}

public int ileDniWMiesiacu() {
int dni = 0;
switch (gcalendar.get(Calendar.MONTH)) {
case 0:
case 2:
case 4:
case 6:
case 7:
case 9:
case 11:
dni = 31;
break;
case 3:
case 5:
case 8:
case 10:
dni = 30;
break;
case 1:
dni = 28;
dni = 29;
break;
}
return dni;
}

static String wichIsEarlier(GregorianCalendar y, GregorianCalendar gcalendar) {
int yyear = y.get(Calendar.YEAR);
int ymonth = y.get(Calendar.MONTH);
int ydate = y.get(Calendar.DATE);
int year = gcalendar.get(Calendar.YEAR);
int month = gcalendar.get(Calendar.MONTH);
int day = gcalendar.get(Calendar.DATE);
String re = "";
if (yyear > year)
re = "moja data nie jest wczesniejsza od teraz";
else if (yyear month)
re = " nie jest wczesniejsze";
else if (ydate > day)
re = " nie jest wczesniejsze";
else if (ydate < day)
re = " jest wczesniejsze";
return re;
}

public void changeDat

Solution

As Marc-Andre mentioned, you should really use English for your methods and classes, so that we can review better.

Still, I am fairly certain that

public  boolean czyPrzestepny(){
    boolean rr = false;
    if(gcalendar.isLeapYear(gcalendar.get(Calendar.YEAR))){
        rr=true;
    }
    else{
       rr=false;
    }
    return rr;
}


could probably written as

public boolean isLeapYear()
{
  return gcalendar.isLeapYear(gcalendar.get(Calendar.YEAR))
}


The following code seems, wrong, you probably forgot to check for leap year?

case 1:
       dni = 28;dni= 29;


Also, wichIsEalier should be wichIsEarlier

Also, gcalendar does not follow proper lowerCamelCase, and Data is just too ambiguous as a class name.

Finally, for getting the month name in toString, you could just call gcalendar.get(Calendar.MONTH). and take the first 3 characters. Free I18N is good.

Code Snippets

public  boolean czyPrzestepny(){
    boolean rr = false;
    if(gcalendar.isLeapYear(gcalendar.get(Calendar.YEAR))){
        rr=true;
    }
    else{
       rr=false;
    }
    return rr;
}
public boolean isLeapYear()
{
  return gcalendar.isLeapYear(gcalendar.get(Calendar.YEAR))
}
case 1:
       dni = 28;dni= 29;

Context

StackExchange Code Review Q#29992, answer score: 5

Revisions (0)

No revisions yet.