标签: java-time

Java解析DateTime并忽略时区

我需要解析以下格式的日期字符串。

“2016-05-19 13:07:15 PDT”

我的数据库服务器位于 ET 中,但我想将 TIMESTAMP 存储2016-05-19 13:07:15在我的数据库中,没有时区,也不将其转换为 ET。所以,本质上,我想忽略字符串中的 PDT。

目前,我将字符串解析为一个ZonedDateTime对象,并将其映射到数据库中的 TIMESTAMP 数据类型,但 hibernate 正在后端进行一些转换,因为它知道 myZonedDateTime位于 PDT 中。因此,上面示例中的日期最终存储在我的数据库中,而2016-05-19 16:07:15不是我想要的,2016-05-19 13:07:15.

我想到的一个想法是从字符串末尾剥离 PDT,然后将其解析为 a LocalDateTime,但在这样做之前,我想看看是否有任何方法可以基本上忽略 a 中的时区ZonedDateTime,或者将上面显示的字符串解析为 a LocalDateTime(或其他一些不关心时区的 java 时间对象),而不实际执行从 PDT 到 ET 的转换。

有没有什么方法可以实现这一点,而无需在解析之前将时区从字符串中实际拉出?

java hibernate jpa java-time

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

如何使用默认时区偏移量将 Java ZonedDateTime 转换为 OffsetDateTime?

假设我有一个 ZonedDateTime 2018-10-30T18:04:58.874Z:如何将其转换为 OffsetDateTime2018-10-30T13:04:58.874-05:00

我更喜欢偏移量是默认/系统偏移量,例如从OffsetDateTime.now().

java java-time

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

如何使用 #inst Date 文字中的 clojure.java-time 获得 java.time DateTime?

使用clj-time可以像这样解析 #inst Date 文字:

(require '[clj-time.coerce :as c])
(c/from-date #inst "2012-12-12")
;; => #object[org.joda.time.DateTime 0x4a251269 "2012-12-12T00:00:00.000Z"]
Run Code Online (Sandbox Code Playgroud)

这将如何使用新的 java.time 包装器clojure.java-time来完成?

clojure java-time

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

如何从给定季度获得月份

我试图从某个季度获得几个月。使用下面的代码,我成功地从 LocalDate.now() 实例中获取了当前季度的月份名称。

我如何从一个季度的字符串(例如“Q1”)中获得一个季度的月份?

int monthInt = Month.from(LocalDate.now()).firstMonthOfQuarter().getValue();
      for (int j = 1; j <= 3; j++) { //for each month in quarter
          System.out.println(Month.of(monthInt).name()); //January, February, March
          monthInt++;
      }
Run Code Online (Sandbox Code Playgroud)

java date java-time localdate

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

DateTimeFormatter 无法解析格式化的日期

我使用 DateTimeFormatter 来格式化日期。这有效,但是当我尝试将此格式化日期转换回日期时,出现异常。

代码:

DateTimeFormatter weekOfYear = DateTimeFormatter.ofPattern("w/yyyy");

LocalDate localDate = LocalDate.now();
String dateString = weekOfYear.format(localDate);

localDate = LocalDate.parse(dateString, weekOfYear);
Run Code Online (Sandbox Code Playgroud)

当我尝试解析它时出现异常:

java.time.format.DateTimeParseException: Text '50/2019' could not be parsed: Unable to obtain LocalDate from TemporalAccessor: {Year=2019, WeekOfWeekBasedYear[WeekFields[MONDAY,4]]=50},ISO of type java.time.format.Parsed

    at java.base/java.time.format.DateTimeFormatter.createError(DateTimeFormatter.java:2020)
    at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1955)
    at java.base/java.time.LocalDate.parse(LocalDate.java:428)
    at DateTimeFormatterTest.weekOfYear(DateTimeFormatterTest.java:17)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:567)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) …
Run Code Online (Sandbox Code Playgroud)

java java-time

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

java.time.DateTimeException:无法从时间中提取 ZoneId

当我运行第一段时,它非常好并生成一个输出。但是在第二种情况下,当我运行此段 2 时,它会生成

