Skip to content

Commit

Permalink
googleapis#3157 add methods toDate, fromDate
Browse files Browse the repository at this point in the history
  • Loading branch information
praful committed Nov 12, 2018
1 parent a7bd8a5 commit ebbeac0
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.threeten.bp.LocalDate;

/**
* Represents a Date without time, such as 2017-03-17. Date is timezone independent.
*/
Expand Down Expand Up @@ -68,7 +70,39 @@ public static Date parseDate(String date) {
int dayOfMonth = Integer.parseInt(matcher.group(3));
return new Date(year, month, dayOfMonth);
}

/**
* @param date
* @return LocalDate
*/
public static LocalDate toDate(Date date){
return LocalDate.of(date.year, date.month, date.dayOfMonth);
}

/**
* @param date
* @return Date
*/
public static Date toDate(LocalDate date){
return new Date(date.getDayOfYear(), date.getMonthValue(), date.getDayOfMonth());
}

/**
* @param date
* @return LocalDate
*/
public static LocalDate fromDate(Date date){
return LocalDate.of(date.year, date.month, date.dayOfMonth);
}

/**
* @param date
* @return Date
*/
public static Date fromDate(LocalDate date){
return new Date(date.getDayOfYear(), date.getMonthValue(), date.getDayOfMonth());
}

/** Returns the year. */
public int getYear() {
return year;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.threeten.bp.LocalDate;

/** Unit tests for {@link Date}. */
@RunWith(JUnit4.class)
Expand Down Expand Up @@ -70,6 +71,40 @@ public void validOrdering() {
public void serialization() {
reserializeAndAssert(Date.fromYearMonthDay(2017, 4, 20));
}

@Test
public void toLocalDate(){
LocalDate localDate = Date.toDate(Date.parseDate("2016-09-18"));
assertThat(localDate.getYear()).isEqualTo(2016);
assertThat(localDate.getMonth().getValue()).isEqualTo(9);
assertThat(localDate.getDayOfMonth()).isEqualTo(18);
}

@Test
public void toDate(){
Date date = Date.toDate(LocalDate.of(2016, 9, 18));
assertThat(date.getYear()).isEqualTo(2016);
assertThat(date.getMonth()).isEqualTo(9);
assertThat(date.getDayOfMonth()).isEqualTo(18);

}

@Test
public void fromLocalDate(){
LocalDate localDate = Date.fromDate(Date.parseDate("2016-09-18"));
assertThat(localDate.getYear()).isEqualTo(2016);
assertThat(localDate.getMonth().getValue()).isEqualTo(9);
assertThat(localDate.getDayOfMonth()).isEqualTo(18);
}

@Test
public void fromDate(){
Date date = Date.fromDate(LocalDate.of(2016, 9, 18));
assertThat(date.getYear()).isEqualTo(2016);
assertThat(date.getMonth()).isEqualTo(9);
assertThat(date.getDayOfMonth()).isEqualTo(18);
}


private void assertDescending(Date... dates) {
for (int i = 0; i < dates.length - 1; i++) {
Expand Down

0 comments on commit ebbeac0

Please sign in to comment.