我正在开发一个应用程序,其中用户将注册产品,我需要的日期和时间,当他在UTC+5注册的产品从谷歌或互联网。我真的对此一无所知。格式-周一03.12.2016 23:54。还有,有没有办法改变语言,让星期六用俄语写?
发布于 2019-09-02 17:38:19
有一个用于获取真实设备时间的a library called TrueTime。Wiki示例(使用Rx):
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();
});发布于 2019-10-16 06:09:43
使用HttpGet、客户机和响应,我设法从Response Date头中获取服务器的当前时间。我可以随时调用它,并将获得自信的响应(Google几乎100%可用,我可以信任获得正确的日期和时间)
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
发布于 2019-09-02 17:04:12
使用下面的代码
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;
}https://stackoverflow.com/questions/57754116
复制相似问题