我创建了一个采用两个日期(以毫秒为单位)的方法,并返回一个表示这两个日期之间的持续时间的句子.
目前,我有这个代码:
public static String formatDuration(long start, long end) {
Interval interval = new Interval(start, end);
return getPeriodFormatter().print(interval.toPeriod()).trim();
}
private static PeriodFormatter getPeriodFormatter() {
PeriodFormatter pf = new PeriodFormatterBuilder().printZeroRarelyFirst()
.appendYears().appendSuffix("y ", "y ")
.appendMonths().appendSuffix("m" , "m ")
.appendDays().appendSuffix("d ", "d ")
.appendHours().appendSuffix("h ", "h ")
.appendMinutes().appendSuffix("m ", "m ")
.appendSeconds().appendSuffix("s ", "s ")
.toFormatter();
return pf;
}
Run Code Online (Sandbox Code Playgroud)
但是,我想我误解了我必须在JodaTime中定义间隔的方式.如果我尝试以下测试代码:
@Test
public void foobar() {
try {
TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss dd/MM/yyyy");
long start = sdf.parse("10:30:00 24/06/2009").getTime();
long end = …Run Code Online (Sandbox Code Playgroud) 如果这是一个愚蠢的问题我很抱歉,但我找不到任何有关此问题的信息.
我想在我的应用程序视图中使用JSP标记(而不是播放'标记') - 特别是用于格式化的Joda Time JSP标记.
我无法弄清楚如何调用这些标签 - 在JSP中我只需要导入taglib然后关闭.
<%@taglib prefix="joda" uri="http://www.joda.org/joda/time/tags" %>
Run Code Online (Sandbox Code Playgroud)
我是否遗漏了一些简单的内容 - 如何在Play视图定义中执行此操作?目前我的taglib调用呈现为HTML.我知道Play的视图内容是基于Groovy的 - 我也试过在那里找到相关信息但是没有成功.
谢谢.
编辑对于它的价值,我开始怀疑我在这里误解了这种情况 - 我是否认为Play模板引擎与JSP 无关并且是一种替代而不是扩展?
有没有办法将Time和Date变量转换为DateTime?
我有一个介于两个DateTime变量之间的句点,对于那个句点中的每个日期,我想在该日期中存储一个句点,该日期具有开始DateTime和结束日期时间,因此一天可以有一个由DateTime定义的多个句点.
似乎无法弄清楚如何将日期和时间组合到DateTime.
提前致谢!
当我尝试解析这样的字符串日期时,我在Joda-Time中得到了一个错误的日期:
2013-11-20 18:20:00 +01:00
我期待获得以下日期:
Wed Nov 20 19:20:00 CET 2013
但我得到:
Wed Nov 20 18:20:00 CET 2013
我正在使用Joda-Time,这是我的代码:
String dateString = "2013-11-20 18:20:00 +01:00";
DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss Z");
DateTime temp = formatter.parseDateTime(dateString);
Date date = temp.toDate();
Run Code Online (Sandbox Code Playgroud) 我正在尝试计算两个joda日期之间的小时差异.
val d1 = getDateTime1()
val d2 = getDateTime2()
Run Code Online (Sandbox Code Playgroud)
这是我做的:
# attemp1
val hours = Hours.hoursBetween(d1, d2) // error - types mismatch
# attempt2
val p = Period(d1, d2, PeriodType.hours()) // error - there is such a constructor
p.getHours
Run Code Online (Sandbox Code Playgroud)
那我该怎么做?
给定UTC偏移,即-5,我可以使用Joda Time确定美国时区吗?太平洋,山地,中部,复活节之一就足够了.我不能使用Java 8.
我正在使用转换Grails Joda-Time插件到JavaTime.
我有这样的旧Joda时间码:
def style
switch (type) {
case LocalTime:
style = '-S'
break
case LocalDate:
style = 'S-'
break
default:
style = 'SS'
}
Locale locale = LocaleContextHolder.locale
return DateTimeFormatter.ofPattern(style, locale).withResolverStyle(ResolverStyle.LENIENT)
Run Code Online (Sandbox Code Playgroud)
我怎样才能将它转换为JSR 310?我找不到任何类似于接受样式的方法forStyle(String style).
UPD 我找到了解决方法:
Locale locale = LocaleContextHolder.locale
DateTimeFormatter formatter
switch (type) {
case LocalTime:
formatter = DateTimeFormatter.ofLocalizedTime(FormatStyle.SHORT).withLocale(locale)
break
case LocalDate:
formatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT).withLocale(locale)
break
default:
formatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT).withLocale(locale)
}
return formatter
Run Code Online (Sandbox Code Playgroud)
但它失败的Instant类型.Spock规范重现:
def 'Instant locale formatting'() {
given:
Instant inst …Run Code Online (Sandbox Code Playgroud) 我似乎是一个非常愚蠢的问题,因为Joda-Time易于使用,没有关于如何在其用户指南中使用它的解释(我在网上找不到有用的答案).
我尝试使用Joda-Time的方法,它可以计算两个日期之间的持续时间.我必须将当前日期与其他日期进行比较.
我不能使用,LocalDateTime因为该类不存在(所以我创建它),但我看到"持续时间"需要ReadableDuration在类中实现,但这个接口也不存在...
我试着写:
DateTime dt = new DateTime(juDate);
Run Code Online (Sandbox Code Playgroud)
和
public class LocalDateTime implements ReadableDuration{
public LocalDateTime() {
}
}
Run Code Online (Sandbox Code Playgroud)
我依靠Maven安装了Joda-Time ,我有2.9.1版本.
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.9.1</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
你能解释一下新手怎么办吗?
我有以下格式的时间字符串:
2016-01-07T08:00:00+00:00
Run Code Online (Sandbox Code Playgroud)
当我尝试使用以下方法解析字符串时.
public static DateTime getDateTimeObject(String dateTime) {
//DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern(PATTERN);
//DateTime dateTimeObj = dateTimeFormatter.parseDateTime(dateTime);
Logger.d(dateTime);
DateTime dateTimeObj = null;
try {
dateTimeObj = ISODateTimeFormat.dateTime().parseDateTime(dateTime);
return dateTimeObj;
} catch (Exception e) {
Logger.e(e.getMessage());
}
return dateTimeObj;
}
Run Code Online (Sandbox Code Playgroud)
我总是得到以下异常.
Invalid format: "2016-01-07T08:00:00+00:00" is malformed at "+00:00"
Run Code Online (Sandbox Code Playgroud)
如何解析ISO格式的字符串以获取有效的DateTime对象?
我在一个使用库的项目上工作,这对我来说是很新的,尽管我在其他项目中使用它也没有任何问题。
org.joda.time.DateTime
因此,我与Scala一起工作,并在Databricks上作为项目来运行该项目。
scalaVersion:=“ 2.11.12”
异常来自的代码-根据我到目前为止的调查^^-如下:
var lastEndTime = config.getState("some parameters")
val timespanStart: Long = lastEndTime // last query ending time
var timespanEnd: Long = (System.currentTimeMillis / 1000) - (60*840) // 14 hours ago
val start = new DateTime(timespanStart * 1000)
val end = new DateTime(timespanEnd * 1000)
val date = DateTime.now()
Run Code Online (Sandbox Code Playgroud)
其中,getState()函数返回1483228800作为Long类型值。
编辑:我在建立数据框时使用开始和结束日期进行过滤。我将列(时间跨度类型)与这些值进行比较!
val df2= df
.where(col("column_name").isNotNull)
.where(col("column_name") > start &&
col("column_name") <= end)
Run Code Online (Sandbox Code Playgroud)
我得到的错误:
错误从用户代码中抛出:java.lang.RuntimeException:不支持的文字类型类org.joda.time.DateTime 2017-01-01T00:00:00.000Z
我不确定我是否真正理解这是怎么回事,为什么会出错,所以每种帮助都是值得欢迎的!!提前非常感谢您!!