Java 中日期递减

发布于 2024-11-03 16:43:52 字数 236 浏览 3 评论 0原文

我想从当前时间获取前一天(24 小时)。

例如,如果当前时间是 Date currentTime = new Date();

2011-04-25 12:15:31:562 GMT

如何确定时间即

2011-04-24 12:15:31:562 GMT

I want to get the previous day (24 hours) from the current time.

e.g if current time is Date currentTime = new Date();

2011-04-25 12:15:31:562 GMT

How to determine time i.e

2011-04-24 12:15:31:562 GMT

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

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

发布评论

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

评论(5

眼藏柔 2024-11-10 16:43:52

您可以使用日历类来做到这一点:

Calendar cal = Calendar.getInstance();
cal.setTime ( date ); // convert your date to Calendar object
int daysToDecrement = -1;
cal.add(Calendar.DATE, daysToDecrement);
date = cal.getTime(); // again get back your date object

You can do that using Calendar class:

Calendar cal = Calendar.getInstance();
cal.setTime ( date ); // convert your date to Calendar object
int daysToDecrement = -1;
cal.add(Calendar.DATE, daysToDecrement);
date = cal.getTime(); // again get back your date object
缪败 2024-11-10 16:43:52

我建议您使用 Joda Time 开始,这是一个很多< /em> 更好的 API。然后您可以使用:

DateTime yesterday = new DateTime().minusDays(1);

请注意,“昨天的这个时间”并不总是 24 小时前...您需要考虑时区等。您可能需要使用 LocalDateTimeInstant 而不是 DateTime

I would suggest you use Joda Time to start with, which is a much nicer API. Then you can use:

DateTime yesterday = new DateTime().minusDays(1);

Note that "this time yesterday" isn't always 24 hours ago though... you need to think about time zones etc. You may want to use LocalDateTime or Instant instead of DateTime.

漆黑的白昼 2024-11-10 16:43:52

请在这里查看:
Java 日期与日历

Calendar cal=Calendar.getInstance();
cal.setTime(date); //not sure if date.getTime() is needed here
cal.add(Calendar.DAY_OF_MONTH, -1);
Date newDate = cal.getTime();

please checkout this here:
Java Date vs Calendar

Calendar cal=Calendar.getInstance();
cal.setTime(date); //not sure if date.getTime() is needed here
cal.add(Calendar.DAY_OF_MONTH, -1);
Date newDate = cal.getTime();
始终不够 2024-11-10 16:43:52

24小时和1天不是一回事。但您可以使用日历来执行这两项操作:

Calendar c = Calendar.getInstance();
c.setTime(new Date());
c.add(Calendar.DATE, -1);
Date d = c.getTime();

如果您要返回 24 小时,则可以使用 Calendar.HOUR_OF_DAY

24 hours and 1 day are not the same thing. But you do both using Calendar:

Calendar c = Calendar.getInstance();
c.setTime(new Date());
c.add(Calendar.DATE, -1);
Date d = c.getTime();

If you are going back 24 hours, you would use Calendar.HOUR_OF_DAY

凉月流沐 2024-11-10 16:43:52

java.time

java.util Date-Time API 及其格式化 API SimpleDateFormat 已过时且容易出错。建议完全停止使用它们并切换到 现代日期时间 API*

另外,下面引用的是Joda-Time主页的通知< /a>:

请注意,从 Java SE 8 开始,用户被要求迁移到 java.time (JSR-310) - JDK 的核心部分,它将取代该项目。

使用现代日期时间 API java.time 的解决方案:

import java.time.Instant;
import java.time.temporal.ChronoUnit;

public class Main {
    public static void main(String[] args) {
        Instant now = Instant.now();
        System.out.println("Now:       " + now);

        Instant yesterday = now.minus(1, ChronoUnit.DAYS);
        System.out.println("Yesterday: " + yesterday);
    }
}

示例运行的输出:

Now:       2021-07-16T20:40:24.402592Z
Yesterday: 2021-07-15T20:40:24.402592Z

在线演示

无论出于何种原因,如果您需要将 Instant 的这个对象转换为 < code>java.util.Date,您可以按如下方式执行此操作:

Date date = Date.from(odt.toInstant());

跟踪:日期时间


* 无论出于何种原因,如果您必须坚持使用 Java 6 或 Java 7,您可以使用 ThreeTen-Backport 将大部分 java.time 功能向后移植到 Java 6 和 Java 6 7. 如果您正在处理 Android 项目,并且您的 Android API 级别仍然不符合 Java-8,请检查 通过脱糖提供 Java 8+ API如何在Android项目中使用ThreeTenABP

java.time

The java.util Date-Time API and their formatting API, SimpleDateFormat are outdated and error-prone. It is recommended to stop using them completely and switch to the modern Date-Time API*.

Also, quoted below is a notice from the home page of Joda-Time:

Note that from Java SE 8 onwards, users are asked to migrate to java.time (JSR-310) - a core part of the JDK which replaces this project.

Solution using java.time, the modern Date-Time API:

import java.time.Instant;
import java.time.temporal.ChronoUnit;

public class Main {
    public static void main(String[] args) {
        Instant now = Instant.now();
        System.out.println("Now:       " + now);

        Instant yesterday = now.minus(1, ChronoUnit.DAYS);
        System.out.println("Yesterday: " + yesterday);
    }
}

Output of a sample run:

Now:       2021-07-16T20:40:24.402592Z
Yesterday: 2021-07-15T20:40:24.402592Z

ONLINE DEMO

For any reason, if you need to convert this object of Instant to an object of java.util.Date, you can do so as follows:

Date date = Date.from(odt.toInstant());

Learn more about the modern Date-Time API from Trail: Date Time.


* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.

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