10

I have this date string : 2016-04-26T09:14:10.477Z which is in UTC timezone. I want to convert it to the user local Timezone, so if it is GMT +02:00, I want a date with a value of 2016-04-26T11:14:10.477Z (note the 9 changes to 11).

I've been using this so far :

val sdf = SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS", Locale.getDefault())
val now = Date()

val limit = sdf.parse(coupon.usedAt)

Here limit = Tue Apr 26 09:14:10 GMT+02:00 2016 which is not correct I suppose.

So how can I convert it correctly? Thanks!

Edit : my question differs from this one, since I need to use the object SimpleDateFormat and not the Date one

PEHLAJ
  • 9,980
  • 9
  • 41
  • 53
tufekoi
  • 929
  • 2
  • 14
  • 33
  • you can devide by 1000 of timestamp for convert UTC to GMT – Naitik Apr 26 '16 at 09:17
  • Possible duplicate of [How can I get the current date and time in UTC or GMT in Java?](http://stackoverflow.com/questions/308683/how-can-i-get-the-current-date-and-time-in-utc-or-gmt-in-java) – T D Nguyen Apr 26 '16 at 09:22
  • Try with this code. [Click here to view all formats of Date and time](https://stackoverflow.com/questions/5369682/get-current-time-and-date-on-android/34328449#34328449) – Amitsharma Aug 13 '17 at 18:39

5 Answers5

14

Try this

SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
Date myDate = simpleDateFormat.parse(rawQuestion.getString("AskDateTime"));
8

The question was in Kotlin and I see all the answers are written in Java, so here is the Kotlin solution

val formatter = SimpleDateFormat("yyyy-MM-dd HH:mm:ssZ", Locale.getDefault())
formatter.timeZone = TimeZone.getTimeZone("UTC")
val result = formatter.parse(dateAsString)
Alaa Eddine Cherbib
  • 1,062
  • 14
  • 17
0

Try this.

String dateString = "04/26/2016 10:22:00 AM";
    SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss aa");
    Date convertedDate = new Date();
    try {
        convertedDate = dateFormat.parse(dateString);
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    Calendar calendar = Calendar.getInstance();
    calendar.setTimeZone(TimeZone.getTimeZone("UTC"));
    calendar.setTime(convertedDate);
    Date time = calendar.getTime();
    SimpleDateFormat outputFmt = new SimpleDateFormat("MMM dd, yyy h:mm a zz");
    String utcTime = outputFmt.format(time);
Alex Chengalan
  • 8,211
  • 4
  • 42
  • 56
0
    DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
    Date date = sdf.parse("2016-04-26T09:14:10.477Z"); 
    sdf.setTimeZone(TimeZone.getTimeZone("IST"));
    System.out.println(sdf.format(date));
Exceptional
  • 2,994
  • 1
  • 18
  • 25
0

Try this, replace the format

public static final String DATE_FORMAT       = "yyyy-MM-dd'T'HH:mm:ss'Z'";

public static String getFormattedLocalTimeFromUtc (String utcTimeStamp, String outputFormat) {

   String formattedTime = null;
   if (!TextUtils.isEmpty (utcTimeStamp)) {
     if (utcTimeStamp.contains ("T")) {

        String localTime = null;
        SimpleDateFormat sdf = new SimpleDateFormat (DATE_FORMAT, Locale.getDefault ());
        sdf.setTimeZone (TimeZone.getTimeZone ("UTC"));
        try {
            localTime = sdf.parse (utcTimeStamp).toString ();
        }
        catch (ParseException e) {
            e.printStackTrace ();
        }

        DateFormat inputDateFormat = new SimpleDateFormat ("EEE MMM dd HH:mm:ss z yyyy");
        inputDateFormat.setTimeZone (TimeZone.getTimeZone ("UTC"));
        Date date = null;
        try {
            date = inputDateFormat.parse (localTime);
        }
        catch (ParseException e) {
            e.printStackTrace ();
        }

        DateFormat outputDateFormat = new SimpleDateFormat (outputFormat);
        formattedTime = outputDateFormat.format (date);
     }
   }
   return formattedTime;
}
PEHLAJ
  • 9,980
  • 9
  • 41
  • 53