获取最近三年之间的数据,

发布于 2024-12-27 22:07:02 字数 241 浏览 1 评论 0原文

我尝试加载过去三年之间的数据。我想获取当前日期时间和过去 3 年前之间的数据。

例如:-从 1/19/2009 到当前时间 (1/19/2012)

我得到的当前时间如下。

String date= DateFormat.getDateInstance().format(new Date());

有人可以告诉我如何做到这一点吗?如果您有任何示例,那将是一个真正的帮助!

I'm try to load data between lasst three years. i want to get data between current datetime and last 3 years back.

eg:-FROM 1/19/2009 TO current time (1/19/2012)

I get the current time as below.

String date= DateFormat.getDateInstance().format(new Date());

Could someone please tell me the way to do this? If you have any worked through examples, that would be a real help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

泪冰清 2025-01-03 22:07:02

您可以使用 Calender 的 add()

        Calendar cal = Calendar.getInstance();
        SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
        Log.d("current date",dateFormat.format(cal.getTime()));
        cal.add(Calendar.YEAR, -3);
        Log.d("3 years previous date",dateFormat.format(cal.getTime()));

OUTPUT

01-19 05:34:52.148: DEBUG/current date(556): 19/01/2012
01-19 05:34:52.148: DEBUG/3 years previous date(556): 19/01/2009

You can use Calender's add()

        Calendar cal = Calendar.getInstance();
        SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
        Log.d("current date",dateFormat.format(cal.getTime()));
        cal.add(Calendar.YEAR, -3);
        Log.d("3 years previous date",dateFormat.format(cal.getTime()));

OUTPUT

01-19 05:34:52.148: DEBUG/current date(556): 19/01/2012
01-19 05:34:52.148: DEBUG/3 years previous date(556): 19/01/2009
捶死心动 2025-01-03 22:07:02

问题是什么?如何提前3年约会?那么这段代码将会很有用:

    Calendar c = Calendar.getInstance();
    c.setTimeInMillis(System.currentTimeMillis());

    //set up time to the last minute of today
    c.set(Calendar.HOUR, 23);
    c.set(Calendar.MINUTE, 59);
    c.set(Calendar.SECOND, 59);

    long to = c.getTimeInMillis();// end point of period

    c.add(Calendar.YEAR, -1 * yearCount);

    long since = c.getTimeInMillis();//start point of period

What is the question? How to get date 3 years earlier? Then this code will be useful:

    Calendar c = Calendar.getInstance();
    c.setTimeInMillis(System.currentTimeMillis());

    //set up time to the last minute of today
    c.set(Calendar.HOUR, 23);
    c.set(Calendar.MINUTE, 59);
    c.set(Calendar.SECOND, 59);

    long to = c.getTimeInMillis();// end point of period

    c.add(Calendar.YEAR, -1 * yearCount);

    long since = c.getTimeInMillis();//start point of period
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文