2012-11-26 67 views
3

我在开发过程中卡住了, 我有这样一种方式,我需要找到两个日期之间即延迟要求.. currentdate-date from database将毫秒转换为适当的java日期格式?

,我需要显示的dd:hh:mm格式延迟。在提到很多参考资料后,我发现如何转换为单位毫秒小时和分钟,但是我期望的结果是:如果结果是一些X毫秒,我需要以恰当的日期分秒数显示它

例如:2天:03分钟:46秒

这里是代码我使用:

Calendar calendar1 = Calendar.getInstance(); 
Calendar calendar2 = Calendar.getInstance(); 
calendar1.setTime(date); 
calendar2.setTime(date1); 
long milliseconds1 = calendar1.getTimeInMillis(); 
long milliseconds2 = calendar2.getTimeInMillis(); 
long diff = milliseconds1 - milliseconds2; 
System.out.println("diff ::"+diff); 
long diffSeconds = diff/1000; 
long diffMinutes = diff/(60 * 1000); 
long diffHours = diff/(60 * 60 * 1000); 
long diffDays = diff/(24 * 60 * 60 * 1000); 

任何人都可以请建议我该怎么做进一步? 请指导我..

+0

在该代码中,假设每天有24小时,每小时60分钟,每分钟有60秒。这在夏时制的国家以及闰秒行星中都不适用。你应该使用像Joda Time这样的日历框架,让它完成繁重的工作。 – wallenborn

+0

@wallenborn,是啊现在在乔达时间做背景工作,你可以请帮我如何使用它 – Esh

回答

3

您需要先计算diffDays

diffDays = diff/(24 * 60 * 60 * 1000); 

然后计算剩余毫秒:

diff -= diffDays * 24 * 60 * 60 * 1000; 

使用新的diff计算diffHours等等...

一个建议:使用像这样的常数值:

private static final int SECOND = 1000; 
private static final int MINUTE = 60 * SECOND; 
// and so on 
+2

+1简单的任务,简单的解决方案。 – Frank

+0

丹可以请你详细解释一下 – Esh

+0

这个假设每天都有24小时,如果你住在夏令时的地区,每年都会失败两次。 – wallenborn

8

使用Joda Time。 Java API不包含处理“两个日期/时间值之间的差异”的任何内容。在乔达时间,你可以选择Period(这实际上是以日历为中心的差异)和Duration(这实际上只是一个经过的时间差)。

理想情况下,使用乔达时间全部您的日期/时间问题 - 这是一个更好,更好的API。

在这种情况下,我怀疑你已经逻辑得到了Duration,但你会想要将它转换为Period,以使用PeriodFormatter进行字符串转换。

+0

可以请你提供一些例子乔达时间 – Esh

+0

@Esh:那么,你试过什么? –

+0

到目前为止,我试图获得单独的小时和分钟,现在我卡在年表概念,我想设置年表'000',但它采取默认的ISOChronology [亚洲/加尔各答],不知道如何覆盖它 – Esh

0
Calendar calendar1 = Calendar.getInstance(); 
Calendar calendar2 = Calendar.getInstance(); 
calendar1.setTime(date); 
calendar2.setTime(date1); 

int dayDiff = calendar1.get(Calendar.DAY_OF_MONTH) - calendar2.get(Calendar.DAY_OF_MONTH); 
int hourDiff = calendar1.get(Calendar.HOUR_OF_DAY) -calendar2.get(Calendar.HOUR_OF_DAY); 
int dayDiff = calendar1.get(Calendar.SECOND) - calendar2.get(Calendar.SECOND); 
1

-我会建议使用Joda图书馆当你处理任何日期时间的问题。

约达时间具有时间间隔的一个概念:

Interval interval = new Interval(oldTime, new Instant());

顺便说,约达有两个概念:间隔用于表示两个时刻之间的时间间隔(代表上午08点之间的时间上午10点),以及表示时间长度没有实际时间界限的持续时间(例如,代表两个小时!)

1

随着JodaTime,这非常简单。请参阅以下代码:

public void testJoda() { 
    DateTime then = new DateTime("2012-03-23T02:30:00.000"); 
    DateTime now = new DateTime("2012-03-24T02:29:00.000"); 

    DurationFieldType[] ddhhmm = { DurationFieldType.days(), 
      DurationFieldType.hours(), DurationFieldType.minutes() }; 
    Period p = new Period(then, now, PeriodType.forFields(ddhhmm)); 
    System.out.println(p.toString()); 

    p = new Period(then, now, PeriodType.days()); 
    System.out.println(p.toString()); 

    p = new Period(then, now, PeriodType.hours()); 
    System.out.println(p.toString()); 

    DurationFieldType[] hhmm = { DurationFieldType.hours(), DurationFieldType.minutes() }; 
    p = new Period(then, now, PeriodType.forFields(hhmm)); 
    System.out.println(p.toString()); 
} 

其中测试23h59m的区间。如预期其输出:

PT23H59M 
P0D 
PT23H 
PT23H59M 

以相同的时间间隔,一天过去了,就在3月25日春季DST跳:

DateTime then = new DateTime("2012-03-24T02:30:00.000"); 
DateTime now = new DateTime("2012-03-25T03:29:00.000"); 

和你正确地得到:

P1DT-1M 
P1D 
PT23H 
PT23H59M 

真的没有那么容易。