package com.alibaba.json.bvt.date;

import java.text.SimpleDateFormat;
import java.util.Date;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.annotation.JSONField;

import junit.framework.TestCase;
import org.junit.Assert;

public class DateFieldFormatTest extends TestCase {
    
    public void test_format_() throws Exception {
        Date now = new Date();
        Model model = new Model();
        model.serverTime = now;
        model.publishTime = now;
        model.setStartDate( now );
        
        String text = JSON.toJSONString(model);
        System.out.println(text);

        String t1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(now);
        String t2 = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(now);
        String t3 = new SimpleDateFormat("yyyy-MM-dd").format(now);

        Assert.assertEquals("{\"publishTime\":\""+t2+"\",\"serverTime\":\""+t1+"\",\"startDate\":\""+t3+"\"}",text);
        
        Model model2 = JSON.parseObject(text, Model.class);
        Assert.assertEquals(t2,new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(model2.publishTime));
        Assert.assertEquals(t1,new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(model2.serverTime));
        Assert.assertEquals(t3,new SimpleDateFormat("yyyy-MM-dd").format(model2.getStartDate()));
        
    }

    public static class Model {

        @JSONField(format = "yyyy-MM-dd HH:mm:ss")
        public Date serverTime;

        @JSONField(format = "yyyy/MM/dd HH:mm:ss")
        public Date publishTime;

        @JSONField(format = "yyyy-MM-dd")
        private Date startDate;

        public Date getStartDate() {
            return startDate;
        }

        public void setStartDate(Date startDate) {
            this.startDate = startDate;
        }
    }
}

@@ -82,24 +82,76 @@
 a.util.Date;
+%0Aimport java.util.Locale;%0Aimport java.util.TimeZone;
 %0A%0Aimport com
@@ -335,16 +335,182 @@
 stCase %7B
+%0A%0A    protected void setUp() throws Exception %7B%0A        JSON.defaultTimeZone = TimeZone.getTimeZone(%22Asia/Shanghai%22);%0A        JSON.defaultLocale = Locale.CHINA;%0A    %7D
 %0A    %0A  

add timezone in setup