Created
October 17, 2012 14:36
-
-
Save miere/3905854 to your computer and use it in GitHub Desktop.
Java Date API benchmark
This file contains hidden or 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
| import static com.unimondes.dsl.DateDsl.*; | |
| import static com.unimondes.dsl.DateDsl.Months.*; | |
| import static org.junit.Assert.assertEquals; | |
| import java.util.Calendar; | |
| import java.util.Date; | |
| import java.util.GregorianCalendar; | |
| import org.joda.time.DateTime; | |
| import org.junit.Test; | |
| public class Benchmark { | |
| public static void benchmarkNow() { | |
| Date now; | |
| // DateDSL | |
| now = now().toDate(); | |
| assertIsNow(now); | |
| // java.util.Date | |
| now = new Date(); | |
| assertIsNow(now); | |
| // java.util.Calendar | |
| now = Calendar.getInstance().getTime(); | |
| assertIsNow(now); | |
| // joda-time | |
| now = new DateTime().toDate(); | |
| assertIsNow(now); | |
| } | |
| @Test | |
| public void benchmarkClearTime() { | |
| Date noTime; | |
| // DateDSL | |
| noTime = now().clearTime().toDate(); | |
| assertNoTime(noTime); | |
| // java.util.Date | |
| // noWay noTime = new Date(); | |
| // java.util.Calendar | |
| Calendar cal = Calendar.getInstance(); | |
| cal.set(Calendar.HOUR_OF_DAY, 0); | |
| cal.set(Calendar.MINUTE, 0); | |
| cal.set(Calendar.SECOND, 0); | |
| cal.set(Calendar.MILLISECOND, 0); | |
| noTime = cal.getTime(); | |
| assertNoTime(noTime); | |
| // joda-time | |
| noTime = new DateTime().toDateMidnight().toDate(); | |
| assertNoTime(noTime); | |
| } | |
| @SuppressWarnings("deprecation") | |
| @Test | |
| public void benchmarkGivenDate() { | |
| Date givenDate; | |
| // DateDSL | |
| givenDate = date(2003, JANUARY, 10, 12, 0, 0, 0).toDate(); | |
| assert10Jan2003(givenDate); | |
| // java.util.Date | |
| givenDate = new Date(103, 0, 10, 12, 0); | |
| assert10Jan2003(givenDate); | |
| // java.util.Calendar | |
| Calendar cal = Calendar.getInstance(); | |
| cal.set(Calendar.YEAR, 2003); | |
| cal.set(Calendar.MONTH, Calendar.JANUARY); | |
| cal.set(Calendar.DAY_OF_MONTH, 10); | |
| cal.set(Calendar.HOUR_OF_DAY, 12); | |
| cal.set(Calendar.MINUTE, 0); | |
| cal.set(Calendar.SECOND, 0); | |
| cal.set(Calendar.MILLISECOND, 0); | |
| givenDate = cal.getTime(); | |
| assert10Jan2003(givenDate); | |
| // joda-time | |
| givenDate = new DateTime(2003, 1, 10, 12, 0, 0, 0).toDate(); | |
| assert10Jan2003(givenDate); | |
| } | |
| @Test | |
| public void benchmarkMath() { | |
| Date givenDate; | |
| // DateDSL | |
| givenDate = date().add(minutes(10)).toDate(); | |
| assert10minutesInFuture(givenDate); | |
| // java.util.Date | |
| // no way = new Date(103, 0, 10, 12, 0); | |
| // java.util.Calendar | |
| Calendar cal = Calendar.getInstance(); | |
| cal.add(Calendar.MINUTE, 10); | |
| givenDate = cal.getTime(); | |
| assert10minutesInFuture(givenDate); | |
| // joda-time | |
| givenDate = new DateTime().plusMinutes(10).toDate(); | |
| assert10minutesInFuture(givenDate); | |
| } | |
| @Test | |
| public void benchmarkFirstMoment() { | |
| Date dayOne; | |
| // DateDSL | |
| dayOne = date().firstDayOfMonth().clearTime().toDate(); | |
| // CO0OL date().firstDayOfNextMonth().toDate(); | |
| assertFirstMoment(dayOne); | |
| // java.util.Date | |
| // no way = new Date(103, 0, 10, 12, 0); | |
| // java.util.Calendar | |
| Calendar cal = Calendar.getInstance(); | |
| cal.set(Calendar.DAY_OF_MONTH, 1); | |
| cal.set(Calendar.HOUR_OF_DAY, 0); | |
| cal.set(Calendar.MINUTE, 0); | |
| cal.set(Calendar.SECOND, 0); | |
| cal.set(Calendar.MILLISECOND, 0); | |
| dayOne = cal.getTime(); | |
| assertFirstMoment(dayOne); | |
| // joda-time | |
| dayOne = new DateTime().withDayOfMonth(1).toDateMidnight().toDate(); | |
| assertFirstMoment(dayOne); | |
| } | |
| @Test | |
| public void benchmarkLastMoment() { | |
| Date lastDay; | |
| // DateDSL | |
| lastDay = date().lastDayOfMonth().lastTime().toDate(); | |
| // CO00OL date().lastDayOfNextMonth().toDate(); | |
| assertLastMoment(lastDay); | |
| // java.util.Date | |
| // no way = new Date(103, 0, 10, 12, 0); | |
| // java.util.Calendar | |
| Calendar cal = Calendar.getInstance(); | |
| cal.set(Calendar.DAY_OF_MONTH, | |
| cal.getActualMaximum(Calendar.DAY_OF_MONTH)); | |
| cal.set(Calendar.HOUR_OF_DAY, 23); | |
| cal.set(Calendar.MINUTE, 59); | |
| cal.set(Calendar.SECOND, 59); | |
| cal.set(Calendar.MILLISECOND, 999); | |
| lastDay = cal.getTime(); | |
| assertLastMoment(lastDay); | |
| // joda-time | |
| lastDay = new DateTime().withDayOfMonth(1).plusMonths(1).withMillisOfDay(0).minusMillis(1) | |
| .toDate(); | |
| assertLastMoment(lastDay); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment