0

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?

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Saef Myth
  • 287
  • 8
  • 19
  • 1
    What's the input type which you want to convert to the format ? – Muzammil Husnain Oct 17 '17 at 12:21
  • THIS date is 2017-10-17 10:23:30 in UTC it should be 2017-10-17 01:23:30 – Saef Myth Oct 17 '17 at 12:24
  • since my UTC offset is +3 – Saef Myth Oct 17 '17 at 12:24
  • It’s easy to get confused with the old and now long outdated classes `Date` and `SimpleDateFormat` that Android offers built-in. It’s worth considering getting the modern Java date and time API in **ThreeTen ABP**. This will allow more natural and straightforward code for the job. See [this question: How to use ThreeTenABP in Android Project](https://stackoverflow.com/questions/38922754/how-to-use-threetenabp-in-android-project). – Ole V.V. Oct 19 '17 at 12:08
  • //you need to change the "hh" to "HH" private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//changed hh to HH so that you can have 12 hour format. TimeZone tz = TimeZone.getDefault(); // +03:00 dateFormat.setTimeZone(tz); Date parsed_date = dateFormat.parse("2017-10-17 10:23:30"); – Dharma Cool Aug 22 '20 at 08:32

2 Answers2

1

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;
        }
Saef Myth
  • 287
  • 8
  • 19
  • 1
    If your hours are on a 24 hours clock (0 through 23), you want capital `HH` in the format pattern strings. Lowercase `hh` is for hour within AM or PM (1 through 12). – Ole V.V. Oct 19 '17 at 12:22
0

A Date does not have a time zone. So your parsing works since Tue Oct 17 13:23:30 GMT+03:00 2017 is the same point in time as 2017-10-17 10:23:30 UTC.

The time zone is only added when you try to print a date. For example:

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));

Date parsed_date= dateFormat.parse("2017-10-17 10:23:30");
String strDate = dateFormat.format(parsed_date);
System.out.println(strDate);

prints:

2017-10-17 10:23:30

Update: to show a date in another format / time-zone you can use something like this:

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

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

SimpleDateFormat printFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
printFormat.setTimeZone(TimeZone.getTimeZone("GMT+3"));
String strDate = printFormat.format(parsed_date);
System.out.println(strDate);

This prints:

2017-10-17 01:23:30
Henry
  • 42,982
  • 7
  • 68
  • 84