Search code examples
androidsimpledateformatutcdatetime-parsing

Parsing datetime to UTC


I have this date => 2017-10-17 10:23:30 UTC TIME

I'm parsing it like this

    private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
    TimeZone tz = TimeZone.getDefault(); // +03:00
    dateFormat.setTimeZone(tz);

    Date parsed_date = dateFormat.parse("2017-10-17 10:23:30");

but this gives me this

Tue Oct 17 13:23:30 GMT+03:00 2017

I want this

2017-10-17 01:23:30

What's the problem?


Solution

  • Thank you guys for your help that's what i wonted i turn out that if the date in UTC you should start with parsing in the utc format that will keep the date as its the for formating it to your local timezone format it with local time zone

       private String getDate(String dateString) {
                SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
                formatter.setTimeZone(TimeZone.getTimeZone("UTC"));
                Date value = null;
                try {
                    value = formatter.parse(dateString);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                SimpleDateFormat dateFormatter = new SimpleDateFormat("hh:mm aa");
                dateFormatter.setTimeZone(TimeZone.getDefault());
                String dt = dateFormatter.format(value);
    
                return dt;
            }