标签: java-time

Jackson Java 8 DateTime序列化

Jackson运行java.time.Instant,默认情况下启用WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS(READ_以及). 杰克逊-数据类型- jsr310

它像这样生成JSON

{ "createDate":1421261297.356000000, "modifyDate":1421261297.356000000 }

在JavaScript中,从传统的millis时间戳(而不是像上面的秒/纳米)那样获得Date要容易得多,比如new Date(1421261297356).

我认为应该有一些理由默认采用纳米方法,那么这是什么原因呢?

timestamp milliseconds java-8 jackson-modules java-time

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

在Java 8中没有获得正确的时区?

我已将系统数据时间设置为亚洲/加尔各答,它是:

Date: 2015-06-12
Time: 12:07:43.548
Run Code Online (Sandbox Code Playgroud)

现在我在Java 8中编写了以下程序

ZoneId paris = ZoneId.of("Europe/Paris");
LocalDateTime localtDateAndTime = LocalDateTime.now();
ZonedDateTime dateAndTimeInParis  = ZonedDateTime.of(localtDateAndTime, paris );
System.out.println("Current date and time in a particular timezone : " + dateAndTimeInParis  );
Run Code Online (Sandbox Code Playgroud)

当我运行代码时,它会显示巴黎时间:

Current date and time in a particular timezone :    

2015-06-12T12:07:43.548+02:00[Europe/Paris]
Run Code Online (Sandbox Code Playgroud)

上述欧洲/巴黎与亚洲/加尔各答相同.有谁能解释我做错了什么?

更新:我不喜欢使用其他Java包中的类; 因为我听说这个包java.time有足够的功能来处理最大日期时间工作,我希望这个包括:)

java java-8 java-time

2
推荐指数
1
解决办法
117
查看次数

托管Bean的托管Bean类java.time.ZonedDateTime并未声明公共的无参数构造器。

为了获得当前日期,我在faces-config.xml下面声明了一个托管bean 。

<managed-bean>
    <managed-bean-name>currentDate</managed-bean-name>
    <managed-bean-class>java.time.ZonedDateTime</managed-bean-class>
    <managed-bean-scope>request</managed-bean-scope>
</managed-bean>
Run Code Online (Sandbox Code Playgroud)

之前,我一直在使用org.joda.time.DateTime并且工作正常。可以像下面这样在EL中访问此bean。

<h:outputText value="#{currentDate}" converter="#{converter}"/>
Run Code Online (Sandbox Code Playgroud)

但是,这导致引发以下异常。

16:15:29,984 SEVERE [javax.enterprise.resource.webcontainer.jsf.application] (default task-16) Error Rendering View[/admin_side/Home.xhtml]: com.sun.faces.mgbean.ManagedBeanCreationException: Unable to create managed bean currentDate.  The following problems were found:
     - Managed bean class java.time.ZonedDateTime for managed bean currentDate doesnt declare a public no-argument constructor.
    at com.sun.faces.mgbean.BeanManager.create(BeanManager.java:265)
    at com.sun.faces.el.ManagedBeanELResolver.resolveBean(ManagedBeanELResolver.java:257)
    at com.sun.faces.el.ManagedBeanELResolver.getValue(ManagedBeanELResolver.java:117)
    at com.sun.faces.el.DemuxCompositeELResolver._getValue(DemuxCompositeELResolver.java:176)
    at com.sun.faces.el.DemuxCompositeELResolver.getValue(DemuxCompositeELResolver.java:203)
    at com.sun.el.parser.AstIdentifier.getValue(AstIdentifier.java:116)
    at com.sun.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:226)
    at org.jboss.weld.el.WeldValueExpression.getValue(WeldValueExpression.java:50)
    at org.jboss.weld.el.WeldValueExpression.getValue(WeldValueExpression.java:50)
    at com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:109)
    at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:194)
    at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:182)
    at javax.faces.component.UIOutput.getValue(UIOutput.java:174)
    at com.sun.faces.renderkit.html_basic.HtmlBasicInputRenderer.getValue(HtmlBasicInputRenderer.java:205)
    at …
Run Code Online (Sandbox Code Playgroud)

jsf faces-config managed-bean java-time

2
推荐指数
1
解决办法
395
查看次数

如何在Java.time中将LocalDateTime的精度设置为纳秒?

根据java.time文档,java.time应该能够以纳秒精度呈现LocalDateTime或LocalTime,但是当我运行LocalDateTime.now()并打印出来时,它只显示3位而不是9位.

像这样:

2016-08-11T22:17:35.031
Run Code Online (Sandbox Code Playgroud)

有没有办法获得更高的精度?

java java-8 java-time

2
推荐指数
1
解决办法
8705
查看次数

从OffsetDateTime中删除分钟,秒和纳秒

是否有一种简洁的方法来消除所有的分钟,秒和纳秒OffsetDateTime?以下是我要做的事情.

