我想将joda设置DateTime
为今天凌晨2点(参见下面的示例代码).但我得到了这个例外:
Exception in thread "main" org.joda.time.IllegalFieldValueException: Value 2 for hourOfDay is not supported: Illegal instant due to time zone offset transition: 2011-03-27T02:52:05.239 (Europe/Prague)
at org.joda.time.chrono.ZonedChronology$ZonedDateTimeField.set(ZonedChronology.java:469)
at org.joda.time.MutableDateTime.setHourOfDay(MutableDateTime.java:702)
Run Code Online (Sandbox Code Playgroud)
上面处理异常的正确方法是什么,或者DateTime
在一天中的特定时刻创建一个?
示例代码:
MutableDateTime now = new MutableDateTime();
now.setHourOfDay(2);
now.setMinuteOfHour(0);
now.setSecondOfMinute(0);
now.setMillisOfSecond(0);
DateTime myDate = now.toDateTime();
Run Code Online (Sandbox Code Playgroud)
谢谢.
假设有两个日期A(开始时间)和B(结束时间).A&B可以是同一天甚至是不同日子的时间.我的任务是以秒为单位显示差异.我正在使用的日期格式是
Date Format :: "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
Run Code Online (Sandbox Code Playgroud)
例如
start date :: "2011-11-16T14:09:23.000+00:00"
end date :: "2011-11-17T05:09:23.000+00:00"
Run Code Online (Sandbox Code Playgroud)
感谢帮助.
我在我的游戏应用程序中使用Jodatime,但目前不必做来回转换自/至一束java.util.Date
和java.sql.Time
.
由于jodatime包含在Play发行版中,我认为可能有更好的方法来实现这一点.有没有什么办法可以让我的模型字段DateTime
s,而不是java.util.Date
和java.sql.Time
这样的转换是自动完成的?还有另一种简化方法吗?
我之前从未使用过JodaTime,但回答了这个问题,如何在一个月内获得序数平日.
我尝试了它并想出了这个丑陋的代码来取消下面的所有字段:
DateTime startOfMonth =
input.withDayOfMonth(1)
.withHourOfDay(0) // there
.withMinuteOfHour(0) // has got to
.withSecondOfMinute(0) // be a shorter way
.withMillisOfSecond(0); // to do this
Run Code Online (Sandbox Code Playgroud)
Date startOfMonth = DateUtils.truncate(input, Calendar.MONTH);
Run Code Online (Sandbox Code Playgroud)
在JodaTime中,首选的成语是什么?
您将如何重写下面的方法,该方法将返回下个月的第一天,org.joda.time
包装在Joda-Time中?
public static Date firstDayOfNextMonth() {
Calendar nowCal = Calendar.getInstance();
int month = nowCal.get(Calendar.MONTH) + 1;
int year = nowCal.get(Calendar.YEAR);
Calendar cal = Calendar.getInstance();
cal.clear();
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, month);
cal.set(Calendar.DAY_OF_MONTH, 1);
Date dueDate = new Date(cal.getTimeInMillis());
return dueDate;
}
Run Code Online (Sandbox Code Playgroud) 在Joda-Time,有没有办法获得一周的第一天(星期一)的日期.
例如,我想根据今天的当前日期21/01/11找出这个星期一星期一的日期
提前干杯.
编辑:我也希望找到一周结束的日期,即星期日的日期.干杯
LocalDate#toDateMidnight
读取的javdoc 如下:
从v1.5开始,建议您避免使用DateMidnight并使用toDateTimeAtStartOfDay(),因为下面有详细说明.
如果默认时区在午夜切换到夏令时,则此方法将引发异常,此LocalDate表示该切换日期.问题是在所需的日期没有午夜这样的时间,因此抛出了这样的例外.
在某些时区中不存在午夜这一事实似乎足以避免DateMidnight
完全使用(假设您的代码没有使用已知不具有此DST情况的固定时区,并且永远不需要在其中使用不同的时区未来).
但是,DateMidnight
不会弃用,并且DateMidnight
类本身的javadoc中没有类似的建议或警告.此外,DateMidnight
构造函数愉快地接受即时和时区,使得在给定的一天不存在午夜,而不是投掷IllegalArgumentException
类似的LocalDate#toDateMidnight
.结果DateMidnight
表现为DateTime
在一天开始时的时间.
当某个特定日期不存在午夜时,为什么LocalDate#toDateMidnight
在DateMidnight
构造函数没有的情况下抛出异常?推荐的用例是DateMidnight
什么?
如果我可以使用Java 8 Date and Time API(java.time
),有没有理由使用Joda Time ?
我应该每次都使用Java 8日期和时间吗?
我知道这不是"它应该工作的方式",但仍然是:如果你有两个DateTime对象,那么减去它们的好方法是什么?将它们转换为Date对象?
DateTime start = new DateTime();
System.out.println(start + " - doing some stuff");
// do stuff
DateTime end = new DateTime();
Period diff = // end - start ???
System.out.println(end + " - doing some stuff took diff seconds");
Run Code Online (Sandbox Code Playgroud)