JodaTime 从周期中获取值

发布于 2024-11-01 06:32:47 字数 240 浏览 8 评论 0原文

我有两个日期时间,一个是“自从”和“现在”的时间,

我需要的是获取那时之间的时间。

我的问题是我想要得到它的格式:

示例: 自 = '2010 年 4 月 17 日' now = '2011 年 4 月 15 日'

我想要 '0 年 11 个月 29 天'

如果现在是 '2010 年 4 月 13 日',结果应该是这样的: “1年0个月2天”

但是这个逻辑让我很困惑。

I have two DateTimes, one is the time 'since' and 'now'

What I need, is get the time between then.

My problem is into the format I want to get it:

Example:
since = '17 april 2010'
now = '15 april 2011'

I want to have '0 years, 11 months, 29 days'

And in case since is '13 april 2010' the result should be like:
'1 years, 0 months, 2 days'

But this logic is puzzling me.

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

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

发布评论

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

评论(2

掐死时间 2024-11-08 06:32:47

我不完全确定我明白你的问题。听起来像您想要的:

DateTime since = ...;
DateTime now = ...;

Period period = new Period(since, now, PeriodType.yearMonthDay());
int years = period.getYears();
int months = period.getMonths();
int days = period.getDays();

如果不是这样,您能提供更多详细信息吗?

I'm not entirely sure I follow your question. It sounds like you want:

DateTime since = ...;
DateTime now = ...;

Period period = new Period(since, now, PeriodType.yearMonthDay());
int years = period.getYears();
int months = period.getMonths();
int days = period.getDays();

If that isn't the case, could you give more details?

节枝 2024-11-08 06:32:47

java.time

下面引用的是 主页的通知乔达时间

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

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

  1. 使用 Period# Between 获取之间的时间段两个 LocalDate
  2. 请注意,字符串中的月份名称为小写,因此,您需要使用 parseCaseInsensitive 函数构建 DateTimeFormatter
  3. 切勿在没有区域设置的情况下使用DateTimeFormatter

演示:

import java.time.LocalDate;
import java.time.Period;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        DateTimeFormatter dtf = new DateTimeFormatterBuilder()
                                .parseCaseInsensitive()
                                .appendPattern("d MMMM uuuu")
                                .toFormatter(Locale.ENGLISH);
        
        LocalDate since = LocalDate.parse("17 april 2010", dtf);
        LocalDate now = LocalDate.parse("15 april 2011", dtf);
        
        Period period = Period.between(since, now);
        
        String strPeriod = String.format("%d years %d months %d days", period.getYears(), period.getMonths(),
                period.getDays());
        System.out.println(strPeriod);
    }
}

输出:

0 years 11 months 29 days

在线演示< /a>

Trail 了解有关现代日期时间 API 的更多信息:日期时间


* 无论出于何种原因,如果您必须坚持使用 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

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:

  1. Use Period#between to get the period between two LocalDates.
  2. Note that the month name in your string is in lowercase and therefore, you will need to build the DateTimeFormatter using parseCaseInsensitive function.
  3. Never use DateTimeFormatter without a Locale.

Demo:

import java.time.LocalDate;
import java.time.Period;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        DateTimeFormatter dtf = new DateTimeFormatterBuilder()
                                .parseCaseInsensitive()
                                .appendPattern("d MMMM uuuu")
                                .toFormatter(Locale.ENGLISH);
        
        LocalDate since = LocalDate.parse("17 april 2010", dtf);
        LocalDate now = LocalDate.parse("15 april 2011", dtf);
        
        Period period = Period.between(since, now);
        
        String strPeriod = String.format("%d years %d months %d days", period.getYears(), period.getMonths(),
                period.getDays());
        System.out.println(strPeriod);
    }
}

Output:

0 years 11 months 29 days

ONLINE DEMO

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 和您的相关数据。
原文