Skip to content

Commit

Permalink
Calendar updated
Browse files Browse the repository at this point in the history
  • Loading branch information
Umit-Soylu committed Sep 15, 2020
1 parent 724dabc commit dc361ca
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 17 deletions.
35 changes: 27 additions & 8 deletions src/com/bilgeadam/java/tutorials/date_time/Calendars.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Locale;

public class Calendars {
private static final String calendarFormatString = "yyyy/MMMM/dd hh:mm:ss:SSSS";
Expand All @@ -14,25 +15,43 @@ public Calendars() {
simpleDateFormat = new SimpleDateFormat();
simpleDateFormat.applyPattern(calendarFormatString);


// Initialize calendar with current time
this.calendar = new GregorianCalendar();
}

public Calendar getCalendar() {

return this.calendar;
/*
return calendar.getDisplayName((Calendar.MONTH, Calendar.DAY_OF_MONTH, Calendar.YEAR),
/**
* This fucntion uses getDispalyName method with Locale
* @param locale requested
* @return String of the Month
*/
public String getCalendarMonth(Locale locale) {
return calendar.getDisplayName(Calendar.MONTH,
Calendar.LONG,
Locale.ENGLISH);*/
locale);
}

/**
* This method only returns integer representation of the year
* @return Current Year
*/
public int getCalendarYear() {
return calendar.get(Calendar.YEAR);

}

/**
*
* @return True if leap year, false otherwise
*/
public boolean isLeapYear(){
return calendar.isLeapYear(calendar.get(Calendar.YEAR));
}

/**
* Calendar also includes Date object which can be retrieved and parsed as needed.
* @return Formatted Date object
*/
public String getCalendarString() {

return this.simpleDateFormat.format(this.calendar.getTime());
}
}
22 changes: 13 additions & 9 deletions test/com/bilgeadam/java/tutorials/date_time/CalendarsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import org.junit.jupiter.api.Test;

import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Locale;

import static org.junit.jupiter.api.Assertions.assertEquals;

Expand All @@ -23,19 +25,21 @@ void tearDown() {

@Test
void getCalendar() {
System.out.println("testCalendars.getCalendar() = " + testCalendars.getCalendar().get(Calendar.YEAR));
System.out.println("testCalendars.getCalendar() = " + testCalendars.getCalendar().get(Calendar.MONTH));
System.out.println("testCalendars.getCalendar() = " + testCalendars.getCalendar().get(Calendar.DAY_OF_MONTH));
System.out.println("testCalendars.getCalendar() = " + testCalendars.getCalendar().isLenient());
// Locale object can be set with language and country separately.
Locale locale = new Locale("tr", "TR");

// Get the current month for given locale
String testMonth = new GregorianCalendar().getDisplayName(Calendar.MONTH, Calendar.LONG, locale);

assertEquals(testMonth, testCalendars.getCalendarMonth(locale));
}

@Test
void testLeapYear() {
assertEquals(true, testCalendars.isLeapYear());
}
// Get current year
int year = testCalendars.getCalendarYear();

@Test
void getCalendarString() {
System.out.println("testCalendars.getCalendarString() = " + testCalendars.getCalendarString());
// If year can be divided by 4, it is a leap year
assertEquals(year % 4 == 0, testCalendars.isLeapYear());
}
}

0 comments on commit dc361ca

Please sign in to comment.