首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从互联网或谷歌(Android studio)获取日期和时间

从互联网或谷歌(Android studio)获取日期和时间
EN

Stack Overflow用户
提问于 2019-09-02 17:00:34
回答 4查看 2K关注 0票数 0

我正在开发一个应用程序,其中用户将注册产品,我需要的日期和时间,当他在UTC+5注册的产品从谷歌或互联网。我真的对此一无所知。格式-周一03.12.2016 23:54。还有,有没有办法改变语言,让星期六用俄语写?

EN

回答 4

Stack Overflow用户

发布于 2019-09-02 17:38:19

有一个用于获取真实设备时间的a library called TrueTime。Wiki示例(使用Rx):

代码语言:javascript
复制
TrueTimeRx.build()
        .initializeRx("time.google.com")
        .subscribeOn(Schedulers.io())
        .subscribe(date -> {
            Log.v(TAG, "TrueTime was initialized and we have a time: " + date);
        }, throwable -> {
            throwable.printStackTrace();
        });
票数 1
EN

Stack Overflow用户

发布于 2019-10-16 06:09:43

使用HttpGet、客户机和响应,我设法从Response Date头中获取服务器的当前时间。我可以随时调用它,并将获得自信的响应(Google几乎100%可用,我可以信任获得正确的日期和时间)

代码语言:javascript
复制
try{
        HttpClient httpclient = new DefaultHttpClient();
        HttpResponse response = httpclient.execute(new HttpGet("https://google.com/"));
        StatusLine statusLine = response.getStatusLine();
        if(statusLine.getStatusCode() == HttpStatus.SC_OK){
            String dateStr = response.getFirstHeader("Date").getValue();
            //Here I do something with the Date String
            System.out.println(dateStr);

        } else{
            //Closes the connection.
            response.getEntity().getContent().close();
            throw new IOException(statusLine.getReasonPhrase());
        }
    }catch (ClientProtocolException e) {
        Log.d("Response", e.getMessage());
    }catch (IOException e) {
        Log.d("Response", e.getMessage());
    }

检查此项目:Click Here

票数 1
EN

Stack Overflow用户

发布于 2019-09-02 17:04:12

使用下面的代码

代码语言:javascript
复制
static final String DATEFORMAT = "yyyy-MM-dd HH:mm:ss"

public static Date GetUTCdatetimeAsDate()
{
    //note: doesn't check for null
    return StringDateToDate(GetUTCdatetimeAsString());
}

public static String GetUTCdatetimeAsString()
{
    final SimpleDateFormat sdf = new SimpleDateFormat(DATEFORMAT);
    sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
    final String utcTime = sdf.format(new Date());

    return utcTime;
}

public static Date StringDateToDate(String StrDate)
{
    Date dateToReturn = null;
    SimpleDateFormat dateFormat = new SimpleDateFormat(DATEFORMAT);

    try
    {
        dateToReturn = (Date)dateFormat.parse(StrDate);
    }
    catch (ParseException e)
    {
        e.printStackTrace();
    }

    return dateToReturn;
}
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57754116

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档