我试图将今天的日期格式设置为“MM/dd/yyy HH:00”,其中分钟始终清零。使用此格式时,时间无法正确显示:
LocalDate date = DateTime.now().withZone(DateTimeZone.forID("America/New_York")).toLocalDate();
String format = date.toString("MM/dd/yyyy HH:00");
logger.info(format);
Run Code Online (Sandbox Code Playgroud)
输出
有没有其他方法可以使用 来获取时间LocalDate?
我有以下设置工作日的 Joda-Time 代码:
LocalDateTime localDateTime = new LocalDateTime(2016, 1, 1, 20,39);
LocalDateTime localDateTime1 = localDateTime.withDayOfWeek(7);
Run Code Online (Sandbox Code Playgroud)
我怎样才能做同样的事java.time?我看到没有setDayOfTheWeek:
LocalDateTime time;
time.getDayOfWeek()
Run Code Online (Sandbox Code Playgroud) 为了提高某些遗留代码的性能,我正在考虑用 java.time.format.DateTimeFormatter 替换 java.text.SimpleDateFormat。
执行的任务之一是解析使用 java.util.Date.toString 序列化的日期/时间值。使用 SimpleDateFormat,可以将它们转回原始时间戳(忽略小数秒),但是在尝试使用 DateTimeFormatter 执行相同操作时,我遇到了问题。
使用任一格式进行格式化时,我的本地时区会显示为 CET 或 CEST,具体取决于夏令时是否对要格式化的时间有效。然而,在解析时,DateTimeFormatter 似乎对 CET 和 CEST 的处理方式相同。
这会产生夏令时结束时发生重叠的问题。格式化时,02:00:00 创建两次,时间间隔一小时,但使用 CEST 和 CET 时区名称 - 这很好。但在解析时,这种差异无法收回。
这是一个例子:
long msecPerHour = 3600000L;
long cet_dst_2016 = 1477778400000L;
DateTimeFormatter formatter =
DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss zzz yyyy", Locale.ENGLISH);
ZoneId timezone = ZoneId.of("Europe/Berlin");
for (int hours = 0; hours < 6; ++hours) {
long time = cet_dst_2016 + msecPerHour * hours;
String formatted = formatter.format(Instant.ofEpochMilli(time).atZone(timezone));
long parsedTime = Instant.from(formatter.parse(formatted)).toEpochMilli();
System.out.println(formatted + ", diff: …Run Code Online (Sandbox Code Playgroud) 我正在使用以下时间戳格式:
yyyyMMddHHmmssSSS
Run Code Online (Sandbox Code Playgroud)
下面的方法效果很好:
public static String formatTimestamp(final Timestamp timestamp, final String format) {
final DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format);
return timestamp.toLocalDateTime().format(formatter);
}
Run Code Online (Sandbox Code Playgroud)
而且,当我传入带有该格式字符串的时间戳时,它会返回,例如:
20170925142051591
Run Code Online (Sandbox Code Playgroud)
然后,我需要再次从该字符串映射到时间戳,本质上是相反的操作。我知道我可以使用 aSimpleDateFormat及其parse()方法,但如果可能的话,我更愿意坚持使用java.time样式格式。
我写了这段(相当hacky)代码,它适用于某些格式,但不适用于这种特定格式:
public static Timestamp getTimestamp(final String text, final String format, final boolean includeTime) {
final DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format);
final TemporalAccessor temporalAccessor = formatter.parse(text);
if (includeTime) {
final LocalDateTime localDateTime = LocalDateTime.from(temporalAccessor);
return DateTimeUtil.getTimestamp(localDateTime);
} else {
final LocalDate localDate = LocalDate.from(temporalAccessor);
return DateTimeUtil.getTimestamp(localDate);
}
}
Run Code Online (Sandbox Code Playgroud)
它在第二行的部分失败formatter.parse(text);。 …
如何将java.time.LocalTime带有秒分数(微/毫/纳)的 a 转换为 a java.sql.Time?
我知道,由于spring-data-jpa这些类型的 2.x 版本(JSR310)具有映射到旧版本的自定义转换器,但我java.sql.Date在.java.sql.TimestampLocalTime
我对包装 Java 8 的 java.time api的clojure.java-time库用法的使用感到困惑。
我想要一个将字符串格式的日期从“yyyy-MM-dd'T'HH:mm:ss”格式转换为“MM/dd/YYYY hh:mm:ss a”格式的函数。
这是我的功能:
(require '[java-time :as jt])
(defn change-ds-format [in-ds]
{:pre [string? in-ds ]}
(let [input-format "yyyy-MM-dd'T'HH:mm:ss"
output-format "MM/dd/YYYY hh:mm:ss a"]
(->> in-ds
(jt/local-date-time input-format)
(jt/format output-format))))
Run Code Online (Sandbox Code Playgroud)
这看起来没问题:
(change-ds-format "2019-12-28T00:00:00" )
;=> "12/28/2019 12:00:00 AM"
Run Code Online (Sandbox Code Playgroud)
我不知道为什么要推迟到 2020 年?
(change-ds-format "2019-12-29T00:00:00" )
;=> "12/29/2020 12:00:00 AM"
;; Why is this one 2020 ???
Run Code Online (Sandbox Code Playgroud)
我想这可能是时区偏移问题,但我不相信它应该转移到 2020 年。
我正在使用 TrueTime 库,它Date在系统时区返回。Date当我将其转换为毫秒时,我在将其转换为 UTC 日期时遇到问题。
这是我所做的:
// getting true time in GMT [ex : 2020-07-13T18:00:57.192+03:00 [Europe/Istanbul]]
Date now = getTrueNowTimeInGMT();
// I like to use `LocalDateTime`, so I am converting `Date` to `LocalDateTime`:
LocalDateTime ldtOfSystem = LocalDateTime.ofInstant(now.toInstant(), ZoneId.systemDefault());
// Converting system default zone to UTC
ZonedDateTime zdtOfSystem = ldtOfSystem.atZone(ZoneId.systemDefault());
ZonedDateTime zdtOfSystemInUTC = zdtOfSystem.withZoneSameInstant(ZoneId.of("UTC"));
// Making sure that it is in the UTC. It is returning correct UTC time [ex: 2020-07-13T15:00:57.192 [UTC]]
System.out.println(zdtOfSystemInUTC);
// converting zdtOfSystemInUTC to …Run Code Online (Sandbox Code Playgroud) 我有以下格式的日期:1/1/2020 3:4:7 AM我正在尝试使用DateTimeFormatter.
我有以下带有格式化程序的代码来解析它,但它不起作用。
LocalDateTime date = LocalDateTime.parse("1/1/2020 3:4:7 AM", DateTimeFormatter.ofPattern("MM/dd/yyyy hh:mm:ss a"));
Run Code Online (Sandbox Code Playgroud)
我收到以下异常:
java.time.format.DateTimeParseException: Text '1/1/2020 3:4:7 AM' could not be parsed at index 0
谁能帮我?
java java-time localdatetime datetimeformatter java.time.localdatetime
我是 Java 新手,我不明白我的日期解析有什么问题。我已经尝试了许多类似帖子的解决方案,阅读了 DateTimeFormatter 文档,但仍然卡住了。任何帮助表示赞赏。谢谢你。
代码
String date = "2021-02-19T00:45:09.798Z"
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ss.SSSZ");
ZonedDateTime parsedDate = ZonedDateTime.parse(date, formatter);
Run Code Online (Sandbox Code Playgroud)
错误
java.time.format.DateTimeParseException: Text '2021-02-19T00:45:09.798Z' could not be parsed, unparsed text found at index 23
Run Code Online (Sandbox Code Playgroud)
我也尝试使用DateTimeFormatter.ofPattern(pattern).withZone(zone)并收到相同的错误。
我有几行代码,如下所示
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MMM-yy");
formatter = formatter.withLocale( Locale.getDefault() );
LocalDate date = LocalDate.parse("11-NOV-20", formatter);
Run Code Online (Sandbox Code Playgroud)
这给出了以下异常
Exception in thread "main" java.time.format.DateTimeParseException: Text '11-NOV-20' could not be parsed at index 3
at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1949)
at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1851)
at java.time.LocalDate.parse(LocalDate.java:400)
at src.DateTimeTest.main(DateTimeTest.java:30)
Run Code Online (Sandbox Code Playgroud)
如果我将月份从11 月更改为11 月,效果很好。我从数据库表中获取这个值。我是否必须以编程方式更改它,或者有什么方法可以处理它?