下面的代码片段打印了错误的年份。我在这里做错了什么吗?它打印日期时间为2018-12-31 00:00:00但它确实应该是2017-12-31 00:00:00。我在 JavaScript 中转换了相同的纪元,它工作得很好。我还将时区设置为UTC,它给出的结果为,2017-12-30 18:30:00但将时区设置为Asia/Calcutta将其增加一年。我在这里缺少什么?
import java.time.*;
import java.time.format.*;
public class MyClass {
public static void main(String args[]) {
DateTimeFormatter format = DateTimeFormatter.ofPattern("YYYY-MM-dd HH:mm:ss");
String time = LocalDateTime.ofInstant(Instant.ofEpochMilli(1514658600000L), ZoneId.of("Asia/Calcutta")).format(format);
System.out.println(time);
}
}
Run Code Online (Sandbox Code Playgroud) 为什么用下面的代码生成的日期将给定时间偏移到 11:00:00.00?
test("shouldReturnGivenMockedDateTime") {
val mockedDateTime = "2020-01-01T10:00:00.00Z"
val clock: Clock = Clock.fixed(Instant.parse(mockedDateTime), TimeZone.getDefault.toZoneId);
val result = LocalDateTime.ofInstant(clock.instant, TimeZone.getDefault.toZoneId)
assert(result.toString == "2020-01-01T10:00") // FALSE!!!
assert(result.toString == "2020-01-01T11:00") // TRUE
}
Run Code Online (Sandbox Code Playgroud) 我计划编写一个类,extends java.time.YearMonth目的是YearMonth使用一种方法来扩展LocalDates ,该方法使我能够流式传输其中的s YearMonth:
public class ExtendedYearMonth extends YearMonth {
public Stream<LocalDate> days() {
LocalDate firstOfMonth = this.atDay(1);
LocalDate lastOfMonth = firstOfMonth.with(TemporalAdjusters.lastDayOfMonth());
return firstOfMonth.datesUntil(lastOfMonth);
}
}
Run Code Online (Sandbox Code Playgroud)
好吧,当我发现YearMonth是一个final class? final classes 不可扩展。
我当然可以写一个这样的类
public class ExtendedYearMonth {
private YearMonth yearMonth;
// parameterized constructor, getters and setters omitted for brevity
public Stream<LocalDate> days() {
LocalDate firstOfMonth = this.yearMonth.atDay(1);
LocalDate lastOfMonth = firstOfMonth.with(TemporalAdjusters.lastDayOfMonth());
return firstOfMonth.datesUntil(lastOfMonth);
}
}
Run Code Online (Sandbox Code Playgroud)
但这不是我想要的,因为它需要我将 aYearMonth …
我只是想从 TimeZone 获得 offset(+02:00) 但似乎它不适用于 IST、JST、EST 和 BET。
TimeZone exchangeTimeZone = banker.getTimeZone();
String timeZone = ZonedDateTime.now(ZoneId.of(exchangeTimeZone.getID())).getOffset().getId();
Run Code Online (Sandbox Code Playgroud)
它返回错误“未知时区 ID:EST”。我无法使用日期对象。
LocalTime 的文档说它是线程安全的。我知道它本身就是线程安全的。但是,如果我要在像这样的多个线程之间共享它,它会是线程安全的吗?
public class Timesheet {
private static boolean run = true;
private static LocalTime activity;
public static void main(String[] args) {
CompletableFuture.runAsync(() -> {
activity = LocalTime.now();
}
CompletableFuture.runAsync(() -> {
activity = LocalTime.now();
}
try {
while (run) {
Thread.sleep(12000);
run = false;
}
} catch (InterruptedException e) {
} finally {
//print activity
}
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试获得准确的时间。如果我要检查主线程中的活动时间。会准确吗?
编辑:
我现在明白为什么我的代码不是线程安全的。如果我要在同步代码中操作 LocalTime 实例,那会使我的代码线程安全吗?
CompletableFuture.runAsync(() -> {
synchronized (activity) {
activity = LocalTime.now();
}
}
Run Code Online (Sandbox Code Playgroud) 我必须List<Card>在字段issueDate(类型String)上进行过滤,其中卡片要显示到过去 7 年的日期。
我尝试过的解决方案是issueDate从Stringto解析字段Date,然后应用date.after(startDate),date.before(endDate)如下所示:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
cardList.stream()
.map(s->s.getIssueDate())
.filter(dt -> sdf.parse(dt).after(sdf.parse("2020-06-08")) &&
sdf.parse(dt).before(sdf.parse("2020-08-12")))
.collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)
甚至不能申请循环,因为可能有数百张卡发行,它通过迭代每个发卡日期(字符串到日期和检查年份)来影响性能
任何人都可以提出一个很好的解决方案来解决这个问题吗?
如果我能按照我的要求做,那就完美了!
<<LocalTime is provided as locTime>>
Date flyDate = a.getDate();
Date landingDate = b.getDate();
flyDate.add(locTime);
Run Code Online (Sandbox Code Playgroud)
或者我可以了解什么是最佳实践。
我有一堆 Flight 对象,目前它们每个都有一个 Date 来确定航班的飞行日期/时间,还有一个用于 LocalTime 的持续时间,表示飞行需要多少小时:分钟。
我想检查一个航班在时间方面是否与另一个航班兼容。航班a的着陆时间应该在航班b的飞行时间之前。
谢谢!
这看起来是一件非常简单的事情,但我没有这样做。
我有一个字符串模式,yyyyMMddHH我正在尝试解析2021061104为LocalDateTime
这是代码:
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.format.DateTimeParseException;
import java.time.temporal.ChronoField;
import java.time.temporal.ChronoUnit;
class Main {
public static void main(String[] args) {
String pattern = "yyyyMMddHH";
String date = "2021061104";
DateTimeFormatter formatter =
new DateTimeFormatterBuilder()
.parseDefaulting(ChronoField.HOUR_OF_DAY, 0)
.parseDefaulting(ChronoField.MINUTE_OF_HOUR, 0)
.parseDefaulting(ChronoField.SECOND_OF_MINUTE, 0)
.parseLenient()
.appendPattern(pattern)
.toFormatter();
LocalDateTime ldt = LocalDateTime.parse(date, formatter);
}
}
Run Code Online (Sandbox Code Playgroud)
它抛出这个异常:
Exception in thread "main" java.time.format.DateTimeParseException: Text '2021061104' could not be parsed at index 8
at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1949)
at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1851)
at …Run Code Online (Sandbox Code Playgroud) 我试图通过将区域偏移量(例如“GMT+2”)作为字符串来获取所有 zoneId。但是,我不确定是否可以使用任何 Java 8+ 库。是否可以获得具有给定区域偏移量的所有区域 id 值?
有一个名为getAvailableZoneIds()的方法,java.time但它不带偏移参数。