gpt4 book ai didi

java - 根据应用程序是否首次运行检索正确的数据

转载 作者:行者123 更新时间:2023-12-01 12:05:02 25 4
gpt4 key购买 nike

我想从 API 检索一些数据,但如果是第一次运行,那么它应该检索从年初开始的数据,但如果是下一次运行,那么它应该只检索新数据(即数据)仅在上次运行后可用)。

我的问题是保存和检索运行之间的时间戳的最佳方法是什么。

最佳答案

您可以使用Preferences API以独立于系统的方式存储和读取应用程序特定标记。

package com.preferencetest;

import java.util.prefs.Preferences;

public class PreferenceTest {

private static final String RUN_MARKER = "RUN_MARKER";

public static void main(String[] args) {

// Obtain a Preferences node for this class name.
final Preferences pref = Preferences.userRoot().node(
PreferenceTest.class.getName());

// Read the RUN_MARKER value. For the first start this should be the
// default value false.
final boolean previouslyStarted = pref.getBoolean(RUN_MARKER, false);

if(!previouslyStarted) {
// First run: Set the marker to true.
pref.putBoolean(RUN_MARKER, true);
System.out.println("First run");
} else {
System.out.println("This is not the first run.");
}
}
}

关于java - 根据应用程序是否首次运行检索正确的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27672720/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com