Skip to content

Commit f4b1838

Browse files
committed
Added timezone to PiwikDate
1 parent bfbbd1e commit f4b1838

2 files changed

Lines changed: 26 additions & 5 deletions

File tree

src/org/piwik/java/PiwikDate.java

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,24 @@
88

99
import java.text.SimpleDateFormat;
1010
import 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
*/
1718
public 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

test/org/piwik/java/PiwikDateTest.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
*/
77
package org.piwik.java;
88

9+
import java.util.TimeZone;
910
import org.junit.After;
1011
import org.junit.AfterClass;
1112
import 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

0 commit comments

Comments
 (0)