我有一个文件可以具有日期修改值,具有日期或日期时间的格式.我曾经将值解析为:
String t = "2012-01-05T21:21:52.834Z";
logger.info(ZonedDateTime.parse(t).toEpochSecond() * 1000);
Run Code Online (Sandbox Code Playgroud)
现在,字符串也可以
t = "2012-01-05";
Run Code Online (Sandbox Code Playgroud)
哪个引起了错误
线程"main"中的异常java.time.format.DateTimeParseException:无法在java.time.format.DateTimeFormatter.parseResolved0(未知来源)的索引10处解析文本"2012-01-05"
如果我用这个字符串(参考)
LocalDate date = LocalDate.parse(t, DateTimeFormatter.ISO_DATE);
logger.info(date.atStartOfDay(ZoneId.of("UTC")).toEpochSecond() * 1000);
Run Code Online (Sandbox Code Playgroud)
这会奏效.但是,正如我已经提到的那样,字符串可以是这些类型中的任何一种,我如何识别格式然后相应地得到毫秒?
我想知道是否有可能在Java 8中解析时钟时间:分钟:秒?
例如
final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss");
final String str = "12:22:10";
final LocalDateTime dateTime = LocalDateTime.parse(str, formatter);
Run Code Online (Sandbox Code Playgroud)
我试过但得到这个例外:
Exception in thread "main" java.time.format.DateTimeParseException: Text '12:22:10' could not be parsed: Unable to obtain LocalDateTime from TemporalAccessor: {},ISO resolved to 12:22:10 of type java.time.format.Parsed
Run Code Online (Sandbox Code Playgroud) 我正在转换String包含三个不同时间开始、结束和持续时间的s序列
"00:01:00,00:02:00,00:01:00"
到LocalTime变量:
for (final String str : downtime) {
final DependencyDownTime depDownTime = new DependencyDownTime ();
final String[] strings = str.split (",");
if (strings.length == 3) {
LocalTime start = LocalTime.parse (strings[0]);
depDownTime.setStartTime (start);
LocalTime end = LocalTime.parse (strings[1]);
depDownTime.setEndTime (end);
Duration duration = Duration.between (start, end);
depDownTime.setDuration (duration);
downTimes.add (depDownTime);
}
}
Run Code Online (Sandbox Code Playgroud)
在String正在分析已发生变化,现在包括一个日期。 2017-09-13 00:01:00
如何删除仅保留时间的日期字符串?
我曾尝试使用 SimpleDateFormat
final DateFormat dateFormat = new SimpleDateFormat ("HH:mm:ss");
LocalTime start = LocalTime.parse (dateFormat.format …Run Code Online (Sandbox Code Playgroud) 我想查找我的LocalDateTime实例是否包含本地日期和时间或UTC日期和时间(如DateTime.KindC#中的属性)
LocalDateTime date1=LocalDateTime.now(); // it is local
LocalDateTime date2=LocalDateTime.now(ZoneId.of("UTC")); // it is UTC
Run Code Online (Sandbox Code Playgroud)
(date1.getKind() == Kind.UTC || date1.getKind() == Kind.Local)在Java中有类似的东西吗?
我有一个时间 A 应该在时间 B 的 90 分钟范围内(之前和之后)。
示例:如果 timeB 是 4:00 pm ,则时间 A 应该在 2:30pm (-90) 到 5:30pm (+90) 之间
尝试了以下方法:
if(timeA.isAfter(timeB.minusMinutes(90)) || timeA.isBefore(timeB.plusMinutes(90))) {
return isInRange;
}
Run Code Online (Sandbox Code Playgroud)
你能帮我这里的逻辑有什么问题吗?
我研究了使用 Java SDK 存储日期和时间的 DynamoDB 选项。据我所知,Java8 LocalDate 和 LocalDateTime 需要自定义转换器,而对于旧的 Date、Calendar 和 Joda 时间 LocalDateTime 有一些注释。
根据文档,@DynamoDBTypeConvertedTimestamp 支持标准日期类型转换,例如 java.util.Calendar、Long。@DynamoDBTypeConvertedEpochDate 可用于将日期、日历和 org.joda.time.DateTime 转换为数值。
同时,如果我不在 Date 字段上添加任何注释,它会被保存,就像它上面有 @DynamoDBTypeConvertedTimestamp 一样,甚至范围查询也能成功工作。
有没有人有知识来解释不同的可能方法,并就从 DynamoDB 存储和检索 Java 日期和时间对象的最佳方法发表意见?
我正在以两种略有不同的格式解析同一个日期,但遇到了一个我不明白的错误。
解析标准ISO格式的字符串成功:
String s = "2018-04-17T22:57:29";
LocalDateTime date = LocalDateTime.parse(s, DateTimeFormatter.ISO_DATE_TIME); // OK
Run Code Online (Sandbox Code Playgroud)
但是,当我添加后缀 a"Z"并使用时ISO_INSTANT:
s = "2018-04-17T22:57:29Z";
date = LocalDateTime.parse(s, DateTimeFormatter.ISO_INSTANT); // Fails
Run Code Online (Sandbox Code Playgroud)
我收到以下异常:
Exception in thread "main" java.time.format.DateTimeParseException: Text '2018-04-17T22:57:29Z' could not be parsed: Unable to obtain LocalDateTime from TemporalAccessor: {MicroOfSecond=0, NanoOfSecond=0, MilliOfSecond=0, InstantSeconds=1524005849},ISO of type java.time.format.Parsed
at java.time.format.DateTimeFormatter.createError(DateTimeFormatter.java:1920)
at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1855)
at java.time.LocalDateTime.parse(LocalDateTime.java:492)
at iplus.fwk.manifest.Test.main(Test.java:37)
Caused by: java.time.DateTimeException: Unable to obtain LocalDateTime from TemporalAccessor: {MicroOfSecond=0, NanoOfSecond=0, MilliOfSecond=0, InstantSeconds=1524005849},ISO of type java.time.format.Parsed
at java.time.LocalDateTime.from(LocalDateTime.java:461)
at …Run Code Online (Sandbox Code Playgroud) 我找不到如何在 Scala/Java 中轻松计算给定时间点的时区偏移的好答案。
鉴于:
要求:计算时区 1 中特定日期的偏移量,需要将此日期移动到时区 2。
例子:
日期时间 = 2017-10-23 00:00:00 在America/Los_Angeles时区 1,
时区 2 = Europe/Paris
偏移量应等于 +9,它可用于将日期从时区 1 移动到时区 2。
我有一个localdatetimeUTC的对象。我想转换成IST。我怎样才能做到这一点?
LocalDateTime dateTimeOfAfterFiveDays = LocalDateTime.ofEpochSecond(after5,0,ZoneOffset.UTC);
Run Code Online (Sandbox Code Playgroud) 我正在尝试做一些看似很简单的事情,但是我一生无法使它正常工作。
我想将某些字符串解析为LocalTime,然后以所需的格式打印它们。我想要的是:
HH:mm:ss(13:00:00打印为13:00:00)。13:45:20并且13:45:20.000两个都打印为13:45:20)13:45:20.01打印为13:45:20.010)根据以下文档,似乎可以在DateTimeFormatter中使用可选选项optionalStart:
All elements in the optional section are treated as optional.
During formatting, the section is only output if data is available in the
{@code TemporalAccessor} for all the elements in the section.
During parsing, the whole section may be missing from the parsed string.
Run Code Online (Sandbox Code Playgroud)
但是,强制将Millis保留为小数点后3位似乎绕过了可选方面,即.000在millis == 0时被打印:
final DateTimeFormatter formatter = …Run Code Online (Sandbox Code Playgroud)