标签: java-time

我怎么能模拟java.time.LocalDate.now()

在我的测试用例中,我需要测试时间敏感的方法,在那个方法中我们使用的是java 8类的LocalDate,它不是Joda.

当我正在进行测试时,我可以做些什么来改变时间

java systemtime java-8 java-time

38
推荐指数
3
解决办法
2万
查看次数

如何将ZonedDateTime格式化为String?

我想将a转换ZonedDateTime为a String格式("dd/MM/yyyy - hh:mm").我知道这在Joda-Time其他类型中是可能的,只是使用它们toString("dd/MM/yyyy - hh:mm")....但这不起作用ZonedDateTime.toString().

如何格式化ZonedDateTimeString


编辑:

我试图在另一个时区打印时间,结果似乎总是一样:

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)

java string java-time zoneddatetime

38
推荐指数
1
解决办法
4万
查看次数

在Java 8/jsr310中格式化持续时间

我正在将项目从Joda-Time过渡到java8的本地时间库,我遇到了麻烦.我一直无法找到持续时间的格式化程序.我想有一个自定义字符串格式,例如,HHH + MM,其中75小时和15分钟的持续时间格式为"75 + 15".通过转换为句点并使用PeriodFormatter,使用Joda-Time很容易,但我无法在Java8中找到这种类型的类.我错过了什么吗?

java duration jodatime java-8 java-time

37
推荐指数
4
解决办法
3万
查看次数

年代和周年之间的差异?

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)

java datetime date java-8 java-time

37
推荐指数
3
解决办法
7792
查看次数

如何在Spring中使用LocalDateTime RequestParam?我得到"无法将字符串转换为LocalDateTime"

我使用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)

spring-mvc spring-boot java-time

37
推荐指数
5
解决办法
4万
查看次数

无法使用Java 8中的DateTimeFormatter和ZonedDateTime从TemporalAccessor获取ZonedDateTime

我最近搬到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)

就我而言:

  • fecha是'15/06/2014'
  • ConstantesFechas.FORMATO_DIA是'dd/MM/yyyy'
  • obtenerZonaHorariaServidor返回ZoneId.systemDefault()

所以,这是一个简单的例子.但是,解析会抛出此异常:

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的不同组合,但到目前为止没有任何运气.

最好的祝福

java timezone datetime-format java-8 java-time

36
推荐指数
4
解决办法
3万
查看次数

java.time无法解析小数部分吗?

随着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 java-time

35
推荐指数
1
解决办法
1万
查看次数

将TimeUnit转换为ChronoUnit?

Java 8引入了ChronoUnit,它大致相当于Java 5中引入的TimeUnit.

是否存在将TimeUnit转换为ChronoUnit的现有功能?(是的,我知道怎么写自己的)

java java-time

35
推荐指数
2
解决办法
5549
查看次数

Java 8将给定的时间和时区转换为UTC时间

我有一个字符串类型就像一个计时:"2015-01-05 17:00"ZoneId"Australia/Sydney".

如何使用Java 8 datetime API将此时间信息转换为对应的UTC时间?

还需要考虑DST的东西.

java utc java-8 java-time

34
推荐指数
2
解决办法
6万
查看次数

将java.util.Date转换为"java.time"类型?

我有一个java.util.Date对象或一个java.util.Calendar对象.如何在java.time框架中将其转换为正确的类型?

我听说我们现在应该使用java.time类型来处理大部分业务逻辑.使用尚未针对java.time更新的旧代码时,我需要能够来回转换.什么类型映射到java.util.Datejava.util.Calendar

java datetime date java-time

34
推荐指数
1
解决办法
1万
查看次数