how to convert UTC date to locale GMT time on android

后端 未结 5 754
轻奢々
轻奢々 2021-01-05 04:53

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 d

相关标签:
5条回答
  • 2021-01-05 05:08

    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;
    }
    
    0 讨论(0)
  • 2021-01-05 05:14
        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));
    
    0 讨论(0)
  • 2021-01-05 05:15

    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);
    
    0 讨论(0)
  • 2021-01-05 05:19

    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)
    
    0 讨论(0)
  • 2021-01-05 05:22

    Try this

    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
    Date myDate = simpleDateFormat.parse(rawQuestion.getString("AskDateTime"));
    
    0 讨论(0)
提交回复
热议问题