在我的Mac和Windows机器上创建新的LocalDateTime使用时LocalDateTime.now(),我的Mac上的nano 精度为6,而我的Windows机器上的纳米精度为3.两者都在运行jdk-1.8.0-172.
有人可以解释一下Instant.getEpochSecond与Instant.toEpochMilli命名背后的理由吗?我能想到的唯一原因是瞬间的内部表示为相对于纪元的秒数和相对于该秒的纳秒时间,而毫秒数是根据这两个值计算的.
但是为什么要让这样的实现细节泄漏到新的API中呢?
如果我使用类似于d/M/yy创建Java 8 DateTimeFormatter的模式(例如使用DateTimeFormatter.ofPattern(pattern);(我将仅用于解析,而不是格式化),它将所有两个字母的年份解释为20xx,例如解析一个字符串,如同13/5/99被解释为2099-05-13,在我的情况下是错误的(它应该是在1999年).
在我的应用程序中,我试图解析OCR'd文档中的日期,例如仍然可以从90年代开始,所以使用旧的SimpleDateFormat行为将日期解释为在当前日期之前和之后的20年之内我很好.但由于各种原因,我仍然希望将整个日期解析逻辑切换到新的Java 8 DateTimeFormatters.
通过Java源代码,我看到这都是相对于常量解释的ReducedPrinterParser.BASE_DATE,但我认为无法更改从模式构建自己的格式化程序时使用的值.这是不可能的,还是我错过了指定解析两个字母年份的行为的可能性?
如果我们在14:05离开法兰克福,并在16:40到达洛杉矶.飞多长时间?
我试过以下:
ZoneId frank = ZoneId.of("Europe/Berlin");
ZoneId los = ZoneId.of("America/Los_Angeles");
LocalDateTime dateTime = LocalDateTime.of(2015, 02, 20, 14, 05);
LocalDateTime dateTime2 = LocalDateTime.of(2015, 02, 20, 16, 40);
ZonedDateTime berlinDateTime = ZonedDateTime.of(dateTime, frank);
ZonedDateTime losDateTime2 = ZonedDateTime.of(dateTime2, los);
int offsetInSeconds = berlinDateTime.getOffset().getTotalSeconds();
int offsetInSeconds2 = losDateTime2.getOffset().getTotalSeconds();
Duration duration = Duration.ofSeconds(offsetInSeconds - offsetInSeconds2);
System.out.println(duration);
Run Code Online (Sandbox Code Playgroud)
但我无法得到大约11小时30分钟的成功答案.有人请帮助我弄清楚上面的问题.谢谢 :)
我想以"dd.MM.yyyy "模式格式化我的java 8 LocalDateTime对象.有格式的库吗?我尝试了下面的代码,但得到了转换异常.
<fmt:parseDate value="${date}" pattern="yyyy-MM-dd" var="parsedDate" type="date" />
Run Code Online (Sandbox Code Playgroud)
在JSTL中是否有LocalDateTime类的标记或转换器?
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;
public class Test001 {
public static void main(String[] args) {
Date dt = new Date();
LocalDateTime localDateTime = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS zzz").withZone(ZoneId.of("America/New_York"));
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS zzz");
System.out.println(formatter.format(localDateTime));
System.out.println(sdf.format(dt));
}
}
Run Code Online (Sandbox Code Playgroud)
2016-04-19 11:25:34.917 ET
2016-04-19 11:25:34.637 EDT
Run Code Online (Sandbox Code Playgroud)
我的笔记本电脑的时区是"美国/纽约"(即东部时间美国/加拿大).
我想知道如何使用"EDT"代替"ET" DateTimeFormatter.
我也想知道:为什么在解析/格式模式方面DateTimeFormatter不兼容SimpleDataFormat(因为这个例子表明它们不兼容).
我想写一个布尔值函数,如果给定LocalDateTime落在两个特定时间点之间,则返回true,否则返回false.
具体来说,LocalDateTime如果给定日期在格林威治标准时间周五22:00到格林威治标准时间周日23:00之间,我希望有一个过滤器.
骨架看起来像这样:
public boolean isWeekend(LocalDateTime dateTime) {
//Checks if dateTime falls in between Friday's 22:00 GMT and Sunday's 23:00 GMT
//return ...???
}
Run Code Online (Sandbox Code Playgroud)
这基本上是一个周末过滤器,我想知道是否有一个简单的解决方案与新的Java 8时间库(或任何其他现有的过滤器方法).
我知道如何检查星期几,小时等,但要避免重新发明轮子.
我有以毫秒为单位的时间,我需要将其转换为 ZonedDateTime 对象。
我有以下代码
long m = System.currentTimeMillis();
LocalDateTime d = LocalDateTime.millsToLocalDateTime(m);
Run Code Online (Sandbox Code Playgroud)
线
LocalDateTime d = LocalDateTime.millsToLocalDateTime(m);
Run Code Online (Sandbox Code Playgroud)
给我一个错误,说本地日期时间类型未定义methed millsToLocalDateTime
我从两个版本的天气 API 获取日期数据。第一个是这样的字符串:2019-08-07 09:00:00和这样的:1565209665。如何将其更改为仅日期或日期和月份的名称?例如,星期一,八月。
我在几个配置中尝试过类似的东西,但它只能在完整版中使用。如果我发现某些东西,则会引发错误:
var date = list.get(position).dt_txt
val formatter = DateTimeFormatterBuilder()
.appendPattern("yyyy-MM-dd HH:mm:ss").toFormatter()
formatter.parse(date)
Run Code Online (Sandbox Code Playgroud) 以下代码片段在 Java 8 中运行没有错误。但是,当我在 Java 17 中运行相同的代码时,它会失败。
import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
public class Main2 {
public static void main(String[] args) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH[:mm[:ss[.SSS]]]X");
OffsetDateTime offSetDateTime = OffsetDateTime.parse("2021-10-09T08:59:00.00Z", formatter);
System.out.println(offSetDateTime);
}
}
Run Code Online (Sandbox Code Playgroud)
在 Java 17 上运行时的输出:
Exception in thread "main" java.time.format.DateTimeParseException: Text '2021-10-09T08:59:00.00Z' could not be parsed at index 19
at java.base/java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:2052)
at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1954)
at java.base/java.time.OffsetDateTime.parse(OffsetDateTime.java:404)
at Main2.main(Main2.java:9)
Run Code Online (Sandbox Code Playgroud)
但是,如果我针对 Java 8 运行相同的代码,则会给出以下结果:
2021-10-09T08:59Z
Run Code Online (Sandbox Code Playgroud)
如果我将测试数据从 更改为"2021-10-09T08:59:00.00Z","2021-10-09T08:59:00.000Z"它可以在 Java 17 中运行。您知道 Java 17 中的哪些更改导致它失败吗?