Nik
Nik

Reputation: 7214

How do I convert timestamp string to local time string?

I have timestamp string: "1989-01-01 00:00:00" and i need convert it to local date format.

I execute:

SimpleDateFormat TIMESTAMPFORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
DateFormat.getDateFormat(getContext()).format(TIMESTAMPFORMAT.parse("1989-01-01 00:00:00"));

And getDateFormat returns 31.12.1988

Why?

How can I receive 01.01.1989???

Upvotes: 1

Views: 730

Answers (2)

waqaslam
waqaslam

Reputation: 68177

In order to skip time-zone when formatting, I would suggest you to set it to default as below:

SimpleDateFormat TIMESTAMPFORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
TIMESTAMPFORMAT.setTimeZone(TimeZone.getTimeZone("GMT"));

Upvotes: 2

Jon Skeet
Jon Skeet

Reputation: 1500695

This is probably to do with the time zones involved. I strongly suspect that you're formatting in a different time zone to the one you're using for parsing, and the value goes to before midnight, basically. If you can use the same time zone on both, it's likely to work.

If you possibly can, I'd encourage you to use Joda Time instead though - you really want a LocalDate.

Upvotes: 2

Related Questions