我在Java中使用Joda-Time库,并将日期和时间存储为org.joda.time.DateTime对象.
如何可靠地将此DateTime对象转换为将由SQL服务器(包括时区)正确解析的String,以便我可以在INSERT SQL语句中使用它?
我在我的android项目中使用了Joda time API,当我导出已签名的apk时,我的项目没有编译,它显示错误"Proguard返回错误代码1.请参阅控制台",下面是错误日志,
[2013-07-20 00:50:25 - CC] Proguard returned with error code 1. See console
[2013-07-20 00:50:25 - CC] Warning: org.joda.time.DateMidnight: can't find referenced class org.joda.convert.FromString
[2013-07-20 00:50:25 - CC] Warning: org.joda.time.DateTime: can't find referenced class org.joda.convert.FromString
[2013-07-20 00:50:25 - CC] Warning: org.joda.time.DateTimeZone: can't find referenced class org.joda.convert.FromString
[2013-07-20 00:50:25 - CC] Warning: org.joda.time.DateTimeZone: can't find referenced class org.joda.convert.ToString
[2013-07-20 00:50:25 - CC] Warning: org.joda.time.Days: can't find referenced class org.joda.convert.FromString
[2013-07-20 00:50:25 - CC] Warning: org.joda.time.Days: can't find referenced class …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用Joda-Time - 2.1和Hibernate 4.2.0.CR1.我在一些实体类中有一些日期时间字段,比如
@Column(name = "DISCOUNT_START_DATE")
@Type(type="org.joda.time.contrib.hibernate.PersistentDateTime")
private DateTime discountStartDate;
Run Code Online (Sandbox Code Playgroud)
注释Type是一种类型org.hibernate.annotations.Type.我有这个以下例外.
java.lang.AbstractMethodError: org.joda.time.contrib.hibernate.PersistentDateTime.nullSafeGet(Ljava/sql/ResultSet;[Ljava/lang/String;Lorg/hibernate/engine/spi/SessionImplementor;Ljava/lang/Object;)Ljava/lang/Object;
at org.hibernate.type.CustomType.nullSafeGet(CustomType.java:127)
at org.hibernate.type.AbstractType.hydrate(AbstractType.java:106)
at org.hibernate.persister.entity.AbstractEntityPersister.hydrate(AbstractEntityPersister.java:2903)
at org.hibernate.loader.Loader.loadFromResultSet(Loader.java:1668)
at org.hibernate.loader.Loader.instanceNotYetLoaded(Loader.java:1600)
at org.hibernate.loader.Loader.getRow(Loader.java:1500)
at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:712)
at org.hibernate.loader.Loader.processResultSet(Loader.java:940)
at org.hibernate.loader.Loader.doQuery(Loader.java:910)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:341)
at org.hibernate.loader.Loader.doList(Loader.java:2516)
at org.hibernate.loader.Loader.doList(Loader.java:2502)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2332)
at org.hibernate.loader.Loader.list(Loader.java:2327)
at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:490)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:355)
at org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:195)
at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1246)
at org.hibernate.internal.QueryImpl.list(QueryImpl.java:101)
at dao.DiscountDAO.getList(DiscountDAO.java:84)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
at org.springframework.security.access.intercept.aopalliance.MethodSecurityInterceptor.invoke(MethodSecurityInterceptor.java:64)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at …Run Code Online (Sandbox Code Playgroud) 我正在构建一个使用时间的应用程序.我决定使用Joda时间.我正在尝试将joda时间添加到我的应用程序中.我没有将.jar文件添加到库中的经验.我按照这些步骤(见底部),或者我想.我的项目没有显示任何错误,但是当我运行一个简单的测试时:DateTime test = new DateTime();我收到一个强制关闭并出现以下错误:
Could not find class 'org.joda.time.DateTime', referenced from method xxxx
java.lang.NoClassDefFoundError: org.joda.time.DateTime
Run Code Online (Sandbox Code Playgroud)
这些是我遵循的步骤:
New -> Folder -> libs. 注意:我没有这样做,因为我已经有了一个名为的文件夹libsjoda-time-2.1.jar到新创建的libs文件夹中.Properties -> Java Build Path -> Libraries -> Add Jars -> joda-time-2.1.jar.现在您可以使用以下代码进行测试:
DateTime test = new DateTime();
Run Code Online (Sandbox Code Playgroud)
导入后:
import org.joda.time.DateTime;
Run Code Online (Sandbox Code Playgroud) 我有一个org.joda.time.DateTime对象,我需要检索军事时区缩写(例如T代表UTC-07:00,U代表UTC-08:00,Z代表UTC±00:00).
见http://en.wikipedia.org/wiki/List_of_military_time_zones
这是我目前正在做的事情:
int offset = dt.getZone().toTimeZone().getRawOffset() / (60 * 60 * 1000);
String timeZoneCode = timeZoneCodeMap.get(offset);
Run Code Online (Sandbox Code Playgroud)
where timeZoneCodeMap是使用HashMap<Integer, String>如下条目初始化的
timeZoneCodeMap.put(1, "A");
timeZoneCodeMap.put(2, "B");
timeZoneCodeMap.put(3, "C");
...
timeZoneCodeMap.put(-10, "W");
timeZoneCodeMap.put(-11, "X");
timeZoneCodeMap.put(-12, "Y");
timeZoneCodeMap.put(0, "Z");
Run Code Online (Sandbox Code Playgroud)
是否存在已包含时区到军事缩写的映射的函数或库(在Joda或其他方面)?
如果有更好的方法来计算偏移量,请随时告诉我.
为了测试与时间相关的代码,最好使用虚拟时钟模式
我们的想法是,我们不使用当前时间new Date,而是从可以用虚拟时钟模拟的时钟返回预定义的固定时间.
现在在Java中我们有JodaTime和DateTimeclass,它允许设置采样时间
DateTimeUtils.setCurrentMillisFixed(today.getMillis());
Run Code Online (Sandbox Code Playgroud)
并将固定时间重置为系统时间:
DateTimeUtils.setCurrentMillisSystem();
Run Code Online (Sandbox Code Playgroud)
现在的问题!
如果它在运行测试时全局设置全局上下文中的固定时间,那么将此技术用于setUp和tearDown方法是多么安全.只要我得到它 - 它只会工作,只要我们没有两个并发测试,这种技术在同一环境中并行运行.
import java.text.SimpleDateFormat;
import java.util.Date;
import org.joda.time.*;
public class Test {
public static void main(String[] args) {
String dateStart = "01/01/2000 05:30";
String dateStop = "02/2/2001 06:31";
SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy HH:mm");
Date d1 = null;
Date d2 = null;
try {
d1 = format.parse(dateStart);
d2 = format.parse(dateStop);
DateTime dt1 = new DateTime(d1);
DateTime dt2 = new DateTime(d2);
System.out.print(Years.yearsBetween(dt1, dt2).getYears() + " years, ");
System.out.print(Months.monthsBetween(dt1, dt2).getMonths() % 52 + " months, ");
System.out.print(Weeks.weeksBetween(dt1, dt2).getWeeks() % 4 + " weeks, "); …Run Code Online (Sandbox Code Playgroud) 我一直在用这个概念证明来抨击我的头脑.我想使用一个REST端点,该端点返回带有ISO8601 UTC时间戳的JSON有效负载:
{ ...
"timestamp" : "2014-08-20T11:51:31.233Z"
}
Run Code Online (Sandbox Code Playgroud)
我想使用命令行Java客户端使用Jackson/Spring Boot编写的Jersey 2客户端来使用它.编组POJO定义如下:
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonInclude(Include.NON_NULL)
public class GreetingResource
{
@JsonProperty("timestamp")
private DateTime date;
...
}
Run Code Online (Sandbox Code Playgroud)
遵循以下建议:
https://jersey.java.net/documentation/latest/user-guide.html#json.jackson
并使用以下Gradle依赖项:
dependencies {
compile("org.springframework.boot:spring-boot-starter")
compile("org.springframework.boot:spring-boot-starter-logging")
compile("joda-time:joda-time")
compile("com.fasterxml.jackson.core:jackson-core")
compile("com.fasterxml.jackson.core:jackson-annotations")
compile("com.fasterxml.jackson.core:jackson-databind")
compile("com.fasterxml.jackson.datatype:jackson-datatype-joda")
compile("com.fasterxml.jackson.jaxrs:jackson-jaxrs-json-provider:2.3.3")
compile("org.apache.commons:commons-lang3:3.3.2")
compile("org.glassfish.jersey.core:jersey-client:2.2")
compile("org.glassfish.jersey.media:jersey-media-json-jackson:2.2")
testCompile("org.springframework.boot:spring-boot-starter-test")
}
Run Code Online (Sandbox Code Playgroud)
我总是得到这个错误:
Exception in thread "main" javax.ws.rs.ProcessingException: Error reading entity from input stream.
at org.glassfish.jersey.message.internal.InboundMessageContext.readEntity(InboundMessageContext.java:849)
at org.glassfish.jersey.message.internal.InboundMessageContext.readEntity(InboundMessageContext.java:768)
at org.glassfish.jersey.client.InboundJaxrsResponse.readEntity(InboundJaxrsResponse.java:96)
at org.glassfish.jersey.client.ScopedJaxrsResponse.access$001(ScopedJaxrsResponse.java:56)
at org.glassfish.jersey.client.ScopedJaxrsResponse$1.call(ScopedJaxrsResponse.java:77)
at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
at org.glassfish.jersey.internal.Errors.process(Errors.java:228)
at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:397)
at org.glassfish.jersey.client.ScopedJaxrsResponse.readEntity(ScopedJaxrsResponse.java:74)
at client.GreetingClient.processResponse(GreetingClient.java:62)
at client.GreetingClient.performGet(GreetingClient.java:53)
at …Run Code Online (Sandbox Code Playgroud) 区域名称:无法解析时区名称('z').
因此时区解析如下:
System.out.println(new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy").parse("Fri Nov 11 12:13:14 JST 2010"));
Run Code Online (Sandbox Code Playgroud)
在Joda无法做到:
DateTimeFormatter dtf = DateTimeFormat.forPattern("EEE MMM dd HH:mm:ss z yyyy");
System.out.println(dtf.parseDateTime("Fri Nov 11 12:13:14 JST 2010"));
//Exception in thread "main" java.lang.IllegalArgumentException: Invalid format: "Fri Nov 11 12:13:14 JST 2010" is malformed at "JST 2010"
//at org.joda.time.format.DateTimeFormatter.parseDateTime(DateTimeFormatter.java:673)
Run Code Online (Sandbox Code Playgroud) Joda ISODateTimeFormat文档说ISODateTimeFormat.dateTime()返回模式yyyy-MM-dd'T'HH的格式化程序:mm:ss.SSSZZ
但格式化程序返回一个"Z"代替+00:00看到这个 -
DateTime dt = DateTime.now(DateTimeZone.UTC);
DateTimeFormatter patternFormat = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZZ");
DateTimeFormatter isoFormat = ISODateTimeFormat.dateTime();
System.out.println(dt.toString(patternFormat)); //2014-06-01T03:02:13.552+00:00
System.out.println(dt.toString(isoFormat)); //2014-06-01T03:02:13.552Z
Run Code Online (Sandbox Code Playgroud)
任何人都可以告诉我将+00:00作为Z打印的模式是什么
编辑:只是为了澄清 - 我知道'Z'与+00:00相同但文字上却不同.我要问的是什么模式将Z作为时间偏移而不是+00:00
(对不起,如果这太微不足道了.我想在没有毫秒的情况下使用ISO格式,在写这个问题的过程中,我发现我在ISODateTimeFormat.dateTimeNoMillis()中找到了正确的内容,所以我现在只是为了利益而要求)
干杯
jodatime ×10
java ×7
android ×2
abbreviation ×1
apk ×1
date ×1
datetime ×1
hibernate ×1
jackson ×1
jdbc ×1
jersey ×1
junit ×1
libs ×1
proguard ×1
spring-boot ×1
spring-mvc ×1
sql-server ×1
testng ×1
time ×1
timezone ×1