我正在为我的Android项目使用ThreeTen-Backport库(因为java.time尚未在android开发中实现).
当我写LocalDate today=LocalDate.now();或LocalTime time=LocalTime.now();我得到以下异常时:
Caused by: org.threeten.bp.zone.ZoneRulesException:
No time-zone data files registered
at org.threeten.bp.zone.ZoneRulesProvider.getProvider(ZoneRulesProvider.java:176)
at org.threeten.bp.zone.ZoneRulesProvider.getRules(ZoneRulesProvider.java:133)
at org.threeten.bp.ZoneRegion.ofId(ZoneRegion.java:143)
at org.threeten.bp.ZoneId.of(ZoneId.java:357)
at org.threeten.bp.ZoneId.of(ZoneId.java:285)
at org.threeten.bp.ZoneId.systemDefault(ZoneId.java:244)
at org.threeten.bp.Clock.systemDefaultZone(Clock.java:137)
at org.threeten.bp.LocalDate.now(LocalDate.java:165)
Run Code Online (Sandbox Code Playgroud)
相同的代码行在我使用的另一个java项目中运行良好,该项目使用本机java.time库.
我搜索了一个可能的解决方案,但找不到任何有用的东西:一个解决方案建议我需要使用另一个包含时区规则的jar,另一个建议在类路径中可能有两个或更多的ThreeTenBP库.
那些案件与我的案子不符.
在build.gradle文件内部的依赖项部分,我尝试了一些配置:
compile 'com.jakewharton.threetenabp:threetenabp:1.0.3'compile 'org.threeten:threetenbp:1.0.3'compile 'org.threeten:threetenbp:1.3.1'compile 'org.threeten:threetenbp:1.3.2'我不知道这行代码有什么问题以及如何修复它.
该LocalDate.now()和LocalTime.now()方法应该工作而没有指定时区.
我正在寻找一个计算两个日期之间的月份的解决方案.我认为joda或java8时间可以做到.但是当我比较它们时,我发现了一些非常奇怪的东西.
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import org.joda.time.Months;
public class DateMain {
public static void main(String[] args) throws ParseException {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
Date d1 = simpleDateFormat.parse("2017-01-28 00:00:00.000");
Date d2 = simpleDateFormat.parse("2017-02-28 00:00:00.000");
System.out.println("Test Cast 1");
System.out.println("joda time api result: " + monthsBetweenJoda(d1, d2) + " month");
System.out.println("java8 time api result: " + monthsBetweenJava8(d1, d2) + " month");
Date dd1 = simpleDateFormat.parse("2017-01-29 00:00:00.000");
Date dd2 = simpleDateFormat.parse("2017-02-28 00:00:00.000");
System.out.println("Test Cast …Run Code Online (Sandbox Code Playgroud) 如何javax.time.Instant在本地时区中将a 格式化为字符串?以下内容将本地转换Instant为UTC,而不是像我期望的那样转换为本地时区.删除呼叫toLocalDateTime()也是如此.我怎样才能获得当地时间?
public String getDateTimeString( final Instant instant )
{
checkNotNull( instant );
DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder();
DateTimeFormatter formatter = builder.appendPattern( "yyyyMMddHHmmss" ).toFormatter();
return formatter.print( ZonedDateTime.ofInstant( instant, TimeZone.UTC ).toLocalDateTime() );
}
Run Code Online (Sandbox Code Playgroud)
我遇到了https://blog.joda.org/2009/11/why-jsr-310-isn-joda-time_4941.html.
1)我目前正在将Java Calendar迁移到joda-time.我想知道,我应该使用threeten而不是joda-time吗?三生产准备好了吗?
2)三个库和joda-time库可以在同一个应用程序中一起存在吗?因为我正在使用一些使用joda-time库的第三方库.
3)joda-time会不会成为一个放弃项目,因为有三个?
jsr-310有一个方便的类DateTimeFormatters,允许你构建一个DateTimeFormatter.我特别喜欢这个pattern(String)方法 - 请参阅javadoc
但是,我遇到了一个问题,即这是区分大小写的 - 例如
DateTimeFormatters.pattern("dd-MMM-yyyy");
Run Code Online (Sandbox Code Playgroud)
与"01-Jan-2012"匹配,但不与"01-JAN-2012"或"01-jan-2012"匹配.
一种方法是断开字符串并解析组件,或者另一种方法是使用Regex用区分大小写的字符串替换不区分大小写的字符串.
但感觉应该有一个更简单的方法......
我正在使用描述Jackson数据类型JSR310页面的库,但我仍然难以让它工作.
我配置了以下bean:
@Bean
@Primary
public ObjectMapper objectMapper() {
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JSR310Module());
return mapper;
}
Run Code Online (Sandbox Code Playgroud)
当我调用我的REST API时,日期格式输出是yyyy-MM-dd'T'HH:ss.SSSSSS,例如2015-04-11T00:10:38.905847.这可以通过我的AngularJS代码处理得很好.
当我想向REST API提交内容时,日期会发布为yyyy-MM-dd'T'HH:mm:ss.SSS'Z',例如2015-04-09T08:30:00.000Z
杰克逊最后一直在抱怨'Z'.如果我查看LocalDateTimeDeserializer它在文档中使用的DateTimeFormatter.ISO_LOCAL_DATE_TIMEboils ISO_LOCAL_DATE'T'ISO_LOCAL_TIME,它提到它没有覆盖区域.
所以我想我应该设置DateFormat在ObjectMapper我创建:
@Bean
@Primary
public ObjectMapper objectMapper() {
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JSR310Module());
mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"));
return mapper;
}
Run Code Online (Sandbox Code Playgroud)
但这没有任何作用.我将其更改为简单的类似yyyy-MM-dd但序列化日期保留为以前的格式,反序列化也不受影响.
我在这做错了什么才能让它发挥作用?据我所知,我的JavaScript代码中的日期格式是ISO 8601格式...
jOOQ是否支持JSR310与PostgreSQL结合使用?特别是,我试图使用以下类:
java.time.Instantjava.time.LocalDatejava.time.LocalTimejava.time.LocalDateTime我存储在以下数据类型中(根据http://www.postgresql.org/docs/9.1/static/datatype-datetime.html):
java.time.Instant: timestamp with timezonejava.time.LocalDate: datejava.time.LocalTime: time without timezonejava.time.LocalDateTime: timestamp without timezone这些数据类型是否正确?
是否jOOQ支持转换之间java.sql.Timestamp,java.sql.Date以及java.sql.Time和四大类以上(双向)?
我已经添加了 JavaTimeModule,但仍然无法让 OffsetDateTime 从 RethinkDB @Configuration public class JacksonOffsetDateTimeMapper{ 获取一些数据
@Bean
@Primary
public ObjectMapper objectMapper(Jackson2ObjectMapperBuilder builder) {
return builder.build()
.registerModule(new JavaTimeModule());
}
Run Code Online (Sandbox Code Playgroud)
}
Jackson 的 Gradle 依赖项:实现组:'com.fasterxml.jackson.core',名称:'jackson-databind',版本:'2.13.1'
实现 'com.fasterxml.jackson.datatype:jackson-datatype-jsr310'
实现组:'com.fasterxml.jackson.module',名称:'jackson-module-parameter-names',版本:'2.13.1'
我正在尝试编组包含ISO格式的时间戳的响应,如下所示:
{
...
"time" : "2014-07-02T04:00:00.000000Z"
...
}
Run Code Online (Sandbox Code Playgroud)
进入ZonedDateTime我的域模型对象中的字段.最终,如果我使用在以下片段中注释的解决方案,它会起作用.在SO上有许多类似的问题,但我想得到一个具体的答案,另一种使用的方法JacksonJsonProvider有ObjectMapper + JavaTimeModule什么问题?
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JavaTimeModule());
JacksonJsonProvider provider = new JacksonJsonProvider(mapper);
Client client = ClientBuilder.newBuilder()
// .register(new ObjectMapperContextResolver(){
// @Override
// public ObjectMapper getContext(Class<?> type) {
// ObjectMapper mapper = new ObjectMapper();
// mapper.registerModule(new JavaTimeModule());
// return mapper;
// }
// })
.register(provider)
.register(JacksonFeature.class)
.build();
Run Code Online (Sandbox Code Playgroud)
我得到的错误:
javax.ws.rs.client.ResponseProcessingException: com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of java.time.ZonedDateTime: no String-argument constructor/factory method to deserialize from String …Run Code Online (Sandbox Code Playgroud) 关于时间依赖单元测试的问题,我有一个问题
假设我构建了包含服务接口及其实现的Spring应用程序
如果我想在测试中更改时钟,我将不得不"污染"生产代码和接口,例如setClock方法如下:
public interface MyService {
void heavyBusinessLogic();
void setClock(Clock clock);
}
@Service
public class MyServiceImpl implements MyService {
private Clock clock = Clock.systemDefaultZone();
@Override
public void heavyBusinessLogic() {
if (LocalDate.now(clock)...) {
...
}
}
@Override
public void setClock(Clock clock) {
this.clock = clock;
}
}
Run Code Online (Sandbox Code Playgroud)
在测试中,我可以调用,例如:
service.setClock(Clock.fixed(Instant.parse("2017-02-14T01:22:00Z"), ZoneOffset.UTC));
Run Code Online (Sandbox Code Playgroud)
如何在Spring中消除这种跨领域的关注?
我想坚持使用java.time.Clock(我不想使用Joda)