标签: java-time

计算两个小时的字符串之间的差异

我有一个包含某些小时前的字符串。14:34,现在我要计算当前小时前的时差。21:36-14:34= 7小时2分钟(或类似的时间)。有人可以向我解释我该怎么做?

java time jodatime java-time

5
推荐指数
1
解决办法
2426
查看次数

如何使用java.time从年份和星期解析字符串中的日期

在旧的Java中,我可以这样做:

System.out.println(new SimpleDateFormat("yyyy w", Locale.UK).parse("2015 1"));
// shows Mon Dec 29 00:00:00 CET 2014

System.out.println(new SimpleDateFormat("yyyy w", Locale.US).parse("2015 1"));
// shows Mon Dec 28 00:00:00 CET 2014
Run Code Online (Sandbox Code Playgroud)

我想在Java 8中使用java.time.

System.out.println( LocalDate.parse("2015 1", DateTimeFormatter.ofPattern("yyyy w", Locale.US)));
Run Code Online (Sandbox Code Playgroud)

结果:

java.time.format.DateTimeParseException:无法解析文本'2015 1':无法从TemporalAccessor获取LocalDate:{WeekOfWeekBasedYear [WeekFields [SUNDAY,1]] = 1,Year = 2015},ISO类型为java.time. format.Parsed

如何在java.time中做到这一点?

此外,我不满意我必须通过Locale来确定一周的第一天:星期一和星期天.它不是乡村功能,而是日历功能.我想使用像java.time.temporal.WeekFields.ISO这样的东西来显示周从星期一开始的世界

我发现了类似的情况:https://stackoverflow.com/questions/3941700/how-to-get-dates-of-a-week-i-know-week-number

但不适用于Java 8中的java.time.此外,首先创建日期对象并稍后设置正确周的解决方案并不优雅.我想一次创建最终日期.

java java-time

5
推荐指数
1
解决办法
2407
查看次数

如何将小时,分钟,秒和毫秒变为零?

我想让我的格式为2016-07-08T00:00:00.000Z.

String myDate = "20160708";
LocalDate myLocalDate = LocalDate.parse(myDate, DateTimeFormatter.BASIC_ISO_DATE);
OffsetDateTime myOffsetDate = myLocalDate.atTime(OffsetTime.now(ZoneOffset.UTC));

System.out.println(myOffsetDate); //2016-07-08T14:58:23.170Z
Run Code Online (Sandbox Code Playgroud)

java-8 java-time

5
推荐指数
1
解决办法
2992
查看次数

解析时DateTimeFormatter的替代区域如何​​工作?

解析时,我对DateTimeFormatter' withZone方法的行为感到困惑。根据它的文档:

解析时,要考虑两种不同的情况。如果直接从文本中解析了区域,则可能是因为使用了DateTimeFormatterBuilder.appendZoneId(),所以此覆盖区域无效。如果没有解析任何区域,则该覆盖区域将包含在解析结果中,可在该结果中用于构建瞬间和日期时间。

基于此以及DateTimeFormatter.ISO_DATE_TIME解析时区的事实,我希望以下两项测试能够通过。

@Test
public void testNoZoneInInput() {
    final ZonedDateTime expected = ZonedDateTime.of(2017, 2, 2, 9, 0, 0, 0, ZoneId.of("UTC"));
    final ZonedDateTime actual = ZonedDateTime.parse("2017-02-02T10:00:00", DateTimeFormatter.ISO_DATE_TIME.withZone(ZoneId.of("UTC+1")));
    Assert.assertTrue("Expected " + expected + ", got " + actual + " instead.", expected.isEqual(actual));
}

