Skip to content

Instantly share code, notes, and snippets.

@esfand
Forked from codeslubber/DateBuilder.java
Created May 21, 2013 16:22
Show Gist options
  • Save esfand/5621129 to your computer and use it in GitHub Desktop.
Save esfand/5621129 to your computer and use it in GitHub Desktop.
package com.ontometrics.util;
import java.util.Calendar;
import java.util.Date;
/**
* Provides a fluent builder interface for constructing Dates and DateTimes.
*/
public class DateBuilder {
private Calendar calendar = Calendar.getInstance();
/**
* Provides means of starting from a given date, then changing some subset
* of the fields
*
* @param date
* the starting point date
* @return this, for chaining
*/
public DateBuilder start(Date date) {
calendar.setTime(date);
return this;
}
/**
* So to build the first of the year, you would set this to 1.
*
* @param dayOfMonth
* a number between 1 and 31 (depending on the calendar month of
* course)
* @return this, for chaining
*/
public DateBuilder day(int dayOfMonth) {
calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
return this;
}
/**
* Provide the month of the date you are building, a number between 1 and
* 12.
*
* @param month
* the value desired in the resulting date, e.g. 5 for May
* @return this, for chaining
*/
public DateBuilder month(int month) {
calendar.set(Calendar.MONTH, month);
return this;
}
/**
* Provide the year of the date you are building, e.g. 1980.
*
* @param year
* the value desired in the resulting date
* @return this, for chaining
*/
public DateBuilder year(int year) {
calendar.set(Calendar.YEAR, year);
return this;
}
/**
* Provide the hour, in this case of the day. Note the JDK calls this hour
* of the day.
*
* @param hour
* the value desired in the resulting date, e.g. 14 for 2 pm
* @return this, for chaining
*/
public DateBuilder hour(int hour) {
calendar.set(Calendar.HOUR_OF_DAY, hour);
return this;
}
/**
* Provide the minutes within the hour of the datetime being constructed
*
* @param minutes
* a number between 0 and 59, e.g. if building 12:49, this would
* be 49.
* @return this, for chaining
*/
public DateBuilder minutes(int minutes) {
calendar.set(Calendar.MINUTE, minutes);
return this;
}
/**
* Provides access to the final product.
*
* @return the constructed date with all the desired values
*/
public Date build() {
return calendar.getTime();
}
}
package com.ontometrics.util;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
import java.util.Calendar;
import java.util.Date;
import org.junit.Test;
public class DateBuilderTest {
@Test
public void testCanBuildSimpleDate() {
Date newDate = new DateBuilder().day(1).month(1).year(2013).build();
Calendar calendar = Calendar.getInstance();
calendar.setTime(newDate);
assertThat(calendar.get(Calendar.DAY_OF_MONTH), is(1));
}
@Test
public void testCanBuildDateTime(){
Date newDate = new DateBuilder().day(1).month(1).year(2013).hour(12).minutes(49).build();
Calendar calendar = Calendar.getInstance();
calendar.setTime(newDate);
assertThat(calendar.get(Calendar.HOUR_OF_DAY), is(12));
assertThat(calendar.get(Calendar.MINUTE), is(49));
}
@Test
public void testCanBuildFromToday(){
Date today = new Date();
Calendar calendar = Calendar.getInstance();
calendar.setTime(today);
int todayYear = calendar.get(Calendar.YEAR);
int todayMonth = calendar.get(Calendar.MONTH);
int todayDay = calendar.get(Calendar.DAY_OF_MONTH);
Date newDate = new DateBuilder().start(today).hour(12).minutes(49).build();
calendar.setTime(newDate);
assertThat(calendar.get(Calendar.YEAR), is(todayYear));
assertThat(calendar.get(Calendar.MONTH), is(todayMonth));
assertThat(calendar.get(Calendar.DAY_OF_MONTH), is(todayDay));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment