convert Utc date to Local date in string - Android java.util

Android examples for java.util:Date

Description

convert Utc date to Local date in string

Demo Code

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

public class Main {

  public static String convertUtc2Local(String utcTime) {
    String time = "";
    if (utcTime != null) {
      SimpleDateFormat utcFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.CHINA);
      utcFormatter.setTimeZone(TimeZone.getTimeZone("UTC"));
      Date gpsUTCDate = null;//from  ww  w.j  a va 2 s  . c  o  m
      try {
        gpsUTCDate = utcFormatter.parse(utcTime);
      } catch (ParseException e) {
        e.printStackTrace();
      }
      SimpleDateFormat localFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
      localFormatter.setTimeZone(TimeZone.getDefault());
      assert gpsUTCDate != null;
      time = localFormatter.format(gpsUTCDate.getTime());
    }
    return time;
  }

}

Related Tutorials