Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1013f93
commit 725a6e0
Showing
2 changed files
with
69 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
core-groovy/src/test/groovy/com/baeldung/date/DateTest.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package com.baeldung.groovy.sql | ||
|
||
import static org.junit.Assert.* | ||
import java.util.Calendar.* | ||
import java.time.LocalDate | ||
import java.text.SimpleDateFormat | ||
import org.junit.Test | ||
|
||
|
||
class DateTest { | ||
|
||
def dateStr = "2019-02-28" | ||
def pattern = "yyyy-MM-dd" | ||
|
||
@Test | ||
void whenGetStringRepresentation_thenCorrectlyConvertIntoDate() { | ||
def dateFormat = new SimpleDateFormat(pattern) | ||
def date = dateFormat.parse(dateStr) | ||
|
||
println(" String to Date with DateFormatter : " + date) | ||
|
||
def cal = new GregorianCalendar(); | ||
cal.setTime(date); | ||
|
||
assertEquals(cal.get(Calendar.YEAR),2019) | ||
assertEquals(cal.get(Calendar.DAY_OF_MONTH),28) | ||
assertEquals(cal.get(Calendar.MONTH),java.util.Calendar.FEBRUARY) | ||
} | ||
|
||
@Test | ||
void whenGetStringRepresentation_thenCorrectlyConvertWithDateUtilsExtension() { | ||
|
||
def date = Date.parse(pattern, dateStr) | ||
|
||
println(" String to Date with Date.parse : " + date) | ||
|
||
def cal = new GregorianCalendar(); | ||
cal.setTime(date); | ||
|
||
assertEquals(cal.get(Calendar.YEAR),2019) | ||
assertEquals(cal.get(Calendar.DAY_OF_MONTH),28) | ||
assertEquals(cal.get(Calendar.MONTH),java.util.Calendar.FEBRUARY) | ||
} | ||
|
||
@Test | ||
void whenGetStringRepresentation_thenCorrectlyConvertIntoDateWithLocalDate() { | ||
def date = LocalDate.parse(dateStr, pattern) | ||
|
||
println(" String to Date with LocalDate : " + date) | ||
|
||
assertEquals(date.getYear(),2019) | ||
assertEquals(date.getMonth(),java.time.Month.FEBRUARY) | ||
assertEquals(date.getDayOfMonth(),28) | ||
} | ||
|
||
|
||
} |