【问题标题】:Get time according to current location/current time zone根据当前位置/当前时区获取时间
【发布时间】:2019-12-15 00:51:04
【问题描述】:

即使我从手机更改当前时区并更改时间,我也想根据当前时区获取当前时间,但我仍然应该能够获取当前时间和日期。如何获得?

Time currentTime = new Time();
currentTime.setToNow();

Calendar cal = Calendar.getInstance();
TimeZone tz = cal.getTimeZone();

Date date = cal.getTime();
strDate = date.toString();

【问题讨论】:

  • 您正在使用糟糕的日期时间类,这些类在几年前被 JSR 310 中定义的 java.time 类所取代。

标签: java android


【解决方案1】:

使用此方法获取当地时间

   public static Date getLocalDate(String timeStamp) {
        int index = timeStamp.indexOf("+");
        String dateTime;
        if (index > 0) {
            timeStamp = timeStamp.substring(0, index);
        }

        int indexGMT = timeStamp.indexOf("GMT");
        int indexPlus = timeStamp.indexOf("+");
        if (indexGMT > 0) {
            dateTime = timeStamp.substring(0, indexGMT);
        } else if (indexPlus > 0) {
            dateTime = timeStamp.substring(0, indexPlus);
        } else {
            dateTime = timeStamp;
        }
        SimpleDateFormat sdfgmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        sdfgmt.setTimeZone(TimeZone.getTimeZone("GMT"));
        SimpleDateFormat sdfmad = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        sdfmad.setTimeZone(TimeZone.getDefault());
        Date gmtDate;
        Date localDate = null;
        try {
            gmtDate = sdfgmt.parse(dateTime);
            localDate = sdfmad.parse(sdfmad.format(gmtDate));
        } catch (ParseException e) {

        }
        return localDate;
    }

要获取当前的 UNIX 时间戳,您可以使用以下代码

 long unixTime = System.currentTimeMillis() / 1000L;

【讨论】:

  • 但是用户不仅可以是印度的任何国家,而且还应该自动生成当前位置的时间戳
  • @Aditya UNIX 时间戳始终是唯一的,无论位置(时区)如何。
  • @Aditya 作为参考,请查看 RobP stackoverflow.com/a/23062640/3948854的答案
  • 我尝试了您的代码@Rishabh,但它不起作用,因为我将时间戳设为 --- 1565588955 并将其传递给函数并出现错误
  • @Aditya 有效吗?在解析为函数之前,您必须将其解析为字符串。
【解决方案2】:

试试这个

科特林

fun getDateTimeString( ): String {
            val calendar = Calendar.getInstance()
            calendar.timeZone= TimeZone.getDefault()
            calendar.timeInMillis = Date().time
            return SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()).format(calendar.time)

    }

JAVA

private String getDateTimeString(){
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeZone(TimeZone.getDefault());
        calendar.setTimeInMillis(new Date().getTime());
        return new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()).format(calendar.getTime());
    }

从 Google 获取时间:How to get current time from Google for Android?

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());
    }

【讨论】:

  • 你能给我java代码而不是kotlin @Lakhwinder
  • 我从设置中更改了我的时间和日期,然后它给了我更改的日期和时间,而不是今天的日期和时间@Lakhwinder
  • 显然,它会根据您设备的当前时间为您提供时间基准,您实际需要什么?
  • 我想根据当前时区获取当前时间,即使我从手机更改了当前时区并更改了时间或日期,但我仍然应该能够获取当前时间和日期.如何获得?
  • 有帮助吗,如果我让您知道时间没有设置为自动,那么您可以要求用户将时间设置为自动,它会始终为您提供正确的时间
【解决方案3】:

java.time

使用现代的 java.time 类。

获取JVM当前的默认时区。

ZoneId z = ZoneId.systemDefault() ; 

或者询问用户。

ZoneId z = ZoneId.of( "Africa/Tunis" ) ;

获取该地区人们使用的挂钟时间显示的当前时刻。

ZonedDateTime zdt = ZonedDateTime.now( z ) ;

提取日期部分。

LocalDate ld = zdt.toLocalDate() ;

提取一天中的时间。

LocalTime lt = zdt.toLocalTime() ; 

如果您想要更新,如果您想再次使用当前可能是当前区域的任何区域收集当前时刻,只需重复上述步骤即可。 java.time 对象是不可变的,不会自动更新;您必须请求具有新数据的新对象。

ZonedDateTime.now( ZoneId.systmDefault() ).toLocalDate()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-22
    • 2011-10-29
    • 1970-01-01
    • 1970-01-01
    • 2017-04-30
    • 1970-01-01
    相关资源
    最近更新 更多