Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/395.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/231.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 两个单独的getTime()调用在不同的时间返回相同的值_Java_Android_Date_Time - Fatal编程技术网

Java 两个单独的getTime()调用在不同的时间返回相同的值

Java 两个单独的getTime()调用在不同的时间返回相同的值,java,android,date,time,Java,Android,Date,Time,我正在制作一个应用程序,当用户输入一个位置并退出时保存。我必须为此分离Date对象。当我打印出这些值时,它们返回相同的精确时间,尽管它们保存在不同的时间。你知道这是什么原因吗 我的检查位置()方法: private void checkLocation() { float[] distance = new float[2]; for(int i = 1; i <= AssetLoader.getTotalLocations(); i++) {

我正在制作一个应用程序,当用户输入一个位置并退出时保存。我必须为此分离
Date
对象。当我打印出这些值时,它们返回相同的精确时间,尽管它们保存在不同的时间。你知道这是什么原因吗

我的检查位置()方法:

private void checkLocation() {
            float[] distance = new float[2];
            for(int i = 1; i <= AssetLoader.getTotalLocations(); i++) {
                float mItemLat = AssetLoader.getItemLatitude(String.valueOf(i));
                float mItemLong = AssetLoader.getItemLongitude(String.valueOf(i));

                Location.distanceBetween(mLastLocation.getLatitude(), mLastLocation.getLongitude(),
                        mItemLat, mItemLong, distance);

                if(distance[0] > 55){
                    Toast.makeText(getBaseContext(), "Outside, distance from center: " + distance[0] + " radius: " + "55", Toast.LENGTH_LONG).show();
                    if(startDate != null) {
                        stopDate = calendar.getTime();
                        Log.e("STOP DATE", "SAVED");
                        saveTimeDifference(startDate, stopDate, String.valueOf(i));
                    }
                } else {
                    Toast.makeText(getBaseContext(), "Inside, distance from center: " + distance[0] + " radius: " + "55", Toast.LENGTH_LONG).show();
                    if(startDate == null) {
                        startDate = calendar.getTime();
                        Log.e("START DATE", "SAVED");
                    }
                }
            }
        }
请注意,当前时间是在不同的时间访问的。对GregorianCalendar对象调用getTime()时,它似乎保存错误或返回错误的时间

谢谢你的建议

calendar.getTime()
返回当前存储在
calendar
对象中的
java.util.Date
对象

当最初通过调用(或其他方式)获取
日历时,它具有“当前时间”

5小时后,它仍将保持相同的时间。这不是“实时”时间,因此多次调用
calendar.getTime()
将获得相同的值

如果要捕获当前时间,只需调用:

答案是正确的

java.time

如果您正在做更多的日期时间工作,请考虑使用新的JavaTimeFrimes框架来取代麻烦的旧日期时间类。

UTC 要以高达的分辨率获取时间线上的当前时刻,请使用

不变对象 java.time中的类使用该模式。每个实例的值永远不会改变(永远不会“变化”)。因此,当您想再次了解当前时刻时,请再次调用
Instant.now
创建另一个
Instant
实例

决议 在Java8中,当前时刻的捕获分辨率高达,而不是类的处理能力。此限制是由于使用了的遗留实现。在Java 9中,
Clock
的一个新实现捕获当前时刻,其分辨率高达纳秒,具体取决于计算机的硬件时钟

分区 虽然通常最好将日期时间值存储在中,但为了演示,您可能希望在用户期望的时区中显示这些值

要将其调整到特定的时间,请应用时区()。指定一个字母,不要使用3-4个字母的缩写,如
EST
IST

ZoneId zoneId = ZoneId.of( "America/Montreal" );
ZonedDateTime zdt = ZonedDateTime.ofInstant( now , zoneId );
逝去 要以天、小时、分钟、秒为单位跟踪经过的时间跨度,请使用类。作为文本表示,该类使用标准格式(如
PT1H30M
)解析并生成字符串,持续一个半小时

Duration duration = Duration.between( startInstant , stopInstant );
String output = duration.toString();
您可以获得此时间跨度内的总纳秒数、秒数、分钟数、小时数或天数

奇怪的是,Java8中的
Duration
类缺少方便地查询天部分、小时部分、分钟部分和秒部分的方法


对于以年、月、日为单位的时间跨度,请使用该类。

是否可以显示日历对象的初始化
private calendar=new gregoriacalendar(TimeZone.getDefault())所以这是一个字段,有很长的生命周期,而且它永远不会被更新。从OP的角度来看,使用System.currentTimeMillis()可能更简单;获取当前时间的时间戳。@Gauthier为True,但当前代码有
startDate
stopDate
作为类型为
Date
的字段,所以我只保留了它。
Date
对象通过提供(相对)漂亮的
toString()
输出,例如,对于OP已经调用的
println()
调用,确实有助于调试。感谢您的回复。这给了我正确的时间。谢谢你的补充信息。它帮了我很多忙!
Instant now = Instant.now();
ZoneId zoneId = ZoneId.of( "America/Montreal" );
ZonedDateTime zdt = ZonedDateTime.ofInstant( now , zoneId );
Duration duration = Duration.between( startInstant , stopInstant );
String output = duration.toString();