我正在使用Joda来解析日期并使用不使用前导零的格式,例如:
Mon Nov 20 14:40:36 2006
Mon Nov 6 14:40:36 2006
Run Code Online (Sandbox Code Playgroud)
请注意,dayOfMonth字段用空白填充.
目前我似乎必须使用两种不同的格式,如果一个失败则重新分析
"EEE MMM dd HH:mm:ss yyyy"
"EEE MMM d HH:mm:ss yyyy"
Run Code Online (Sandbox Code Playgroud)
是否有单一格式(或API开关)处理这两种情况?(SimpleDateFormat的答案是一样的 - 我不使用它?)
我试图写一个简单实用的方法添加的天数aninteger的乔达时间瞬间.这是我的第一次尝试.
/**
* Adds a number of days specified to the instant in time specified.
*
* @param instant - the date to be added to
* @param numberOfDaysToAdd - the number of days to be added to the instant specified
* @return an instant that has been incremented by the number of days specified
*/
public static Instant addNumberOfDaysToInstant(final Instant instant, final int numberOfDaysToAdd) {
Days days = Days.days(numberOfDaysToAdd);
Interval interval = new Interval(instant, days);
return interval.getEnd().toInstant(); …Run Code Online (Sandbox Code Playgroud) 我想将Joda Time UTC DateTime对象转换为本地时间.
这是一种艰苦的方法,它似乎有效.但必须有更好的方法.
这是没有周围声明的代码(在Scala中):
val dtUTC = new DateTime("2010-10-28T04:00")
println("dtUTC = " + dtUTC)
val dtLocal = timestampLocal(dtUTC)
println("local = " + dtLocal)
def timestampLocal(dtUTC: DateTime): String = {
// This is a laborious way to convert from UTC to local. There must be a better way.
val instantUTC = dtUTC.getMillis
val localDateTimeZone = DateTimeZone.getDefault
val instantLocal = localDateTimeZone.convertUTCToLocal(instantUTC)
val dtLocal = new DateTime(instantLocal)
dtLocal.toString
}
Run Code Online (Sandbox Code Playgroud)
这是输出:
dtUTC = 2010-10-28T04:00:00.000 + 11:00 local = 2010-10-28T15:00:00.000 + 11:00
如何从JSR-310类中计算修改过的Julian日LocalDate?(在JDK 8中)
具体来说,这是连续天数的计算,称为"修改的朱利安日",而不是儒略历系统中的日期.
例如:
LocalDate date = LocalDate.now();
long modifiedJulianDay = ???
Run Code Online (Sandbox Code Playgroud) 在测试我的一些方法时,我遇到了一个奇怪的问题,我似乎已经能够获得我的问题的具体例子.
我使用的ja_JP_JP_#u-ca-japanese语言环境无法使用由语言环境定义的自己的日期模式来解析日期.
我想知道我做错了什么,或者这是否是一个JDK错误.
请注意,为了构造ja_JP_JP_#u-ca-japanese,您需要根据Locale javadoc的new Locale("ja", "JP", "JP")摘录使用:
特别案例
出于兼容性原因,两个不符合要求的语言环境被视为特殊情况.这些是ja_JP_JP和th_TH_TH.这些在BCP 47中是不正确的,因为变体太短.为了便于迁移到BCP 47,这些都在施工期间得到了特殊处理.这两种情况(并且只有这些)导致构造函数生成扩展,所有其他值的行为与Java 7之前完全相同.
Java使用ja_JP_JP代表日本和日本帝国日历一起使用日语.现在可以使用Unicode语言环境扩展来表示,通过指定Unicode语言环境键ca(对于"calendar")并键入japanese.当使用参数"ja","JP","JP"调用Locale构造函数时,会自动添加扩展名"u-ca-japanese".
Java使用th_TH_TH来表示泰国使用的泰语以及泰国数字.现在,使用Unicode语言环境扩展,通过指定Unicode语言环境键nu(用于"数字")和值泰语,也可以表示这一点.当使用参数"th","TH","TH"调用Locale构造函数时,会自动添加扩展名"u-nu-thai".
给出的测试用例演示了这个问题:
@Test
public void testJapaneseLocale() {
LocalDate specificLocalDate = LocalDate.of(2014, 10, 2);
Locale jpLocale = new Locale("ja", "JP", "JP");
DateTimeFormatter jpDateTimeFormatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT).withLocale(jpLocale);
String jpDate = specificLocalDate.format(jpDateTimeFormatter);
String jpPattern = DateTimeFormatterBuilder.getLocalizedDateTimePattern(FormatStyle.SHORT, null, Chronology.ofLocale(jpLocale), jpLocale);
LocalDate jpLocalDate = LocalDate.parse(jpDate, DateTimeFormatter.ofPattern(jpPattern, jpLocale));
assertEquals(specificLocalDate, jpLocalDate);
}
Run Code Online (Sandbox Code Playgroud)
此代码适用于任何其他常规语言环境,例如英语等.
我不明白为什么MutableDateTime.setDate()将时间设置为"昨天"(参见日志时间戳小时 - 它是20:28).这个时区有关系吗?我需要在格式化程序上设置一些东西吗?
我希望在使用"10/27/2010"调用setDate之后,日期将与解析日期00:00 EDT 10/27/10相同,而不是20/28 EDT 10/26/10.这是24小时前从"now".
我在这里缺少什么,或者我应该如何编辑代码以获得所需的结果?我是Joda Time的新手,想解开这个谜.
DateTimeFormatter dateFormatterJ = DateTimeFormat.forPattern("MM/dd/yyyy");
DateTimeFormatter timestampFormatJ = DateTimeFormat.forPattern("HH:mm zzz MM/dd/yy");
MutableDateTime startDate = new MutableDateTime();
log.info("parsed date " +
timestampFormatJ.print(dateFormatterJ.parseMutableDateTime(startDateString)));
startDate.setDate((dateFormatterJ.parseMutableDateTime(startDateString)));
log.info("startDate: " + timestampFormatJ.print(startDate));
Run Code Online (Sandbox Code Playgroud)
在这种情况下,startDateString只是"10/27/2010".
这是日志输出:
10-27 20:28:55 INFO parsed date: 00:00 EDT 10/27/10
10-27 20:28:55 INFO startDate: 20:28 EDT 10/26/10
Run Code Online (Sandbox Code Playgroud)
谢谢
我们有一个应用程序,其中时机至关重要.我们使用joda进行时间转换并以UTC时间存储所有数据.我们已经生产了一段时间,并且everthing一直是完美的但是......
现在我们注意到在时间变化前几个小时发生的事件已经过早转换!实际上,保存到数据库的UTC时间会减少一个小时.
这是一个例子.我的活动发生在2010年11月6日@太平洋时间晚上9点,通常会保存为11/7/2010 @ 4am.但是,由于夏令时在7日结束(大概是凌晨2点),这个时间会被移动并存储为11/7/2010 @ 5am.
我们需要将DST更改记录,直到实际发生在太平洋标准时间太平洋标准时间太平洋标准时间的PST区域.我认为joda会处理这个问题,特别是因为它被吹捧为比java的默认功能有了很大改进.
您的任何反馈都会有所帮助,特别是如果您能在明天的时间变更之前将它发送给我们!之后它将是学术性的,但仍然是一个有用的讨论.
以下是我们用于执行时区更改并将结果作为常规Java日期对象获取的一些代码.
public Date convertToTimeZone(Date dt, TimeZone from, TimeZone to){
DateTimeZone tzFrom = DateTimeZone.forTimeZone(from);
DateTimeZone tzTo = DateTimeZone.forTimeZone(to);
Date utc = new Date(tzFrom.convertLocalToUTC(dt.getTime(), false));
Date convertedTime = new Date(tzTo.convertUTCToLocal(utc.getTime()));
return convertedTime;
}
Run Code Online (Sandbox Code Playgroud)
编辑:以下评论的代码示例
public Date convert(Date dt, TimeZone from, TimeZone to) {
long fromOffset = from.getOffset(dt.getTime());
long toOffset = to.getOffset(dt.getTime());
long convertedTime = dt.getTime() - (fromOffset - toOffset);
return new Date(convertedTime);
}
Run Code Online (Sandbox Code Playgroud)
完整单元测试示例
package com.test.time;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
import …Run Code Online (Sandbox Code Playgroud) 我使用Joda库来获取自给定时间戳以来的时间段:
public static String getTimePassedSince(Date initialTimestamp){
DateTime initDT = new DateTime(initialTimestamp.getTime());
DateTime now = new DateTime();
Period p = new Period(initDT, now);
PeriodFormatter formatter = new PeriodFormatterBuilder()
.appendYears().appendSuffix(" year, ", " years, ")
.appendMonths().appendSuffix(" month, ", " months, ")
.appendDays().appendSuffix(" day, ", " days, ")
.appendHours().appendSuffix(" hour, ", " hours, ")
.appendMinutes().appendSuffix(" minute, ", " minutes, ")
.appendSeconds().appendSuffix(" second, ", " seconds")
.printZeroNever()
.toFormatter();
return formatter.print(p);
}
Run Code Online (Sandbox Code Playgroud)
该函数返回给定时间戳的确切时间段字符串.例如:
3 minutes, 23 seconds
1 hour, 30 minutes, 57 seconds
1 day, …Run Code Online (Sandbox Code Playgroud) 我正在尝试让 JodaTime taglib 在我的 Spring 3 MVC 应用程序中工作。
根据网站,我应该能够将它放在我的 jsp 页面中,它应该可以工作。
<%@taglib prefix="joda" uri="http://www.joda.org/joda/time/tags" %>
<% pageContext.setAttribute("now", new org.joda.time.DateTime()); %>
<joda:format value="${now}" style="SM" />
Run Code Online (Sandbox Code Playgroud)
但是,我收到此异常:
org.apache.jasper.JasperException: /WEB-INF/jsp/reports/../taglibs.jspf(7,66) PWC6188: The absolute uri: http://www.joda.org/joda/time/tags cannot be resolved in either web.xml or the jar files deployed with this application
Run Code Online (Sandbox Code Playgroud)
我正在运行 Glassfish 3.0.1。Servlet 2.4 版在我的 web.xml 文件中声明。JSP 2.1。JSTL 1.1。
添加:我已将 joda-time-jsptag-1.0.2.jar 添加到我的 WEB-INF/lib 目录中。我在同一个 lib 目录上也有 joda-time-1.6.1.jar。
taglib uri 是否需要有效?当我尝试在浏览器中访问http://www.joda.org/joda/time/tags 时,它会重定向到 sourceforge 页面。也许我只需要一个更新的 URI 链接?
有接班人吗?
我在我的应用程序的Java后端上大量使用Joda DateTime对象(根据SO建议).但我还没有找到一种非常一致的方式来回转JavaScript.MDN描述的Date对象似乎表明"符合IETF的RFC 1123时间戳"是一种标准格式,但我的搜索似乎没有出现在Joda库中内置的格式化程序以获取该格式的DateTime对象.
是否有一个简单的方法可以调用将DateTime对象转换为我的Web客户端可以使用的格式?它会支持IE8(就JavaScript而言)吗?
*注意:我没有使用Spring或任何自动绑定(序列化/反序列化)的东西,此时它不是一个选项.我知道我知道...