@Test
public void testWithZoneInInput() {
    final ZonedDateTime expected = ZonedDateTime.of(2017, 2, 2, 9, 0, 0, 0, ZoneId.of("UTC"));
    final ZonedDateTime actual = ZonedDateTime.parse("2017-02-02T09:00:00Z", DateTimeFormatter.ISO_DATE_TIME.withZone(ZoneId.of("UTC+1")));
    Assert.assertTrue("Expected " + expected + ", got " + actual …
Run Code Online (Sandbox Code Playgroud)

java java-time

5
推荐指数
1
解决办法
188
查看次数

如何判断ZonedDateTime是"今天"?

我以为这已经被问到了,但我找不到任何东西.

使用java.time什么是确定给定ZonedDateTime是"今天" 的最佳方法?

我想出了至少两种可能的解决方案.我不确定这些方法是否存在任何漏洞或陷阱.基本上这个想法是让我们java.time弄清楚,而不是自己做任何数学:

/**
 * @param zonedDateTime a zoned date time to compare with "now".
 * @return true if zonedDateTime is "today".
 * Where today is defined as year, month, and day of month being equal.
 */
public static boolean isZonedDateTimeToday1(ZonedDateTime zonedDateTime) {
    ZonedDateTime now = ZonedDateTime.now();

    return now.getYear() == zonedDateTime.getYear()
            && now.getMonth() == zonedDateTime.getMonth()
            && now.getDayOfMonth() == zonedDateTime.getDayOfMonth();
}


/**
 * @param zonedDateTime a zoned date time to compare with …
Run Code Online (Sandbox Code Playgroud)

java java-time

5
推荐指数
1
解决办法
2265
查看次数

Hibernate Java 8 ZonedDateTime

我正在使用Hibernate和JPA将我的数据存储在数据库中.现在我想保存ZonedDateTime的java.time.

问题是,Hibernate只会坚持日期和时间.它不关心ZoneId或偏移.

有没有办法说服Hibernate持有这些信息?

hibernate jpa java-8 java-time zoneddatetime

5
推荐指数
1
解决办法
5013
查看次数

Spring boot + Jackson - 始终将日期转换为UTC

我在没有时间戳的情况下将日期保存在我的数据库中,因此我希望我的Spring Boot Rest控制器接收日期的方式标准化,因此可以在世界任何地方部署服务(AWS EC2等).

我试着设置以下属性,没有帮助:

spring.jackson.time-zone=UTC
Run Code Online (Sandbox Code Playgroud)

还有另一个属性,总是true,所以我没有设置,那就是:

spring.jackson.deserialization.adjust-dates-to-context-time-zone=true
Run Code Online (Sandbox Code Playgroud)

我正在部署到2个独立的Ubuntu容器,一个在UTC时区,另一个在我当前的时区America/Sao_Paulo,即(GMT -3).

示例负载:

{"date":"2017-09-15T18:58:00.000Z"}
Run Code Online (Sandbox Code Playgroud)

当服务部署在圣保罗时,它会收到:

2017-09-15 18:58:00.000000
Run Code Online (Sandbox Code Playgroud)

这是正确的.

在UTC上部署服务时,它会收到:

2017-09-15 15:58:00.000000
Run Code Online (Sandbox Code Playgroud)

这是不正确的.

我用Java存储日期信息LocalDateTime.

示例型号:

import java.time.LocalDateTime;

class Model {
    private LocalDateTime date;

    public LocalDateTime getDate() {
        return date;
    }

    public void setDate(LocalDateTime date) {
        this.date = date;
    }
}
Run Code Online (Sandbox Code Playgroud)

示例资源:

@RestController
class Resource {
    @RequestMapping(consumes = MediaType.APPLICATION_JSON_VALUE, method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<?> add(@RequestBody Model model) {
        System.out.println(model.getDate()); …
Run Code Online (Sandbox Code Playgroud)

java spring-mvc spring-boot java-time jackson2

5
推荐指数
1
解决办法
6412
查看次数

LocalDate:解析MM-yyyy

我java.time.format.DateTimeParseException试图做以下事后得到:

LocalDate.parse( "09-2017" , DateTimeFormatter.ofPattern("MM-yyyy") )
Run Code Online (Sandbox Code Playgroud)

怎么了?Java中是否有用于检查dateString格式的实用程序?

java parsing date java-8 java-time

5
推荐指数
1
解决办法
2329
查看次数

在计算1918-03-24之后的天数时,Joda-time一个一个错误

计算使用Joda-Time 之间的天数1900-01-01和日期之间的天数1918-03-24似乎给出了一个一个接一个的结果.

使用Java 8可以java.time得到正确的结果.Joda-Time不计算的原因是什么1918-03-25?

使用Joda-time v2.9.9.

public static void main(String[] args) {
    jodaDiff("1918-03-24");
    javaDiff("1918-03-24");
    jodaDiff("1918-03-25");
    javaDiff("1918-03-25");
    jodaDiff("1918-03-26");
    javaDiff("1918-03-26");
    jodaDiff("2017-10-10");
    javaDiff("2017-10-10");
}
private static void jodaDiff(String date) {
    DateTime start = new DateTime(1900, 1, 1, 0, 0, 0, DateTimeZone.forID("UTC"));
    DateTimeFormatter dateDecoder = DateTimeFormat.forPattern("YYYY-MM-dd");
    DateTime end = dateDecoder.parseDateTime(date);
    int diff =  Days.daysBetween(start, end).getDays();
    System.out.println("Joda " + date + " " + diff);
}
private static void javaDiff(String date) {
    LocalDate start = LocalDate.parse("1900-01-01");
    LocalDate …
Run Code Online (Sandbox Code Playgroud)

java datetime jodatime date-difference java-time

5
推荐指数
1
解决办法
352
查看次数

Java 8 DateTimeFormatter解析可选部分

我需要将日期时间解析为两种不同格式的字符串:

  • 19861221235959Z
  • 1986-12-21T23:59:59Z

以下dateTimematmat模式正确解析第一种日期字符串

DateTimeFormatter.ofPattern ("uuuuMMddHHmmss[,S][.S]X")
Run Code Online (Sandbox Code Playgroud)

但是第二个失败,因为没有预期破折号,冒号和T.

我的尝试是使用如下可选部分:

DateTimeFormatter.ofPattern ("uuuu[-]MM[-]dd['T']HH[:]mm[:]ss[,S][.S]X")
Run Code Online (Sandbox Code Playgroud)

出乎意料的是,这解析了第二种日期字符串(带有破折号的日期字符串),但不是第一种,抛出一个

java.time.format.DateTimeParseException: Text '19861221235959Z' could not be parsed at index 0
Run Code Online (Sandbox Code Playgroud)

就像可选部分没有被评估为可选部分一样......

java date-parsing java-8 java-time

5
推荐指数
1
解决办法
715
查看次数