Skip to content

Commit c748679

Browse files
author
Daniel Reuter
committed
JSR-310 Parsing-Formatting
1 parent 05df5bb commit c748679

File tree

1 file changed

+19
-8
lines changed

1 file changed

+19
-8
lines changed

src/test/java/de/codecentric/java8examples/timeapi/TimeApiTest.java

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,22 @@
1010
import java.time.ZoneId;
1111
import java.time.ZonedDateTime;
1212
import java.time.chrono.ThaiBuddhistDate;
13+
import java.time.format.DateTimeFormatter;
1314
import java.time.temporal.ChronoField;
1415

1516
import org.joda.time.DateTime;
1617
import org.joda.time.DateTimeFieldType;
1718
import org.joda.time.DateTimeZone;
1819
import org.joda.time.Interval;
1920
import org.joda.time.chrono.BuddhistChronology;
21+
import org.joda.time.format.DateTimeFormat;
2022
import org.junit.Test;
2123

2224
/**
2325
* <a href="http://blog.joda.org/2009/11/why-jsr-310-isn-joda-time_4941.html">Why JSR-310 isn't Joda-Time</a><br/>
2426
* <a href="http://jug.ua/wp-content/uploads/2013/02/JODAjsr310.pdf">nice presentation (JUG Ukraine)</a><br/>
2527
* <a href="http://openjdk.java.net/projects/threeten/">project page</a><br/>
26-
* <a href="http://greannetwork.wordpress.com/2012/12/11/jsr-310-date-and-time-api-example/">examples 1</a><br/>
27-
* <a href="https://today.java.net/pub/a/today/2008/09/18/jsr-310-new-java-date-time-api.html">examples 2</a><br/>
28-
* <a href="http://download.java.net/jdk8/docs/api/java/time/package-summary.html">Java-Doc/a><br/>
28+
* <a href= "http://download.java.net/jdk8/docs/api/java/time/package-summary.html" >Java-Doc</a><br/>
2929
*
3030
* <br>
3131
* <h3>Joda</h3>
@@ -87,7 +87,7 @@ public void timeBetweenToDates() {
8787
org.joda.time.Period betweenJoda = new org.joda.time.Period(birthdayJoda, org.joda.time.LocalDate.now());
8888
System.out.println(betweenJoda.getYears() + " Years " + betweenJoda.getMonths() + " Months " + betweenJoda.getDays() + " Days.");
8989
assertEquals(32, betweenJoda.getYears());
90-
assertEquals(2, betweenJoda.getMonths());
90+
assertEquals(2, betweenJoda.getMonths());
9191
}
9292

9393
@Test
@@ -154,7 +154,7 @@ public void timezoneAndOffset() {
154154

155155
assertEquals(60 * 60 * 1, zonedDateTimeEurope.getOffset().getTotalSeconds());
156156
assertEquals("Europe/Berlin", zonedDateTimeEurope.getZone().getId());
157-
assertFalse(zonedDateTimeEurope.isBefore(zonedDateTimeUTC));
157+
assertFalse(zonedDateTimeEurope.isBefore(zonedDateTimeUTC));
158158
assertFalse(zonedDateTimeEurope.isAfter(zonedDateTimeUTC));
159159
assertTrue(zonedDateTimeEurope.isEqual(zonedDateTimeUTC));
160160

@@ -169,8 +169,19 @@ public void timezoneAndOffset() {
169169
assertTrue(dateTimeEuropeJoda.isEqual(dateTimeUTCJoda));
170170
}
171171

172-
// TODO Formatting & Parsing
173-
// TODO Conversion Hibernate? SQL - zur��ck zu util.date
174-
// TODO Null-Handling und beschriebene Unterschiede
172+
@Test
173+
public void parsingAndFormatting() {
174+
// JSR-310
175+
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("dd.MM.yyyy");
176+
LocalDate parsedDate = LocalDate.parse("03.09.1981", fmt);
177+
assertEquals(LocalDate.of(1981, 9, 3), parsedDate);
178+
assertEquals("03.09.1981", fmt.format(LocalDate.of(1981, 9, 3)));
179+
180+
// Joda
181+
org.joda.time.format.DateTimeFormatter fmtJoda = DateTimeFormat.forPattern("dd.MM.yyyy");
182+
org.joda.time.LocalDate parsedDateJoda = org.joda.time.LocalDate.parse("03.09.1981", fmtJoda);
183+
assertEquals(new org.joda.time.LocalDate(1981, 9, 3), parsedDateJoda);
184+
assertEquals("03.09.1981", fmtJoda.print(new org.joda.time.LocalDate(1981, 9, 3)));
185+
}
175186

176187
}

0 commit comments

Comments
 (0)