在我的测试用例中,我需要测试时间敏感的方法,在那个方法中我们使用的是java 8类的LocalDate,它不是Joda.
当我正在进行测试时,我可以做些什么来改变时间
我想将a转换ZonedDateTime为a String格式("dd/MM/yyyy - hh:mm").我知道这在Joda-Time其他类型中是可能的,只是使用它们toString("dd/MM/yyyy - hh:mm")....但这不起作用ZonedDateTime.toString().
ZonedDateTime为String?编辑:
我试图在另一个时区打印时间,结果似乎总是一样:
ZonedDateTime date = ZonedDateTime.now();
ZoneId la = ZoneId.of("America/Los_Angeles");
ZonedDateTime date2 = date.of(date.toLocalDateTime(), la);
// 24/02/2017 - 04:53
System.out.println(DateTimeFormatter.ofPattern("dd/MM/yyyy - hh:mm").format(date));
// same result as the previous one
// 24/02/2017 - 04:53
System.out.println(DateTimeFormatter.ofPattern("dd/MM/yyyy - hh:mm").format(date2));
Run Code Online (Sandbox Code Playgroud)
而且我和洛杉矶不在同一时区.
编辑2:
找到了如何更改时区:
// Change this:
ZonedDateTime date2 = date.of(date.toLocalDateTime(), la); // incorrect!
// To this:
ZonedDateTime date2 = date.withZoneSameInstant(la);
Run Code Online (Sandbox Code Playgroud) 我正在将项目从Joda-Time过渡到java8的本地时间库,我遇到了麻烦.我一直无法找到持续时间的格式化程序.我想有一个自定义字符串格式,例如,HHH + MM,其中75小时和15分钟的持续时间格式为"75 + 15".通过转换为句点并使用PeriodFormatter,使用Joda-Time很容易,但我无法在Java8中找到这种类型的类.我错过了什么吗?
Java的8位的DateTimeFormatter类有一个方法,ofPattern(String pattern)即让您从一串定义的格式A-z,a-z信件.这些例子并没有明确的区别y,今年的时代和Y,以星期为一年.它是什么?
Symbol Meaning Presentation Examples
------ ------- ------------ -------
y year-of-era year 2004; 04
Y week-based-year year 1996; 96
Run Code Online (Sandbox Code Playgroud) 我使用Spring Boot并包含jackson-datatype-jsr310在Maven中:
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>2.7.3</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
当我尝试使用具有Java 8日期/时间类型的RequestParam时,
@GetMapping("/test")
public Page<User> get(
@RequestParam(value = "start", required = false)
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime start) {
//...
}
Run Code Online (Sandbox Code Playgroud)
并使用以下URL测试它:
/test?start=2016-10-8T00:00
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
{
"timestamp": 1477528408379,
"status": 400,
"error": "Bad Request",
"exception": "org.springframework.web.method.annotation.MethodArgumentTypeMismatchException",
"message": "Failed to convert value of type [java.lang.String] to required type [java.time.LocalDateTime]; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@org.springframework.web.bind.annotation.RequestParam @org.springframework.format.annotation.DateTimeFormat java.time.LocalDateTime] for value '2016-10-8T00:00'; nested exception is java.lang.IllegalArgumentException: Parse attempt failed for …Run Code Online (Sandbox Code Playgroud) 我最近搬到Java 8,希望更容易处理本地和分区时间.
但是,在我看来,在解析一个简单的日期时,我面临一个简单的问题.
public static ZonedDateTime convertirAFecha(String fecha) throws Exception {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(
ConstantesFechas.FORMATO_DIA).withZone(
obtenerZonaHorariaServidor());
ZonedDateTime resultado = ZonedDateTime.parse(fecha, formatter);
return resultado;
}
Run Code Online (Sandbox Code Playgroud)
就我而言:
所以,这是一个简单的例子.但是,解析会抛出此异常:
java.time.format.DateTimeParseException: Text '15/06/2014' could not be parsed: Unable to obtain ZonedDateTime from TemporalAccessor: {},ISO resolved to 2014-06-15 of type java.time.format.Parsed
Run Code Online (Sandbox Code Playgroud)
有小费吗?我一直在尝试解析和使用TemporalAccesor的不同组合,但到目前为止没有任何运气.
最好的祝福
随着Mac OS X(Mavericks)上的第一个Java 8(b132)发布,使用新的java.time包的代码可以工作:
String input = "20111203123456";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern( "yyyyMMddHHmmss");
LocalDateTime localDateTime = LocalDateTime.parse( input, formatter );
Run Code Online (Sandbox Code Playgroud)
渲染:
2011-12-03T12:34:56
Run Code Online (Sandbox Code Playgroud)
但是当我按照DateTimeFormatter类doc中的指定添加"SS"作为秒的小数(和"55"作为输入)时,会抛出异常:
java.time.format.DateTimeParseException: Text '2011120312345655' could not be parsed at index 0
Run Code Online (Sandbox Code Playgroud)
该文档说默认使用严格模式,并且需要与输入数字相同数量的格式字符.所以我很困惑为什么这段代码失败了:
String input = "2011120312345655";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern( "yyyyMMddHHmmssSS");
LocalDateTime localDateTime = LocalDateTime.parse( input, formatter );
Run Code Online (Sandbox Code Playgroud)
使用文档中的示例("978")的另一个示例(失败):
String input = "20111203123456978";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern( "yyyyMMddHHmmssSSS");
LocalDateTime localDateTime = LocalDateTime.parse( input, formatter );
Run Code Online (Sandbox Code Playgroud)
这个例子工作,添加一个小数点(但我发现在doc中没有这样的要求):
String input = "20111203123456.978";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern( …Run Code Online (Sandbox Code Playgroud) Java 8引入了ChronoUnit,它大致相当于Java 5中引入的TimeUnit.
是否存在将TimeUnit转换为ChronoUnit的现有功能?(是的,我知道怎么写自己的)
我有一个字符串类型就像一个计时:"2015-01-05 17:00"和ZoneId是"Australia/Sydney".
如何使用Java 8 datetime API将此时间信息转换为对应的UTC时间?
还需要考虑DST的东西.
我有一个java.util.Date对象或一个java.util.Calendar对象.如何在java.time框架中将其转换为正确的类型?
我听说我们现在应该使用java.time类型来处理大部分业务逻辑.使用尚未针对java.time更新的旧代码时,我需要能够来回转换.什么类型映射到java.util.Date或java.util.Calendar?
java-time ×10
java ×9
java-8 ×5
date ×2
datetime ×2
duration ×1
jodatime ×1
spring-boot ×1
spring-mvc ×1
string ×1
systemtime ×1
timezone ×1
utc ×1