如何解决如何在Java中仅更改Date对象的时间,使日期信息保持不变
我有一个Date变量,可以说deliveryDate。它以这种格式保存在MonngoDB中。
ISODate("2020-10-07T03:10:00Z")
现在,我想更改交货日期的时间属性。但是它必须基于一些String来完成,该String会告诉您设置什么时间。例如 String time =“ 7:20 AM” [这次是马来西亚的吉隆坡]。那么结果应该是这样的:
deliveryDate = ISODate("2020-10-07T11:20:00Z")
一些一般性说明:星期一,美国协调时间,世界协调时间(UTC)在马来西亚吉隆坡联邦直辖区的吉隆坡,上午7:20。
现在给定字符串时间和日期交付日期。在所有情况下,如何获得上述结果?
我期望的是:
public static Date adjustTimeOfDay(Date deliveryDate,String timeOfDay) {
// Adjust the time of the day of deliveryDate on the basis of timeOfDay
// Keep the offset and zone same while adjusting
return deliveryDate
}
解决方法
您可以使用兼容性方法java.util.Date.toInstant()
,操作该Instant
的一天中的时间,这会创建一个不同的Instant
,然后通过{{转换回Date
1}}。
以下是操作的示例方法:
Date.from(Instant instant)
我在public static Instant adjustTimeOfDay(Instant instant,String timeOfDay) {
// convert the instant to an offset-aware datetime object
OffsetDateTime deliveryOdt = OffsetDateTime.ofInstant(instant,ZoneOffset.UTC);
/*
* provide a formatter that parses a time-of-day String.
* PLEASE NOT that this formatter is not very lenient,* the String must be of the pattern "hh:mm a"
*/
DateTimeFormatter dtf = new DateTimeFormatterBuilder().appendPattern("hh")
.appendLiteral(':')
.appendPattern("mm")
.appendLiteral(' ')
.appendPattern("a")
.parseCaseInsensitive()
.toFormatter();
// parse that String to a LocalTime
LocalTime localTime = LocalTime.parse(timeOfDay,dtf);
/*
* create a new OffsetDateTime
* adding the new LocalTime to the old LocalDate at UTC
*/
OffsetDateTime adjustedOdt = OffsetDateTime.of(deliveryOdt.toLocalDate(),localTime,ZoneOffset.UTC);
return adjustedOdt.toInstant();
}
中这样使用:
main
创建了以下输出:
public static void main(String[] args) {
/*
* instead of creating a Date,* I directly use Instant here and parse your example String,* so just use your deliveryDate.toInstant()
*/
String input = "2020-10-07T03:10:00Z";
Instant instant = Instant.parse(input);
// then take a time of day to be set
String timeOfDayUpdate = "07:20 AM";
Instant adjusted = adjustTimeOfDay(instant,timeOfDayUpdate);
System.out.println(input + " ==> " + OffsetDateTime.ofInstant(adjusted,ZoneOffset.UTC)
.format(DateTimeFormatter.ISO_INSTANT));
}
编辑
您可以将该方法重写为
2020-10-07T03:10:00Z ==> 2020-10-07T07:20:00Z
传递public static Date adjustTimeOfDay(Date date,String timeOfDay) {
// convert the date to an instant and the instant to an offset-aware datetime object
OffsetDateTime deliveryOdt = OffsetDateTime.ofInstant(date.toInstant(),ZoneOffset.UTC);
// provide a formatter that parses a time-of-day String
DateTimeFormatter dtf = new DateTimeFormatterBuilder().appendPattern("hh")
.appendLiteral(':')
.appendPattern("mm")
.appendLiteral(' ')
.appendPattern("a")
.parseCaseInsensitive()
.toFormatter();
// parse that String to a LocalTime
LocalTime localTime = LocalTime.parse(timeOfDay,ZoneOffset.UTC);
// return a Date from the Instant you get out of the OffsetDateTime
return Date.from(adjustedOdt.toInstant());
}
并返回一个。
如果我正确理解了您的要求,那么您正在寻找类似的东西:
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
// Test
System.out.println(adjustTimeOfDay("2020-10-07T03:10:00Z","7:20 AM"));
}
public static String adjustTimeOfDay(String deliveryDate,String timeOfDay) {
// Define the formatter to parse time like 7:20 AM
DateTimeFormatter timeFormatter = new DateTimeFormatterBuilder()
.parseCaseInsensitive()
.appendPattern("h:m a")
.toFormatter(Locale.ENGLISH);
return ZonedDateTime.parse(deliveryDate)
.toLocalDate()
.atTime(LocalTime.parse(timeOfDay,timeFormatter))
.atZone(ZoneId.of("Asia/Kuala_Lumpur"))
.withZoneSameInstant(ZoneOffset.UTC)
.format(DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ss")) + 'Z';
}
}
输出:
2020-10-06T23:20:00Z
我建议您使用HH
以24小时格式使用一段时间。但是,如果您想通过忽略AM/PM
来获取时间字符串,则可以按照上面给出的模式使用hh
,然后您会得到2020-10-06T11:20:00Z
(但我不建议这样做会让任何人感到困惑。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。