当服务器具有gmt时,获取设备上的当前时间

时间:2014-01-30 14:12:01

标签: android

我在服务器中存储以下代码作为gmt,并希望它根据Android设备时区更改。我错误的值无法弄清楚错误。我真的很感激任何帮助。
在此先感谢。

public class MainActivity extends Activity {

    TextView t;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        String d="2014-01-14 16:28:50";
        t=(TextView)findViewById(R.id.textView1);
        Timestamp ts = Timestamp.valueOf(d);    
        long tsTime1 = ts.getTime();
        String r=getDate(tsTime1);
        t.setText(r);
    }

    private String getDate(long timeStamp){
        SimpleDateFormat objFormatter = new SimpleDateFormat("yyyy-mm-dd hh:mm:ss");
        objFormatter.setTimeZone(TimeZone.getDefault());

        Calendar objCalendar =    
            Calendar.getInstance(TimeZone.getDefault());
        objCalendar.setTimeInMillis(timeStamp*1000);//edit
        String result = objFormatter.format(objCalendar.getTime());
        objCalendar.clear();
        return result;         
    }
}

1 个答案:

答案 0 :(得分:1)

public String TimeFormating(String Time)
 {
     SimpleDateFormat format_before = new SimpleDateFormat("MM-dd-yyyy HH:mm:ss");
     SimpleDateFormat format_to_Convert = new SimpleDateFormat("hh:mm a",Locale.getDefault());

     format_before.setTimeZone(TimeZone.getTimeZone("GMT"));
     format_to_Convert.setTimeZone(TimeZone.getDefault());
     Date time = null;
     try 
     {    
         time = format_before.parse(Time);

     } catch (ParseException e) 
     {
         e.printStackTrace();
     }

     return format_to_Convert.format(time).toLowerCase(Locale.getDefault());

 }