44import static org .junit .Assert .assertFalse ;
55import static org .junit .Assert .assertTrue ;
66
7+ import java .text .DateFormat ;
8+ import java .text .ParseException ;
9+ import java .text .SimpleDateFormat ;
710import java .time .LocalDate ;
11+ import java .time .LocalDateTime ;
812import java .time .Month ;
913import java .time .Period ;
1014import java .time .ZoneId ;
1115import java .time .ZonedDateTime ;
1216import java .time .chrono .ThaiBuddhistDate ;
1317import java .time .format .DateTimeFormatter ;
1418import java .time .temporal .ChronoField ;
19+ import java .util .Calendar ;
20+ import java .util .Date ;
21+ import java .util .GregorianCalendar ;
22+ import java .util .TimeZone ;
1523
1624import org .joda .time .DateTime ;
1725import org .joda .time .DateTimeFieldType ;
2129import org .joda .time .format .DateTimeFormat ;
2230import org .junit .Test ;
2331
32+ import sun .util .BuddhistCalendar ;
33+
2434/**
2535 * <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/>
2636 * <a href="http://jug.ua/wp-content/uploads/2013/02/JODAjsr310.pdf">nice presentation (JUG Ukraine)</a><br/>
@@ -88,6 +98,14 @@ public void timeBetweenToDates() {
8898 System .out .println (betweenJoda .getYears () + " Years " + betweenJoda .getMonths () + " Months " + betweenJoda .getDays () + " Days." );
8999 assertEquals (32 , betweenJoda .getYears ());
90100 assertEquals (2 , betweenJoda .getMonths ());
101+
102+ // Date & Calendar
103+ Calendar birthdayCalendar = Calendar .getInstance ();
104+ birthdayCalendar .set (1981 , Calendar .SEPTEMBER , 3 );
105+ int diffYears = Calendar .getInstance ().get (Calendar .YEAR ) - birthdayCalendar .get (Calendar .YEAR );
106+ int diffMonth = Calendar .getInstance ().get (Calendar .MONTH ) - birthdayCalendar .get (Calendar .MONTH );
107+ assertEquals (32 , diffYears );
108+ assertEquals (2 , diffMonth );
91109 }
92110
93111 @ Test
@@ -102,11 +120,23 @@ public void checkDateIsInInterval() {
102120 Interval interval = new Interval (new DateTime (1981 , 9 , 3 , 12 , 0 ), DateTime .now ());
103121 assertFalse (interval .contains (new DateTime (1980 , 1 , 1 , 12 , 0 )));
104122 assertTrue (interval .contains (new DateTime (1985 , 1 , 1 , 12 , 0 )));
123+
124+ // Date & Calendar
125+ Calendar birthdayCalendar = Calendar .getInstance ();
126+ birthdayCalendar .set (1981 , Calendar .SEPTEMBER , 3 );
127+ Calendar nowCalendar = Calendar .getInstance ();
128+ assertFalse (checkInterval (birthdayCalendar , nowCalendar , new GregorianCalendar (1980 , 1 , 1 )));
129+ assertTrue (checkInterval (birthdayCalendar , nowCalendar , new GregorianCalendar (1985 , 1 , 1 )));
130+
105131 }
106132
107133 private boolean checkInterval (LocalDate from , LocalDate to , LocalDate date ) {
108134 return from .isBefore (date ) && to .isAfter (date );
109135 }
136+
137+ private boolean checkInterval (Calendar from , Calendar to , Calendar date ) {
138+ return date .getTime ().after (from .getTime ()) && date .getTime ().before (to .getTime ());
139+ }
110140
111141 @ Test
112142 public void calculate5Years () {
@@ -123,65 +153,102 @@ public void calculate5Years() {
123153 assertEquals (birthdayPlusJoda .getDayOfMonth (), 3 );
124154 assertEquals (birthdayPlusJoda .getMonthOfYear (), 9 );
125155 assertEquals (birthdayPlusJoda .getYear (), 1991 );
156+
157+ // Date & Calendar
158+ Calendar birthdayCalendar = Calendar .getInstance ();
159+ birthdayCalendar .set (1981 , Calendar .SEPTEMBER , 3 );
160+ birthdayCalendar .add (Calendar .YEAR ,10 );
161+ assertEquals (birthdayCalendar .get (Calendar .DAY_OF_MONTH ), 3 );
162+ // !! 0-based
163+ assertEquals (birthdayCalendar .get (Calendar .MONTH ), Calendar .SEPTEMBER );
164+ assertEquals (birthdayCalendar .get (Calendar .YEAR ), 1991 );
126165 }
127166
128167 @ Test
129168 public void chronologies () {
130169 // JSR-310
131170 ThaiBuddhistDate thaiBuddhistDate = ThaiBuddhistDate .now ();
132- System .out .println (thaiBuddhistDate .getChronology ());
133171 int thaiYear = thaiBuddhistDate .get (ChronoField .YEAR );
134172 LocalDate isoDate = LocalDate .now ();
135- System .out .println (isoDate .getChronology ());
136173 int isoYear = isoDate .get (ChronoField .YEAR );
137-
138174 assertEquals (thaiYear , isoYear + 543 );
139175
140176 // Joda
141177 org .joda .time .LocalDate buddhistDateJoda = new org .joda .time .LocalDate (BuddhistChronology .getInstance ());
142178 int buddhistYearJoda = buddhistDateJoda .get (DateTimeFieldType .year ());
143179 org .joda .time .LocalDate dateJoda = new org .joda .time .LocalDate ();
144180 int yearJoda = dateJoda .get (DateTimeFieldType .year ());
145-
146181 assertEquals (buddhistYearJoda , yearJoda + 543 );
182+
183+ // Date & Calendar
184+ BuddhistCalendar buddhistCalendar = new BuddhistCalendar ();
185+ int buddhistYearCalendar = buddhistCalendar .get (BuddhistCalendar .YEAR );
186+ Calendar calendar = Calendar .getInstance ();
187+ int yearCalendar = calendar .get (Calendar .YEAR );
188+ assertEquals (buddhistYearCalendar , yearCalendar + 543 );
147189 }
190+
148191
149192 @ Test
150- public void timezoneAndOffset () {
193+ public void timezoneAndOffset () throws InterruptedException {
151194 // JSR-310
152- ZonedDateTime zonedDateTimeEurope = ZonedDateTime .now ();
153- ZonedDateTime zonedDateTimeUTC = ZonedDateTime .now (ZoneId .of ("UTC" ));
195+ LocalDateTime dateTime = LocalDateTime .now ();
196+ ZonedDateTime zonedDateTimeEurope = ZonedDateTime .of (dateTime , ZoneId .systemDefault ());
197+ ZonedDateTime zonedDateTimeUTC = ZonedDateTime .of (dateTime , ZoneId .of ("UTC" ));
154198
155199 assertEquals (60 * 60 * 1 , zonedDateTimeEurope .getOffset ().getTotalSeconds ());
156200 assertEquals ("Europe/Berlin" , zonedDateTimeEurope .getZone ().getId ());
157- assertFalse (zonedDateTimeEurope .isBefore (zonedDateTimeUTC ));
201+ //!!
202+ assertTrue (zonedDateTimeEurope .isBefore (zonedDateTimeUTC ));
158203 assertFalse (zonedDateTimeEurope .isAfter (zonedDateTimeUTC ));
159- assertTrue (zonedDateTimeEurope .isEqual (zonedDateTimeUTC ));
204+ assertFalse (zonedDateTimeEurope .isEqual (zonedDateTimeUTC ));
160205
161206 // Joda
162- DateTime dateTimeEuropeJoda = DateTime .now ();
163- DateTime dateTimeUTCJoda = DateTime .now (DateTimeZone .UTC );
207+ Date date = new Date ();
208+ DateTime dateTimeEuropeJoda = new DateTime (date .getTime (),DateTimeZone .getDefault ());
209+ DateTime dateTimeUTCJoda = new DateTime (date .getTime (),DateTimeZone .UTC );
164210
165211 assertEquals (60 * 60 * 1 * 1000 , dateTimeEuropeJoda .getZone ().getOffset (dateTimeEuropeJoda .getMillis ()));
166212 assertEquals ("Europe/Berlin" , dateTimeEuropeJoda .getZone ().getID ());
213+ //!!
167214 assertFalse (dateTimeEuropeJoda .isBefore (dateTimeUTCJoda ));
168215 assertFalse (dateTimeEuropeJoda .isAfter (dateTimeUTCJoda ));
169216 assertTrue (dateTimeEuropeJoda .isEqual (dateTimeUTCJoda ));
217+
218+ // Date & Calendar
219+ Calendar calendarEurope = Calendar .getInstance ();
220+ calendarEurope .setTime (date );
221+ Calendar calendarUTC = Calendar .getInstance (TimeZone .getTimeZone ("UTC" ));
222+ calendarEurope .setTime (date );
223+ assertFalse (calendarEurope .getTime ().before (calendarUTC .getTime ()));
224+ assertFalse (calendarEurope .getTime ().after (calendarUTC .getTime ()));
225+ assertTrue (calendarEurope .getTime ().equals (calendarUTC .getTime ()));
170226 }
171227
172228 @ Test
173229 public void parsingAndFormatting () {
174230 // JSR-310
175231 DateTimeFormatter fmt = DateTimeFormatter .ofPattern ("dd.MM.yyyy" );
176- LocalDate parsedDate = LocalDate .parse ("03.09.1981" , fmt );
177- assertEquals (LocalDate .of (1981 , 9 , 3 ), parsedDate );
232+ LocalDate parsedLocalDate = LocalDate .parse ("03.09.1981" , fmt );
233+ assertEquals (LocalDate .of (1981 , 9 , 3 ), parsedLocalDate );
178234 assertEquals ("03.09.1981" , fmt .format (LocalDate .of (1981 , 9 , 3 )));
179235
180236 // Joda
181237 org .joda .time .format .DateTimeFormatter fmtJoda = DateTimeFormat .forPattern ("dd.MM.yyyy" );
182238 org .joda .time .LocalDate parsedDateJoda = org .joda .time .LocalDate .parse ("03.09.1981" , fmtJoda );
183239 assertEquals (new org .joda .time .LocalDate (1981 , 9 , 3 ), parsedDateJoda );
184240 assertEquals ("03.09.1981" , fmtJoda .print (new org .joda .time .LocalDate (1981 , 9 , 3 )));
241+
242+ // Date & Calendar
243+ DateFormat dateFormat = new SimpleDateFormat ("dd.MM.yyyy" );
244+ Date parsedDate = null ;
245+ try {
246+ parsedDate = dateFormat .parse ("03.09.1981" );
247+ } catch (ParseException e ) {
248+ // handle
249+ }
250+ assertEquals (new GregorianCalendar (1981 , Calendar .SEPTEMBER ,3 ).getTime (), parsedDate );
251+ assertEquals ("03.09.1981" , dateFormat .format (new GregorianCalendar (1981 , Calendar .SEPTEMBER ,3 ).getTime ()));
185252 }
186253
187254}
0 commit comments