UcasRichard / snakeyaml

Automatically exported from code.google.com/p/snakeyaml
Apache License 2.0
0 stars 0 forks source link

Error when getting date. Date return with day minus one. #131

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Create a simple test case like this one:

@Test
    public void dateTest() {
        Yaml yaml = new Yaml();
        Date date = yaml.loadAs("2011-08-10", Date.class);

        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        assertEquals("2011-08-10", df.format(date));
    }

What is the expected output? What do you see instead?
The expected output is df.format returning "2011-08-10", but returns 
"2011-08-09".

What version of SnakeYAML are you using? On what Java version?
1.9

Please provide any additional information below. (Often a failing test is
the best way to describe the problem.)

I've already provided one.

Please, I'd like to know if it's really a bug or if I'm doing something wrong.

Thanks!
Thiago Senna

Original issue reported on code.google.com by trse...@gmail.com on 9 Sep 2011 at 2:50

GoogleCodeExporter commented 9 years ago
I have a test case that date was correctly parsed, but I need to change my bean 
to use Calendar beside Date.

The yaml string:

!Post &firstBobPost
    titulo:         About the model layer
    criadoEm:       2011-08-10
    autor:          *bob
    conteudo:       >
                    The model has a central position in a Play!

The Bean:

@Entity
public class Post extends AbstractPersistable<Long> {

    private String titulo;
    private Calendar criadoEm;

    @Lob
    private String conteudo;

    @ManyToOne
    private Usuario autor;

    @OneToMany(mappedBy="post", cascade=CascadeType.ALL)
    private List<Comentario> comentarios = new ArrayList<Comentario>();

....

The Test:
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
assertNotNull(post.getCriadoEm());assertEquals("2011-08-10", 
df.format(post.getCriadoEm().getTime()));

Original comment by trse...@gmail.com on 9 Sep 2011 at 2:57

GoogleCodeExporter commented 9 years ago
SimpleDateFormat is using by default your local time zone. Set the GMT time 
zone and the problem should disappear:

DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
df.setTimeZone(java.util.TimeZone.getTimeZone("GMT"));

Original comment by py4fun@gmail.com on 9 Sep 2011 at 4:46

GoogleCodeExporter commented 9 years ago
Very nice. Thanks and excuse-me for openning this issue.

Original comment by trse...@gmail.com on 9 Sep 2011 at 6:11

GoogleCodeExporter commented 9 years ago
You are welcome.

Original comment by py4fun@gmail.com on 9 Sep 2011 at 10:57