Search code examples
androidtimezonesimpledateformat

convert utc to local time in iran


I try convert UTC time to local time , minute work well but hour always have 1hour later, for example if UTC time is 04:55 , my phone clock is 9:25 but my code generate 8:25

   String dateStr = "04:55";
        SimpleDateFormat df = new SimpleDateFormat("HH:mm");
        df.setTimeZone(TimeZone.getTimeZone("UTC"));
        Date date = null;
        try {
            date = df.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        df.setTimeZone(TimeZone.getDefault());
        String formattedDate = df.format(date);

        time.setText(String.valueOf(formattedDate));

Solution

  •  df.setTimeZone(TimeZone.getDefault());
    

    to

     df.setTimeZone(TimeZone.getTimeZone("GMT+4:30"));
    

    working code

    String dateStr = "04:55";
                    SimpleDateFormat df = new SimpleDateFormat("HH:mm");
                    df.setTimeZone(TimeZone.getTimeZone("UTC"));
                    Date date = null;
                    try {
                        date = df.parse(dateStr);
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                    df.setTimeZone(TimeZone.getTimeZone("GMT+4:30"));
                    String formattedDate = df.format(date);
    
    
                    System.out.println(formattedDate);
                    //time.setText(String.valueOf(formattedDate));
    
            }
    

    output i got 9:25