final OffsetDateTime strippedTime = OffsetDateTime.now().withMinute(0).withSecond(0).withNano(0);
System.out.println(strippedTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.nnnnnnnnn")));
Run Code Online (Sandbox Code Playgroud)

上面的代码给了我:

2016-11-08 09:00:00.000000000
Run Code Online (Sandbox Code Playgroud)

LocalTime.MIDNIGHT遗憾地剥离了物体的小时数,所以对我来说没用.任何建议赞赏.

java java-time

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

java.time的官方区域名称列表在哪里?

是否有正式的区域名称列表用于以下内容:

zoneId = ZoneId.of("America/New_York")
Run Code Online (Sandbox Code Playgroud)

或者java.time.ZoneId是否可以生成我可以使用的列表本身?

我正在使用它将Instant转换为时间的字符串表示形式,如:

ZoneDateTime.ofInstant(instant, zoneId).format(DateTimeFormatter.ofPattern("..."))
Run Code Online (Sandbox Code Playgroud)

java java-time

2
推荐指数
4
解决办法
5477
查看次数

Joda-Java 8转换时间

如何在Java 8中实现以下功能java.time:

DateTime dt = new DateTime(year, month, date, hours, min, sec, milli);
Run Code Online (Sandbox Code Playgroud)

但我找到了这个选项:

OffsetDateTime.of(year, month, dayOfMonth, hour, minute, second, nanoOfSecond, offset)
Run Code Online (Sandbox Code Playgroud)

但它只有毫秒的方法.

我怎样才能达到与Joda-Time相同的效果?

datetime jodatime java-8 java-time

2
推荐指数
1
解决办法
4501
查看次数

DateTimeFormatter基于周的年份差异

我正在将我的应用程序从Joda-Time迁移到Java 8 java.time.

我遇到的一件事是使用图案中的模式打印基于周的一年DateTimeFormatter.

注意:我已经看到了这个问题: Java Time使用DateTimeFormatter解析基于星期的周模式

根据文件

y       year-of-era                 year              2004; 04
Y       week-based-year             year              1996; 96
Run Code Online (Sandbox Code Playgroud)

然而,当我尝试这两个时,似乎Y总是和它一样y.

我的测试代码:

DateTimeFormatter yearF = DateTimeFormatter.ofPattern("yyyy").withZone(ZoneOffset.UTC);
DateTimeFormatter weekYearF = DateTimeFormatter.ofPattern("YYYY").withZone(ZoneOffset.UTC);

DateTimeFormatter dateTimeFormatter = new DateTimeFormatterBuilder()
    .appendValue(ChronoField.YEAR_OF_ERA)   .appendLiteral(" ") .append(yearF)
    .appendLiteral(" -- ")
    .appendValue(IsoFields.WEEK_BASED_YEAR) .appendLiteral(" ") .append(weekYearF)
    .toFormatter()
    .withZone(ZoneOffset.UTC);

System.out.println(dateTimeFormatter.toString());

ZonedDateTime dateTime = ZonedDateTime.ofInstant(Instant.ofEpochMilli(946778645000L), ZoneOffset.UTC);
for (int i = 2000 ; i < 2020; i ++ ) {
    System.out.println(dateTime.withYear(i).format(dateTimeFormatter));
}
Run Code Online (Sandbox Code Playgroud)

输出:

Value(YearOfEra)' '(Value(YearOfEra,4,19,EXCEEDS_PAD))' -- 'Value(WeekBasedYear)' '(Localized(WeekBasedYear,4,19,EXCEEDS_PAD)) …
Run Code Online (Sandbox Code Playgroud)

java datetime dayofweek java-8 java-time

2
推荐指数
1
解决办法
848
查看次数

Java 8+中的DateTimeFormatter.ISO_LOCAL_DATE与DateTimeFormatter.ofPattern(“ yyyy-MM-dd”)

我有一个使用创建的日期ZonedDateTime.now(ZoneOffset.of("+2:00")).minusDays(5)。现在我想将其格式化为yyyy-MM-dd。

在Java 8+中,DateTimeFormatter.ISO_LOCAL_DATE等价于DateTimeFormatter.ofPattern("yyyy-MM-dd")吗?

输出看起来是相同的,并且从文档来看,它们应该是相同的。但是,其中的“本地”部分让我停了下来。注意,我有一个偏移量,并且我正在使用ZonedDateTime。我想确保没有任何陷阱或“除非在特殊情况下,否则它们是相同的,因为您正在处理偏移量等”。

java iso8601 date-formatting java-time localdate

2
推荐指数
1
解决办法
3746
查看次数

根据输入字符串解析LocalDateTime

客户端可以是能够发送或者String格式 "yyyy-MM-dd HH:mm:ss"或"yyyy-MM-dd"并根据它,我需要或者只是解析满LocalDateTime,如果他给我发了全格式或者创建LocalDateTime具有默认对象Time的一部分"23:59:59"

目前我已经编写了这个解决方案但是因为我使用异常来控制业务逻辑似乎很糟糕.

public class LocalDateTimeConverter implements IStringConverter<LocalDateTime> {

    private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    @Override
    public LocalDateTime convert(String value) {
        LocalDateTime localDateTime;
        try {
            localDateTime = LocalDateTime.parse(value, DATE_TIME_FORMATTER);
        } catch (DateTimeParseException ex) {
            localDateTime = LocalDateTime.of(LocalDate.parse(value), LocalTime.of(23, 59, 59));
        }

        return localDateTime;
    }

}
Run Code Online (Sandbox Code Playgroud)

有关如何更清楚地实施它的任何建议?

java java-8 java-time

2
推荐指数
1
解决办法
98
查看次数