Java 中国的Unix时代

Java 中国的Unix时代,java,simpledateformat,unix-timestamp,worldtimeapi,Java,Simpledateformat,Unix Timestamp,Worldtimeapi,我想知道中国的当地时间。我从worldtimeapi.org网站上获得UnixtTimeStamp 问题:我用的是当地时间而不是中国时间 private class BackgroundProcess extends AsyncTask<Void, Void, String> { @Override protected String doInBackground(Void... voids) { String value = null;

我想知道中国的当地时间。我从worldtimeapi.org网站上获得UnixtTimeStamp

问题:我用的是当地时间而不是中国时间

private class BackgroundProcess extends AsyncTask<Void, Void, String> {




    @Override
    protected String doInBackground(Void... voids) {
        String value = null;
        HttpHandler httpHandler = new HttpHandler();
        // Making a request to url and getting response
        String url = "http://worldtimeapi.org/api/timezone/Asia/Shanghai";

        String jsonStr = httpHandler.makeServiceCall(url);

        Log.e(TAG, "Response from url: " + jsonStr);

        if (jsonStr != null) {
            try {
                JSONObject jsonObj = new JSONObject(jsonStr);

               value = jsonObj.getString("unixtime");

                Log.e(TAG, "Operation Okay: " + "\n\n"+value);

            } catch (final JSONException e) {

                Log.e(TAG, "Json parsing error: " + e.getMessage());



            }

        } else {
            Log.e(TAG, "Couldn't get json from server.");

        }

        return value ;





    }

    @Override
    protected void onPostExecute(String aVoid) {
        super.onPostExecute(aVoid);

        Toast.makeText(Timer_FullTime.this, aVoid, Toast.LENGTH_SHORT).show();

        long l = Long.valueOf(aVoid);
        long milliSec = l * 1000 ;



        SimpleDateFormat formatter = new SimpleDateFormat("hh:mm a dd/MM/yyyy");
        String dateString = formatter.format(new Date(milliSec));
        currentTime.setText("" + dateString);
    }
}
私有类BackgroundProcess扩展异步任务{
@凌驾
受保护的字符串背景(无效…无效){
字符串值=null;
HttpHandler HttpHandler=新的HttpHandler();
//向url发出请求并获得响应
字符串url=”http://worldtimeapi.org/api/timezone/Asia/Shanghai";
字符串jsonStr=httpHandler.makeServiceCall(url);
Log.e(标签,“来自url的响应:+jsonStr”);
if(jsonStr!=null){
试一试{
JSONObject jsonObj=新的JSONObject(jsonStr);
value=jsonObj.getString(“unixtime”);
Log.e(标记“操作正常:”+“\n\n”+值);
}捕获(最终JSONException e){
Log.e(标记“Json解析错误:”+e.getMessage());
}
}否则{
e(标记“无法从服务器获取json”);
}
返回值;
}
@凌驾
受保护的void onPostExecute(字符串避免){
super.onPostExecute(避免);
Toast.makeText(Timer\u FullTime.this、aVoid、Toast.LENGTH\u SHORT.show();
long l=long.valueOf(避免);
长毫秒=l*1000;
SimpleDataFormat格式化程序=新的SimpleDataFormat(“hh:mm a dd/mm/yyyy”);
String dateString=formatter.format(新日期(毫秒));
currentTime.setText(“+dateString”);
}
}
我不明白为什么我不能得到现在6点半左右的《中国时间》, 相反,我一直在用我的当地时间,现在是3:54左右

提前感谢

避免遗留日期时间类 切勿使用
SimpleDateFormat
Date
。这些可怕的类在几年前被JSR310中定义的现代java.time类所取代

java.time 您似乎得到了一个长整数,表示自1970 UTC第一时刻的历元引用以来的整秒数

如果是,则将其解析为
即时

Instant instant = Instant.ofEpochSeconds( seconds ) ;
通过应用
ZoneId
获得
zoneDateTime
,从UTC调整到您的特定时区

ZoneId z = ZoneId.of( "Asia/Shanghai" ) ;
ZonedDateTime zdt = instant.atZone( z ) ;
对于较旧的Android,请参阅Threeten Backport和ThreeTenABP项目


所有这些都已经被讨论过很多次了。发布前搜索堆栈溢出。

好吧,您没有将SimpleDataFormat的时区设置为默认时区以外的任何其他时区,因此它会在您的时区中显示unix时间(在世界各地都是相同的)。获取当前时间戳只需
System.currentTimeMillis()
。要获取上海的当前时间,您只需
LocalDateTime。现在(ZoneId.of(“亚洲/上海”))
首先感谢您的时间,然后使用LocalDateTime。现在(ZoneId.of(“亚洲/上海”))我需要从worldtimeapi.prgNo调用毫秒的api。打电话给REST服务知道什么时候是没用的。你的电脑已经知道现在几点了。JVM有时区数据,可以将当前时间格式化为任何时区,如我所示。如果你的Minsdk版本是26(Android 8.0),你可以使用LocalDateTime,但问题是我的Minsdk版本是2619@saadzahoor我建议在这种情况下试试JodaTime