File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 88
99import java .text .SimpleDateFormat ;
1010import java .util .Date ;
11+ import java .util .TimeZone ;
1112
1213/**
13- * A datetime object that will return the datetime in the format {@code yyyy-MM-dd hh:mm:ss }.
14+ * A datetime object that will return the datetime in the format {@code yyyy-MM-dd hh:mm:ss}.
1415 *
1516 * @author brettcsorba
1617 */
1718public class PiwikDate extends Date {
18- SimpleDateFormat format = new SimpleDateFormat ( "yyyy-MM-dd hh:mm:ss" ) ;
19+ SimpleDateFormat format ;
1920
2021 /**
2122 * Allocates a Date object and initializes it so that it represents the time
2223 * at which it was allocated, measured to the nearest millisecond.
2324 */
2425 public PiwikDate (){
2526 super ();
27+ format = new SimpleDateFormat ("yyyy-MM-dd hh:mm:ss" );
28+ format .setTimeZone (TimeZone .getTimeZone ("UTC" ));
2629 }
2730
2831 /**
@@ -33,11 +36,23 @@ public PiwikDate(){
3336 */
3437 public PiwikDate (long date ){
3538 super (date );
39+ format = new SimpleDateFormat ("yyyy-MM-dd hh:mm:ss" );
40+ format .setTimeZone (TimeZone .getTimeZone ("UTC" ));
3641 }
42+
43+ /**
44+ * Sets the time zone of the String that will be returned by {@link toString()}.
45+ * Defaults to UTC.
46+ * @param zone the TimeZone to set
47+ */
48+ public void setTimeZone (TimeZone zone ){
49+ format .setTimeZone (zone );
50+ }
51+
3752 /**
3853 * Converts this PiwikDate object to a String of the form:<br>
3954 * <br>
40- * {@code yyyy-MM-dd hh:mm:ss }.
55+ * {@code yyyy-MM-dd hh:mm:ss}.
4156 * @return a string representation of this PiwikDate
4257 */
4358 @ Override
Original file line number Diff line number Diff line change 66 */
77package org .piwik .java ;
88
9+ import java .util .TimeZone ;
910import org .junit .After ;
1011import org .junit .AfterClass ;
1112import static org .junit .Assert .assertEquals ;
@@ -53,14 +54,19 @@ public void testConstructor1(){
5354 PiwikDate date = new PiwikDate (1433186085092L );
5455
5556 assertNotNull (date );
57+
58+ assertEquals ("2015-06-01 07:14:45" , date .toString ());
5659 }
60+
5761 /**
58- * Test of toString method, of class PiwikDate.
62+ * Test of setTimeZone method, of class PiwikDate.
5963 */
6064 @ Test
61- public void testToString (){
65+ public void testSetTimeZone (){
6266 PiwikDate date = new PiwikDate (1433186085092L );
6367
68+ date .setTimeZone (TimeZone .getTimeZone ("America/New_York" ));
69+
6470 assertEquals ("2015-06-01 03:14:45" , date .toString ());
6571 }
6672
You can’t perform that action at this time.
0 commit comments