我有一个场景,我的输入日期格式是yyyy-MM-dd,并且所需的输出日期格式必须是yyyy-MM-dd HH:mm:ss.SSSJava 8 或更高版本。例子:
输入:2022-03-15
预期 O/P:2022-03-15 12:00:00.000
我没有java.time经常使用该 API,因为我的工作主要涉及捕获、存储和显示日期和时间,而不以任何方式操作它们。但有时,我必须做这样的事情:
我还认为,基于其他不相关的经验,人类倾向于认为“过去 30 天”实际上意味着“过去 30 天的一天的开始”,而不仅仅是“现在减去30*24*60*60*1000过去的毫秒”。只需 System.currentTimeMillis 和算术即可轻松完成。
所以我的要求是:
如果我要使用旧的 API 来执行此操作,我会这样做:
TimeZone zone = ...; // However I get my time zone
Locale locale = ...; // However I get my locale
Calendar now = Calendar.getInstance(zone, locale);
// Reset to midnight
now.set(Calendar.HOUR, 0);
now.set(Calendar.MINUTE, 0);
now.set(Calendar.SECOND, 0);
now.set(Calendar.MILLISECOND, 0);
// Now go back 30 days
now.add(Calendar.DAY, -30);
// done
Run Code Online (Sandbox Code Playgroud)
除了重复调用以清空 的时间部分之外Calendar,它非常简单。它只使用了 3 个类,其中两个是 和 …
public static void main(String[] args) {
System.out.println("Norfolk Island times as per Joda Time");
IntStream.range(1, 13)
.forEach(month -> System.out.println(DateTime.now((DateTimeZone.forID("Pacific/Norfolk")))
.withYear(2023)
.withMonthOfYear(month)
.withDayOfMonth(10)
.withHourOfDay(2)
.withMinuteOfHour(10)
.withSecondOfMinute(2)
.withMillisOfSecond(0)));
System.out.println("-------------------------------------");
System.out.println("Norfolk Island times as per JDK OffsetDateTime");
IntStream.range(1, 13)
.forEach(month -> System.out.println(OffsetDateTime.now(ZoneId.of("Pacific/Norfolk"))
.withYear(2023)
.withMonth(month)
.withDayOfMonth(10)
.withHour(2)
.withMinute(10)
.withSecond(2)
.withNano(0)));
System.out.println("-------------------------------------");
}
Run Code Online (Sandbox Code Playgroud)
输出:
Norfolk Island times as per Joda Time
2023-01-10T02:10:02.000+12:00
2023-02-10T02:10:02.000+12:00
2023-03-10T02:10:02.000+12:00
2023-04-10T02:10:02.000+11:00
2023-05-10T02:10:02.000+11:00
2023-06-10T02:10:02.000+11:00
2023-07-10T02:10:02.000+11:00
2023-08-10T02:10:02.000+11:00
2023-09-10T02:10:02.000+11:00
2023-10-10T02:10:02.000+12:00
2023-11-10T02:10:02.000+12:00
2023-12-10T02:10:02.000+12:00
-------------------------------------
Norfolk Island times as per JDK OffsetDateTime
2023-01-10T02:10:02+11:00
2023-02-10T02:10:02+11:00
2023-03-10T02:10:02+11:00
2023-04-10T02:10:02+11:00
2023-05-10T02:10:02+11:00 …Run Code Online (Sandbox Code Playgroud) 我有一个 ZULU 时间戳,必须将其转换为巴黎时区。
ZULU 2022-11-04T06:10:08.606+00:00 --> Paris 2022-11-04T07:10:08.606+01:00
Run Code Online (Sandbox Code Playgroud)
并且必须注意 DST 例如:夏季时间 +2 小时 冬季时间 +1 小时
我编写了以下代码,该代码在本地按预期工作,但在服务器(巴黎)上部署时未按预期工作。
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Locale;
public class ParisTime {
public static void main(String[] args) throws ParseException {
// String date = "2022-05-31T23:30:12.209+00:00";
String date = "2022-11-04T06:10:08.606+00:00";
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS", Locale.ENGLISH);
LocalDateTime dateTime = dateFormat.parse(date).toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
ZonedDateTime of = ZonedDateTime.of(dateTime, ZoneId.of("Europe/Paris"));
String hourDiff = of.toString().substring(of.toString().indexOf('+') + 1, of.toString().indexOf('+') + 3);
String zonedDateTime = of.plusHours(Integer.valueOf(hourDiff)).toString();
String newDatetime …Run Code Online (Sandbox Code Playgroud) 我有一个像这样的 UTC 日期时间(字符串):2022-11-22T17:15:00
还有一个像这样的 ZoneID:"America/Tijuana"
使用 java.time API,我想获取该区域的实际日期时间,即:(2022-11-22T09:15:00时间是 09:15 而不是 17:15)
2022-11-22T17:152022-11-22T17:15-08:00[America/Tijuana]以上都没有给我我正在寻找的东西。
这是我的代码:
ZoneId zonaID = ZoneId.of('America/Tijuana');
CharSequence dateUTC = "2022-11-22T17:15:00";
LocalDateTime dateTimeL = LocalDateTime.parse(dateUTC);
ZonedDateTime myZDT = ZonedDateTime.now();
ZonedDateTime myZDTFinal = myZDT.of(dateTimeL, zonaID);
System.out.println("using toLocalDateTime: " + myZDTFinal.toLocalDateTime());
System.out.println("using toString: " + myZDTFinal.toString());
Run Code Online (Sandbox Code Playgroud)
我知道这可能是一个重复的问题,但有很多关于日期时间的问题,我只是无法弄清楚这一点。
任何帮助将不胜感激。
我正在研究 Java Date(Java 8)。
我有 2 个日期,例如:2023-02-22和2023-02-28
我的预期输出包含:
2023-02-22,2023-02-23,2023-02-24,2023-02-25,2023-02-26,2023-02-27,2023-02-28
日期格式:yyyy-MM-dd
我的代码:
public List<String> getDatesBetween(@NotEmpty String fromDate, @NotEmpty String toDate) throws Exception {
List<String> dates = new ArrayList<>();
// get dates between 2 dates
return dates;
}
Run Code Online (Sandbox Code Playgroud)
如何从该日期获取列表日期?谢谢,
我试图使用 Instant.parse 方法解析一个非常大的日期(但仍然小于 Instant.MAX),但出现错误。
String input = "78000000-01-01T00:00:00Z";
Instant instant = Instant.parse(input);
Run Code Online (Sandbox Code Playgroud)
例外:
Exception in thread "main" java.time.format.DateTimeParseException: Text '78000000-01-01T00:00:00Z' could not be parsed at index 0
at java.base/java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:2106)
at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:2008)
at java.base/java.time.Instant.parse(Instant.java:399)
Run Code Online (Sandbox Code Playgroud)
BC 日期“-78000000-01-01T00:00:00Z”已解析正常。
我是否发现了 Java 中的错误:)?
我有一个事件的纪元时间,以eventTime秒为单位,区域(事件的位置)为userZone。我需要检查某个活动是否符合资格。仅当事件在当天结束之前发生(考虑到用户所在的区域)时,事件才符合资格。由于纪元时间采用 UTC 格式,因此需要将其转换为时区时间,然后在该时区中查找一天的开始和结束。Java中有一个库ZonedDateTime。
我需要实现的是:
- Get eventTime in epoch (UTC) => getEventTime(event)
- Get the userZone (for example CDT or EST) => getUserZone(user)
- Convert eventTime to the local time of the user => getEventInLocalTime(eventTime, userZone)
- Get end of the day time for userZone => getEndOfToday(userZone)
- if (0 <= eventLocalTime <= endOfToday) return True
Run Code Online (Sandbox Code Playgroud)
有人曾经ZonedDateTime帮助过这个逻辑吗?我实现了一些逻辑,但不确定它,因为ZonedDateTime我不清楚当天结束时返回的对象。基本上,我想要达到的目标是,如果两个不同时区的两个用户在同一纪元时间触发事件,那么显然,具有较早时区的用户将有资格获得更少的时间。
我有一个具有 startDateTime 字段的模型,该字段以 OffsetDateTime 格式存储 DateTime 。
startDateTime: 2023-07-25T04:40:46.143-08:00
Run Code Online (Sandbox Code Playgroud)
然而,当我们将上述对象存储在 GCP 上的 PostgreSQL 数据库中时,它会以以下格式存储:
2023-07-25 12:40:46.143+00
看起来它正在将上述对象调整为 UTC 时间。
但我的要求是,它应该以与对象中相同的格式存储,并且不应调整为 UTC 时间。
我探索了这里给出的方法,但没有一个方法满足我的要求。
有人可以建议是否有办法实现这一目标。任何帮助,将不胜感激。
我使用的代码和输入是startDateTime: 2023-07-25T12:40:46.143Z
和"timeZoneOffset": "UTC-08:00"。我正在根据 timezoneOffset 值将输入转换为预期的 OffsetDateTime。
val actualDateTime: OffsetDateTime = OffsetDateTime.parse(startDateTime)
val zoneOffset: ZoneOffset = ZoneOffset.of(timeZoneOffset.replace("UTC", ""))
val expectedDateTime: OffsetDateTime = actualDateTime.withOffsetSameInstant(zoneOffset)
return expectedDateTime.toString()
Run Code Online (Sandbox Code Playgroud)
数据定义语言:
CREATE TABLE IF NOT EXISTS TRANSACTION
(
id uuid DEFAULT,
start_date_time TIMESTAMP WITH TIME ZONE NOT NULL,
end_date_time TIMESTAMP WITH TIME ZONE NOT NULL, …Run Code Online (Sandbox Code Playgroud) joda.time.DateTime.now().toDate()
为什么不java.time.提供这样的方法?什么是转换的最佳方式java.time.LocalDateTime来java.util.date?
我知道我可以做如下,但感觉不对.
java.time.LocalDateTime date;
java.util.Date.from(date.atZone(ZoneId.systemDefault()).toInstant());
Run Code Online (Sandbox Code Playgroud)
我问,因为a java.util.Date仍然在很多系统中使用,比如通过hibernate进行postgres的数据库映射.