如何让逆时从美国时间机器人 [英] how to get reverse time in android from US time

查看:105
本文介绍了如何让逆时从美国时间机器人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

package com.example.currenttime;

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

public class Functions {

    private static long converTimeStringINToMillis1(String time) {

        long milliseconds = 0;

        try {
            SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a");
            sdf.setTimeZone(TimeZone.getTimeZone("UTC"));

            // 25/06/2014 8:41:26

            Date date;

            date = sdf.parse(time);
            milliseconds = date.getTime();
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            milliseconds = 0;
            e.printStackTrace();
        }

        return milliseconds;
    }

    public static String setLastSeenTime1(String time) {

        long milliseconds = Math.abs(System.currentTimeMillis()
                - converTimeStringINToMillis1(time));
        String lastSeen = "";

        int seconds = (int) milliseconds / 1000;
        if (seconds < 60)
            lastSeen = String.valueOf(seconds) + "sec ago";
        else if (seconds > 60 && seconds < 3600)
            lastSeen = String.valueOf((int) seconds / 60) + " min ago";
        else if (seconds > 3600 && seconds < 86400)
            lastSeen = String.valueOf((int) seconds / 3600) + " hours ago";
        else if (seconds > 86400 && seconds < 172800)
            lastSeen = " Yesterday";
        else if (seconds > 172800 && seconds < 2592000)
            lastSeen = String.valueOf((int) (seconds / (24 * 3600)))
                    + " days ago";
        else if (seconds > 2592000)
            lastSeen = String.valueOf((int) (seconds / (30 * 24 * 3600)))
                    + " months ago";

        return lastSeen;

    }

}

我有显示逆时我调用这个函数使用这个Android code的:

i have display reverse time i calling this function Using this of android code:

btn_retry.setText(Functions.setLastSeenTime1("09/10/2014 8:41:26 AM"))

但问题是,我得到它位于我们的服务器时间,我要根据本地时区显示,这样我可以看到弹出尊时间请建议我怎么会做到哪里做错了,请帮助。

But Problem is i am getting server time which is located in Us i have to display according to Local time ZOne so that i can see eject reveres time please Suggest me how i will do where am doing wrong please help .

推荐答案

更​​新:
这应该工作,只需更换这是你的工具类中,你只需要通过时间和serverTimeZone。

Update: This should work, just replace this within your utility class, you just have to pass the time and the serverTimeZone.

public static String setLastSeenTime1(String time, TimeZone serverTimeZone) throws  ParseException {

   SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a");
   sdf.setTimeZone(serverTimeZone);

   SimpleDateFormat sdf2 = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a");
   sdf.setTimeZone(TimeZone.getDefault());

   return sdf2.format(sdf.parse(time));

}

这篇关于如何让逆时从美国时间机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