DateTimeException : Unable to extract ZoneId from temporal.
Run Code Online (Sandbox Code Playgroud)

第 1 部分:

LocalDate ld = LocalDate.now();
System.out.println(DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL).format(ld));
Run Code Online (Sandbox Code Playgroud)

第 2 部分:

LocalDateTime ldt = LocalDateTime.now();
DateTimeFormatter dtf = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.FULL);
System.out.println(dtf.format(ldt));
Run Code Online (Sandbox Code Playgroud)

java datetime-format java-time

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

如何获得日期之间的月数?

在这个例子中,now和now + 2 months不等于 2之间的差异,尽管我认为LocalDate数学是这样工作的:

import java.time.LocalDate;
import static java.time.temporal.ChronoUnit.MONTHS;

public class MyClass {
    public static void main(String... args) {
            LocalDate now = LocalDate.of(2020, 7, 31);
            LocalDate later = now.plusMonths(2);
            
            System.out.println("Now: " + now);
            System.out.println("Later: " + later);
            System.out.println("Months between now and later: " + MONTHS.between(now, later));
    }
}
Run Code Online (Sandbox Code Playgroud)

输出:

Now: 2020-07-31
Later: 2020-09-30
Months between now and later: 1
Run Code Online (Sandbox Code Playgroud)

我发现这一点只是因为我碰巧运行了一个单元测试,该测试的日期超出了预期......

查看 LocalDate.addMonths 的 javadoc:

此方法分三步将指定的金额添加到月份字段:

Add the input months to the month-of-year field …
Run Code Online (Sandbox Code Playgroud)

java java-time

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

如何将 Java 的日期字符串/对象转换为 JFreeChart 的日期格式?

我正在使用 JFreeChart 库创建烛台图表。我需要将时间值传递给 OHLCSeries,但它只接受自己的时间值

我要通过这个类https://www.jfree.org/jfreechart/api/javadoc/org/jfree/data/time/Hour.html

而这个构造函数: Hour(int hour, int day, int month, int year)

我从 API 中获得的数据最初是这样的:2021-03-02T16:00:00.000Z

我的问题:如何将此日期字符串/Java 时间格式转换为 JFreeChart 的日期格式?我只能考虑一些复杂的转换成Java类型+分别返回每个值,或者使用正则表达式

java datetime jfreechart date-format java-time

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

从 1970 年到 2020.01.01 的毫秒数

我试图从纪元到 2020.01.01 获得毫秒。我对 Date 使用了旧方法,我也想使用新的性感 LocalDate 但我得到的两个结果不同:

long millisecondsInTheDay = 24 * 60 * 60 * 1000;
long millis1 = LocalDate.of(2020, Month.JANUARY, 1).toEpochDay() * millisecondsInTheDay; // 1577836800000
long millis2 = new Date(2020 - 1900, 0, 1).toInstant().toEpochMilli(); // 1577833200000
Run Code Online (Sandbox Code Playgroud)

差异正好是一小时(3600_000 毫秒)。为什么我得到不同的结果?

java timezone calendar date java-time

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

将小写的短工作日解析为 Java 8 的时间 DayOfWeek

我有一个字符串,例如“mon”或“tue”或“sun”。我想将其解析为 a DayOfWeek(如果不成功则为 null)。

我正在使用 Kotlin,但我想 Java 人员也会明白我想做什么:

private fun String.parseToDayOfWeek(pattern: String = "EE") =
    try {
        DateTimeFormatter.ofPattern(pattern, Locale.US).parse(this, DayOfWeek::from)
    } catch (e: Exception){
        null
    }
Run Code Online (Sandbox Code Playgroud)

这不起作用,我只是得到nulls。

相反,在我解析它之前,我必须像这样清理字符串:

val capitalized = this.lowercase().replaceFirstChar { it.uppercase() }
Run Code Online (Sandbox Code Playgroud)

这感觉很麻烦。我使用的 api 是错误的还是这是一个巨大的绊脚石?

java dayofweek date-parsing java-time localdatetime

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